From a61d49a15a2cd22fac95f4b2e0edc81a346640c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D0=BE=D0=BD=D0=B8=D0=B4=20=D0=9C=D0=B0=D0=BB?= =?UTF-8?q?=D0=B0=D1=84=D0=B5=D0=B5=D0=B2?= Date: Sun, 28 Apr 2024 17:39:36 +0400 Subject: [PATCH] StorekeeperStorage implement --- .../CarCenterDatabaseImplement.csproj | 4 - .../Implements/StorekeeperStorage.cs | 99 +++++++++++++++++++ 2 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 CarCenter/CarCenterDatabaseImplement/Implements/StorekeeperStorage.cs diff --git a/CarCenter/CarCenterDatabaseImplement/CarCenterDatabaseImplement.csproj b/CarCenter/CarCenterDatabaseImplement/CarCenterDatabaseImplement.csproj index 468320d..77878fd 100644 --- a/CarCenter/CarCenterDatabaseImplement/CarCenterDatabaseImplement.csproj +++ b/CarCenter/CarCenterDatabaseImplement/CarCenterDatabaseImplement.csproj @@ -6,10 +6,6 @@ enable - - - - diff --git a/CarCenter/CarCenterDatabaseImplement/Implements/StorekeeperStorage.cs b/CarCenter/CarCenterDatabaseImplement/Implements/StorekeeperStorage.cs new file mode 100644 index 0000000..829a214 --- /dev/null +++ b/CarCenter/CarCenterDatabaseImplement/Implements/StorekeeperStorage.cs @@ -0,0 +1,99 @@ +using CarCenterContracts.BindingModels; +using CarCenterContracts.SearchModels; +using CarCenterContracts.StoragesContracts; +using CarCenterContracts.ViewModels; +using CarCenterDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarCenterDatabaseImplement.Implements +{ + public class StorekeeperStorage : IStorekeeperStorage + { + public List GetFullList() + { + using var context = new CarCenterDatabase(); + + return context.Storekeepers + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(StorekeeperSearchModel model) + { + if (model == null) + { + return new(); + } + + if (model.Id.HasValue) + { + var res = GetElement(model); + + return res != null ? new() { res } : new(); + } + return new(); + } + + public StorekeeperViewModel? GetElement(StorekeeperSearchModel model) + { + using var context = new CarCenterDatabase(); + + if (model.Id.HasValue) + return context.Storekeepers + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + return null; + } + + + public StorekeeperViewModel? Delete(StorekeeperBindingModel model) + { + using var context = new CarCenterDatabase(); + + var res = context.Storekeepers + .FirstOrDefault(x => x.Id == model.Id); + + if (res != null) + { + context.Storekeepers.Remove(res); + context.SaveChanges(); + } + + return res?.GetViewModel; + } + + public StorekeeperViewModel? Insert(StorekeeperBindingModel model) + { + using var context = new CarCenterDatabase(); + + var res = Storekeeper.Create(model); + + if (res != null) + { + context.Storekeepers.Add(res); + context.SaveChanges(); + } + + return res?.GetViewModel; + } + + public StorekeeperViewModel? Update(StorekeeperBindingModel model) + { + using var context = new CarCenterDatabase(); + + var res = context.Storekeepers.FirstOrDefault(x => x.Id == model.Id); + + if (res != null) + { + res.Update(model); + context.SaveChanges(); + } + + return res?.GetViewModel; + } + } +}