SUBD_SportCompetitions/SportCompetitionsDatabaseImplement/Implements/TeamStorage.cs
2024-04-23 21:48:49 +04:00

78 lines
2.2 KiB
C#

using SportCompetitionsContracts.BindingModels;
using SportCompetitionsContracts.SearchModels;
using SportCompetitionsContracts.StoragesContracts;
using SportCompetitionsContracts.ViewModels;
using SportCompetitionsDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SportCompetitionsDatabaseImplement.Implements
{
public class TeamStorage : ITeamStorage
{
public List<TeamViewModel> GetFilteredList(TeamSearchModel model)
{
if (string.IsNullOrEmpty(model.TeamName))
{
return new();
}
using var context = new SportCompetitionsDatabase();
return context.Teams.Where(x => x.TeamName.Contains(model.TeamName)).Select(x => x.GetViewModel).ToList();
}
public List<TeamViewModel> GetFullList()
{
using var context = new SportCompetitionsDatabase();
return context.Teams.Select(x => x.GetViewModel).ToList();
}
public TeamViewModel? Delete(TeamBindingModel model)
{
using var context = new SportCompetitionsDatabase();
var element = context.Teams.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Teams.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public TeamViewModel? GetElement(TeamSearchModel model)
{
using var context = new SportCompetitionsDatabase();
return context.Teams.FirstOrDefault(x => (!string.IsNullOrEmpty(model.TeamName)) && x.TeamName == model.TeamName || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public TeamViewModel? Insert(TeamBindingModel model)
{
var newTeam = Team.Create(model);
if (newTeam == null)
{
return null;
}
using var context = new SportCompetitionsDatabase();
context.Teams.Add(newTeam);
context.SaveChanges();
return newTeam.GetViewModel;
}
public TeamViewModel? Update(TeamBindingModel model)
{
using var context = new SportCompetitionsDatabase();
var component = context.Teams.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}