78 lines
2.4 KiB
C#
78 lines
2.4 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 CompetitionStorage : ICompetitionStorage
|
|
{
|
|
public List<CompetitionViewModel> GetFilteredList(CompetitionSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.CompetitionName))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new SportCompetitionsDatabase();
|
|
return context.Competitions.Where(x => x.CompetitionName.Contains(model.CompetitionName)).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<CompetitionViewModel> GetFullList()
|
|
{
|
|
using var context = new SportCompetitionsDatabase();
|
|
return context.Competitions.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public CompetitionViewModel? Delete(CompetitionBindingModel model)
|
|
{
|
|
using var context = new SportCompetitionsDatabase();
|
|
var element = context.Competitions.FirstOrDefault(x => x.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Competitions.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public CompetitionViewModel? GetElement(CompetitionSearchModel model)
|
|
{
|
|
using var context = new SportCompetitionsDatabase();
|
|
return context.Competitions.FirstOrDefault(x => (!string.IsNullOrEmpty(model.CompetitionName)) && x.CompetitionName == model.CompetitionName || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
|
}
|
|
|
|
public CompetitionViewModel? Insert(CompetitionBindingModel model)
|
|
{
|
|
var newCompetition = Competition.Create(model);
|
|
if (newCompetition == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new SportCompetitionsDatabase();
|
|
context.Competitions.Add(newCompetition);
|
|
context.SaveChanges();
|
|
return newCompetition.GetViewModel;
|
|
}
|
|
|
|
public CompetitionViewModel? Update(CompetitionBindingModel model)
|
|
{
|
|
using var context = new SportCompetitionsDatabase();
|
|
var component = context.Competitions.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
}
|
|
}
|