SUBD_SportCompetitions/SportCompetitionsDatabaseImplement/Implements/MemberStorage.cs

78 lines
2.2 KiB
C#
Raw Normal View History

2024-04-23 21:48:49 +04:00
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 MemberStorage : IMemberStorage
{
public List<MemberViewModel> GetFilteredList(MemberSearchModel model)
{
if (string.IsNullOrEmpty(model.MemberFCs))
{
return new();
}
using var context = new SportCompetitionsDatabase();
return context.Members.Where(x => x.MemberFCs.Contains(model.MemberFCs)).Select(x => x.GetViewModel).ToList();
}
public List<MemberViewModel> GetFullList()
{
using var context = new SportCompetitionsDatabase();
return context.Members.Select(x => x.GetViewModel).ToList();
}
public MemberViewModel? Delete(MemberBindingModel model)
{
using var context = new SportCompetitionsDatabase();
var element = context.Members.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Members.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public MemberViewModel? GetElement(MemberSearchModel model)
{
using var context = new SportCompetitionsDatabase();
return context.Members.FirstOrDefault(x => (!string.IsNullOrEmpty(model.MemberFCs)) && x.MemberFCs == model.MemberFCs || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public MemberViewModel? Insert(MemberBindingModel model)
{
var newMember = Member.Create(model);
if (newMember == null)
{
return null;
}
using var context = new SportCompetitionsDatabase();
context.Members.Add(newMember);
context.SaveChanges();
return newMember.GetViewModel;
}
public MemberViewModel? Update(MemberBindingModel model)
{
using var context = new SportCompetitionsDatabase();
var component = context.Members.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}