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 ResultStorage : IResultStorage { public ResultViewModel? Delete(ResultBindingModel model) { using var context = new SportCompetitionsDatabase(); var element = context.Results.FirstOrDefault(x => x.Id == model.Id); if (element != null) { context.Results.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } public ResultViewModel? GetElement(ResultSearchModel model) { using var context = new SportCompetitionsDatabase(); return context.Results.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ResultName)) && x.ResultName == model.ResultName || model.Id.HasValue && x.Id == model.Id)?.GetViewModel; } public List GetFilteredList(ResultSearchModel model) { if (string.IsNullOrEmpty(model.ResultName)) { return new(); } using var context = new SportCompetitionsDatabase(); return context.Results.Where(x => x.ResultName.Contains(model.ResultName)).Select(x => x.GetViewModel).ToList(); } public List GetFullList() { using var context = new SportCompetitionsDatabase(); return context.Results.Select(x => x.GetViewModel).ToList(); } public ResultViewModel? Insert(ResultBindingModel model) { var newResult = Result.Create(model); if (newResult == null) { return null; } using var context = new SportCompetitionsDatabase(); context.Results.Add(newResult); context.SaveChanges(); return newResult.GetViewModel; } public ResultViewModel? Update(ResultBindingModel model) { using var context = new SportCompetitionsDatabase(); var component = context.Results.FirstOrDefault(x => x.Id == model.Id); if (component == null) { return null; } component.Update(model); context.SaveChanges(); return component.GetViewModel; } } }