From f54aefd491f7f1ffa7f8949005ccf96eb1ecaa3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=88=D0=B8=D0=BD=20=D0=9C=D0=B0=D0=BA=D1=81?= =?UTF-8?q?=D0=B8=D0=BC?= Date: Wed, 5 Apr 2023 22:06:49 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B8=20=D0=B7=D0=B0=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20HeadwaiterStorage=20=D0=B2=20HotelDataBase?= =?UTF-8?q?Implement.Implemets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implemets/HeadwaiterStorage.cs | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 Hotel/HotelDataBaseImplement/Implemets/HeadwaiterStorage.cs diff --git a/Hotel/HotelDataBaseImplement/Implemets/HeadwaiterStorage.cs b/Hotel/HotelDataBaseImplement/Implemets/HeadwaiterStorage.cs new file mode 100644 index 0000000..b3ea8b3 --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Implemets/HeadwaiterStorage.cs @@ -0,0 +1,127 @@ +using HotelContracts.BindingModels; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using HotelDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HotelDataBaseImplement.Implemets +{ + public class HeadwaiterStorage : IHeadwaiterStorage + { + public HeadwaiterViewModel? Delete(HeadwaiterBindingModel model) + { + using var context = new HotelDataBase(); + + var element = context.Headwaiters.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Headwaiters.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public HeadwaiterViewModel? GetElement(HeadwaiterSearchModel model) + { + using var context = new HotelDataBase(); + + if (model.Id.HasValue) + return context.Headwaiters + .Include(x => x.ConferenceBookings) + .Include(x => x.Dinners) + .Include(x => x.Rooms) + .FirstOrDefault(x => x.Id == model.Id)? + .GetViewModel; + + if (!string.IsNullOrEmpty(model.HeadwaiterLogin) && !string.IsNullOrEmpty(model.HeadwaiterPassword)) + return context.Headwaiters + .Include(x => x.ConferenceBookings) + .Include(x => x.Dinners) + .Include(x => x.Rooms) + .FirstOrDefault(x => x.HeadwaiterLogin.Equals(model.HeadwaiterLogin) && x.HeadwaiterPassword.Equals(model.HeadwaiterPassword))? + .GetViewModel; + + if (!string.IsNullOrEmpty(model.HeadwaiterLogin)) + return context.Headwaiters + .Include(x => x.ConferenceBookings) + .Include(x => x.Dinners) + .Include(x => x.Rooms) + .FirstOrDefault(x => x.HeadwaiterLogin.Equals(model.HeadwaiterLogin))? + .GetViewModel; + + return null; + } + + public List GetFilteredList(HeadwaiterSearchModel model) + { + if (string.IsNullOrEmpty(model.HeadwaiterFIO)) + { + return new(); + } + + using var context = new HotelDataBase(); + + return context.Headwaiters + .Include(x => x.ConferenceBookings) + .Include(x => x.Dinners) + .Include(x => x.Rooms) + .Where(x => x.HeadwaiterLogin.Contains(model.HeadwaiterLogin) && x.HeadwaiterPassword == model.HeadwaiterPassword) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new HotelDataBase(); + + return context.Headwaiters + .Select(x => x.GetViewModel) + .ToList(); + } + + public HeadwaiterViewModel? Insert(HeadwaiterBindingModel model) + { + var newHeadwaiter = Headwaiter.Create(model); + + if (newHeadwaiter == null) + { + return null; + } + + using var context = new HotelDataBase(); + + context.Headwaiters.Add(newHeadwaiter); + context.SaveChanges(); + + return newHeadwaiter.GetViewModel; + } + + public HeadwaiterViewModel? Update(HeadwaiterBindingModel model) + { + using var context = new HotelDataBase(); + + var headwaiter = context.Headwaiters + .FirstOrDefault(x => x.Id == model.Id); + + if (headwaiter == null) + { + return null; + } + + headwaiter.Update(model); + context.SaveChanges(); + + return headwaiter.GetViewModel; + } + } +}