From 064a82c06444d761cc297484486c9ce28c11fc57 Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Sat, 8 Apr 2023 15:14:22 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20Implements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ContractStorage.cs | 125 ++++++++++++++++++ .../Implements/DealStorage.cs | 125 ++++++++++++++++++ .../Implements/UserStorage.cs | 109 +++++++++++++++ 3 files changed, 359 insertions(+) create mode 100644 CaseAccounting/CaseAccountingDataBaseImplement/Implements/ContractStorage.cs create mode 100644 CaseAccounting/CaseAccountingDataBaseImplement/Implements/DealStorage.cs create mode 100644 CaseAccounting/CaseAccountingDataBaseImplement/Implements/UserStorage.cs diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/Implements/ContractStorage.cs b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/ContractStorage.cs new file mode 100644 index 0000000..dbd593c --- /dev/null +++ b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/ContractStorage.cs @@ -0,0 +1,125 @@ +using CaseAccountingContracts.BindingModels; +using CaseAccountingContracts.SearchModels; +using CaseAccountingContracts.StoragesContracts; +using CaseAccountingContracts.ViewModels; +using CaseAccountingDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CaseAccountingDataBaseImplement.Implements +{ + public class ContractStorage : IContractStorage + { + public ContractViewModel? Delete(ContractBindingModel model) + { + using var context = new CaseAccountingDatabase(); + var element = context.Contracts + .Include(x => x.User) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element == null) + { + return null; + } + context.Contracts.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + + public ContractViewModel? GetElement(ContractSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new CaseAccountingDatabase(); + return context.Contracts + .Include(x => x.User) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(ContractSearchModel model) + { + using var context = new CaseAccountingDatabase(); + if (model.Id.HasValue) + { + return context.Contracts + .Include(x => x.User) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.Date != null) + { + return context.Contracts + .Include(x => x.User) + .Where(x => x.Date == model.Date) + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.UserId.HasValue) + { + return context.Contracts + .Include(x => x.User) + .Where(x => x.UserId == model.UserId) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + return new(); + } + } + + public List GetFullList() + { + using var context = new CaseAccountingDatabase(); + return context.Contracts + .Include(x => x.User) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ContractViewModel? Insert(ContractBindingModel model) + { + var newContract = Contract.Create(model); + if (newContract == null) + { + return null; + } + + using var context = new CaseAccountingDatabase(); + + context.Contracts.Add(newContract); + context.SaveChanges(); + return context.Contracts + .Include(x => x.User) + .FirstOrDefault(x => x.Id == newContract.Id) + ?.GetViewModel; + } + + public ContractViewModel? Update(ContractBindingModel model) + { + using var context = new CaseAccountingDatabase(); + + var deal = context.Contracts + .Include(x => x.User) + .FirstOrDefault(x => x.Id == model.Id); + + if (deal == null) + { + return null; + } + deal.Update(model); + context.SaveChanges(); + return context.Contracts + .Include(x => x.User) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + } +} diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/Implements/DealStorage.cs b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/DealStorage.cs new file mode 100644 index 0000000..5a966ce --- /dev/null +++ b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/DealStorage.cs @@ -0,0 +1,125 @@ +using CaseAccountingContracts.BindingModels; +using CaseAccountingContracts.SearchModels; +using CaseAccountingContracts.StoragesContracts; +using CaseAccountingContracts.ViewModels; +using CaseAccountingDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CaseAccountingDataBaseImplement.Implements +{ + public class DealStorage : IDealStorage + { + public DealViewModel? Delete(DealBindingModel model) + { + using var context = new CaseAccountingDatabase(); + var element = context.Deals + .Include(x => x.User) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element == null) + { + return null; + } + context.Deals.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + + public DealViewModel? GetElement(DealSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new CaseAccountingDatabase(); + return context.Deals + .Include(x => x.User) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(DealSearchModel model) + { + using var context = new CaseAccountingDatabase(); + if (model.Id.HasValue) + { + return context.Deals + .Include(x => x.User) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.Date != null) + { + return context.Deals + .Include(x => x.User) + .Where(x => x.Date == model.Date) + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.UserId.HasValue) + { + return context.Deals + .Include(x => x.User) + .Where(x => x.UserId == model.UserId) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + return new(); + } + } + + public List GetFullList() + { + using var context = new CaseAccountingDatabase(); + return context.Deals + .Include(x => x.User) + .Select(x => x.GetViewModel) + .ToList(); + } + + public DealViewModel? Insert(DealBindingModel model) + { + var newDeal = Deal.Create(model); + if (newDeal == null) + { + return null; + } + + using var context = new CaseAccountingDatabase(); + + context.Deals.Add(newDeal); + context.SaveChanges(); + return context.Deals + .Include(x => x.User) + .FirstOrDefault(x => x.Id == newDeal.Id) + ?.GetViewModel; + } + + public DealViewModel? Update(DealBindingModel model) + { + using var context = new CaseAccountingDatabase(); + + var deal = context.Deals + .Include(x => x.User) + .FirstOrDefault(x => x.Id == model.Id); + + if (deal == null) + { + return null; + } + deal.Update(model); + context.SaveChanges(); + return context.Deals + .Include(x => x.User) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + } +} diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/Implements/UserStorage.cs b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/UserStorage.cs new file mode 100644 index 0000000..dccfbfa --- /dev/null +++ b/CaseAccounting/CaseAccountingDataBaseImplement/Implements/UserStorage.cs @@ -0,0 +1,109 @@ +using CaseAccountingContracts.BindingModels; +using CaseAccountingContracts.SearchModels; +using CaseAccountingContracts.StoragesContracts; +using CaseAccountingContracts.ViewModels; +using CaseAccountingDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CaseAccountingDataBaseImplement.Implements +{ + public class UserStorage : IUserStorage + { + public UserViewModel? Delete(UserBindingModel model) + { + using var context = new CaseAccountingDatabase(); + var element = context.Users + .Include(x => x.Role) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element == null) + { + return null; + } + context.Users.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + + public UserViewModel? GetElement(UserSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new CaseAccountingDatabase(); + return context.Users + .Include(x => x.Role) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(UserSearchModel model) + { + using var context = new CaseAccountingDatabase(); + if (model.Id.HasValue) + { + return context.Users + .Include(x => x.Role) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + return new(); + } + } + + public List GetFullList() + { + using var context = new CaseAccountingDatabase(); + return context.Users + .Include(x => x.Role) + .Select(x => x.GetViewModel) + .ToList(); + } + + public UserViewModel? Insert(UserBindingModel model) + { + var newUser = User.Create(model); + if (newUser == null) + { + return null; + } + + using var context = new CaseAccountingDatabase(); + + context.Users.Add(newUser); + context.SaveChanges(); + return context.Users + .Include(x => x.Role) + .FirstOrDefault(x => x.Id == newUser.Id) + ?.GetViewModel; + } + + public UserViewModel? Update(UserBindingModel model) + { + using var context = new CaseAccountingDatabase(); + + var user = context.Users + .Include(x => x.Role) + .FirstOrDefault(x => x.Id == model.Id); + + if (user == null) + { + return null; + } + user.Update(model); + context.SaveChanges(); + return context.Users + .Include(x => x.Role) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + } +}