From b0cde0c39519a1ba32e9551b5f53f57f5bce0766 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, 17 May 2023 02:08:51 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB?= =?UTF-8?q?=D0=BB=D0=B5=D1=80=D1=8B:=20=D1=84=D1=83=D0=BB=D0=BB=20=D0=BA?= =?UTF-8?q?=D1=80=D1=83=D0=B4=20=D0=A7=D0=B5=D1=80=D0=B5=D0=B7=20=D1=81?= =?UTF-8?q?=D0=B0=D0=B9=D1=82:=20=D0=BF=D0=BE=D0=BA=D0=B0=D0=B7,=20=D1=83?= =?UTF-8?q?=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=9E=D1=88=D0=B8?= =?UTF-8?q?=D0=B1=D0=BA=D0=B0:=20=D1=87=D1=82=D0=BE=20=D1=82=D0=BE=20?= =?UTF-8?q?=D1=82=D0=B0=D0=BC=20=D0=BE=D0=B3=D1=80=D0=B0=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BF=D0=BB=D0=B0=D0=BD=D0=BF=D0=B8=D1=82=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20id=20=D0=9D=D1=83=20=D0=B8=20=D0=B4=D0=B5?= =?UTF-8?q?=D0=B7=D0=B8=D0=B3=D0=BD=20=D1=81=D0=BE=D0=BC=D0=BE=20=D1=81?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D0=B9=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD?= =?UTF-8?q?=20:3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/HomeController.cs | 154 ++++++++++++++++++ .../Views/Home/AddDinnerToRoom.cshtml | 27 +++ .../Views/Home/CreateRoom.cshtml | 36 ++++ .../Views/Home/DeleteRoom.cshtml | 18 ++ .../Views/Home/ListRooms.cshtml | 95 +++++++++++ .../Views/Home/UpdateDinner.cshtml | 3 - .../Views/Home/UpdateRoom.cshtml | 92 +++++++++++ .../Views/Shared/_Layout.cshtml | 2 +- .../BusinessLogics/RoomLogic.cs | 36 ++++ .../BindingModels/RoomBindingModel.cs | 2 +- .../BusinessLogicsContracts/IRoomLogic.cs | 2 + .../ViewModels/MealPlanViewModel.cs | 2 +- .../ViewModels/RoomViewModel.cs | 10 +- Hotel/HotelDataBaseImplement/HotelDataBase.cs | 2 +- .../Implemets/ConferenceBookingStorage.cs | 14 +- .../Implemets/DinnerStorage.cs | 11 +- .../Implemets/RoomStorage.cs | 86 ++++++---- .../Models/ConferenceBooking.cs | 1 + Hotel/HotelDataBaseImplement/Models/Dinner.cs | 2 +- Hotel/HotelDataBaseImplement/Models/Room.cs | 16 +- .../Models/RoomDinner.cs | 4 +- .../Controllers/MainController.cs | 62 ++++++- Hotel/HotelRestApi/HotelRestApi.csproj | 6 +- 23 files changed, 623 insertions(+), 60 deletions(-) create mode 100644 Hotel/HostrelHeadwaiterApp/Views/Home/AddDinnerToRoom.cshtml create mode 100644 Hotel/HostrelHeadwaiterApp/Views/Home/CreateRoom.cshtml create mode 100644 Hotel/HostrelHeadwaiterApp/Views/Home/DeleteRoom.cshtml create mode 100644 Hotel/HostrelHeadwaiterApp/Views/Home/ListRooms.cshtml create mode 100644 Hotel/HostrelHeadwaiterApp/Views/Home/UpdateRoom.cshtml diff --git a/Hotel/HostrelHeadwaiterApp/Controllers/HomeController.cs b/Hotel/HostrelHeadwaiterApp/Controllers/HomeController.cs index 4979e5a..7309559 100644 --- a/Hotel/HostrelHeadwaiterApp/Controllers/HomeController.cs +++ b/Hotel/HostrelHeadwaiterApp/Controllers/HomeController.cs @@ -1,5 +1,6 @@ using HostrelHeadwaiterApp.Models; using HotelContracts.BindingModels; +using HotelContracts.SearchModels; using HotelContracts.ViewModels; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; @@ -137,6 +138,159 @@ namespace HostrelHeadwaiterApp.Controllers return View(APIClient.GetRequest>($"api/main/getdinnerlist?headwaiterId={APIClient.Headwaiter.Id}")); } + public IActionResult CreateRoom() + { + if (APIClient.Headwaiter == null) + { + return Redirect("~/Home/Enter"); + } + return View(); + } + + [HttpPost] + public void CreateRoom(string roomName, double roomPrice, string roomFrame) + { + if (APIClient.Headwaiter == null) + { + throw new Exception("Необходима авторизация"); + } + if (string.IsNullOrEmpty(roomName)) + { + throw new Exception("Введите название"); + } + if (string.IsNullOrEmpty(roomPrice.ToString())) + { + throw new Exception("Введите цену"); + } + APIClient.PostRequest("api/main/createroom", new RoomBindingModel + { + RoomName = roomName, + RoomPrice = roomPrice, + RoomFrame = roomFrame, + HeadwaiterId = APIClient.Headwaiter.Id, + }); + Response.Redirect("ListRooms"); + } + + public IActionResult DeleteRoom() + { + if (APIClient.Headwaiter == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Rooms = APIClient.GetRequest>($"api/main/getroomlist?headwaiterId={APIClient.Headwaiter.Id}"); + return View(); + } + + [HttpPost] + public void DeleteRoom(int room) + { + if (APIClient.Headwaiter == null) + { + throw new Exception("Необходима авторизация"); + } + APIClient.PostRequest("api/main/deleteroom", new RoomBindingModel + { + Id = room + }); + Response.Redirect("ListRooms"); + } + + public IActionResult UpdateRoom() + { + if (APIClient.Headwaiter == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Rooms = APIClient.GetRequest>($"api/main/getroomlist?headwaiterId={APIClient.Headwaiter.Id}"); + return View(); + } + + [HttpPost] + public void UpdateRoom(int room, string roomName, double roomPrice, string roomFrame) + { + if (APIClient.Headwaiter == null) + { + throw new Exception("Необходима авторизация"); + } + if (string.IsNullOrEmpty(roomName)) + { + throw new Exception("Введите название"); + } + if (string.IsNullOrEmpty(roomPrice.ToString())) + { + throw new Exception("Введите цену"); + } + APIClient.PostRequest("api/main/updateroom", new RoomBindingModel + { + Id = room, + RoomName = roomName, + RoomPrice = roomPrice, + RoomFrame = roomFrame, + HeadwaiterId = APIClient.Headwaiter.Id + }); + Response.Redirect("ListRooms"); + } + + [HttpGet] + public Tuple? GetRoom(int roomId) + { + if (APIClient.Headwaiter == null) + { + throw new Exception("Необходима авторизация"); + } + var result = APIClient.GetRequest>>>($"api/main/getroom?roomId={roomId}"); + if (result == null) + { + return default; + } + string table = ""; + for (int i = 0; i < result.Item2.Count; i++) + { + var dinnerName = result.Item2[i].Item1; + var dinnerPrice = result.Item2[i].Item2; + table += ""; + table += $"{dinnerName}"; + table += $"{dinnerPrice}"; + table += ""; + } + return Tuple.Create(result.Item1, table); + } + + public IActionResult AddDinnerToRoom() + { + if (APIClient.Headwaiter == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Rooms = APIClient.GetRequest>($"api/main/getroomlist?headwaiterId={APIClient.Headwaiter.Id}"); + ViewBag.Dinners = APIClient.GetRequest>($"api/main/getdinnerlist?headwaiterId={APIClient.Headwaiter.Id}"); + return View(); + } + + [HttpPost] + public void AddDinnerToRoom(int room, int dinner) + { + if (APIClient.Headwaiter == null) + { + throw new Exception("Необходима авторизация"); + } + APIClient.PostRequest("api/main/AddDinnerToRoom", Tuple.Create( + new RoomSearchModel() { Id = room }, + new DinnerViewModel() { Id = dinner } + )); + Response.Redirect("ListRooms"); + } + + public IActionResult ListRooms() + { + if (APIClient.Headwaiter == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIClient.GetRequest>($"api/main/getroomlist?headwaiterId={APIClient.Headwaiter.Id}")); + } + [HttpGet] public IActionResult Privacy() { diff --git a/Hotel/HostrelHeadwaiterApp/Views/Home/AddDinnerToRoom.cshtml b/Hotel/HostrelHeadwaiterApp/Views/Home/AddDinnerToRoom.cshtml new file mode 100644 index 0000000..fdeadd9 --- /dev/null +++ b/Hotel/HostrelHeadwaiterApp/Views/Home/AddDinnerToRoom.cshtml @@ -0,0 +1,27 @@ +@using HotelContracts.ViewModels; +@using HotelDataModels.Models; + +@{ + ViewData["Title"] = "AddDinnerToRoom"; +} + +@model Dictionary + +
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+
+
+
\ No newline at end of file diff --git a/Hotel/HostrelHeadwaiterApp/Views/Home/CreateRoom.cshtml b/Hotel/HostrelHeadwaiterApp/Views/Home/CreateRoom.cshtml new file mode 100644 index 0000000..7a1c2ad --- /dev/null +++ b/Hotel/HostrelHeadwaiterApp/Views/Home/CreateRoom.cshtml @@ -0,0 +1,36 @@ +@{ + ViewData["Title"] = "CreateRoom"; +} + +
+
+ +
+ +
+
+ +
+ +
+
+ +
+ + +
+
+
+
\ No newline at end of file diff --git a/Hotel/HostrelHeadwaiterApp/Views/Home/DeleteRoom.cshtml b/Hotel/HostrelHeadwaiterApp/Views/Home/DeleteRoom.cshtml new file mode 100644 index 0000000..ccab2f0 --- /dev/null +++ b/Hotel/HostrelHeadwaiterApp/Views/Home/DeleteRoom.cshtml @@ -0,0 +1,18 @@ +@{ + ViewData["Title"] = "DeleteRoom"; +} + +
+
+
+
+ + +
+
+
+ +
+
+
+
\ No newline at end of file diff --git a/Hotel/HostrelHeadwaiterApp/Views/Home/ListRooms.cshtml b/Hotel/HostrelHeadwaiterApp/Views/Home/ListRooms.cshtml new file mode 100644 index 0000000..3e1b82b --- /dev/null +++ b/Hotel/HostrelHeadwaiterApp/Views/Home/ListRooms.cshtml @@ -0,0 +1,95 @@ +@using HotelContracts.ViewModels + +@model List + +@{ + ViewData["Title"] = "ListRooms"; +} + +
+
+
+
+
+
+
+
+ + + + + + + + + + + @foreach (var item in Model) + { + + + + + + + } + +
+ Номер + + Корпус + + Название + + Цена +
+ @Html.DisplayFor(modelItem => item.Id) + + @Html.DisplayFor(modelItem => item.RoomFrame) + + @Html.DisplayFor(modelItem => item.RoomName) + + @Html.DisplayFor(modelItem => item.RoomPrice) +
+
+
+
+ +
+
+
+
+
\ No newline at end of file diff --git a/Hotel/HostrelHeadwaiterApp/Views/Home/UpdateDinner.cshtml b/Hotel/HostrelHeadwaiterApp/Views/Home/UpdateDinner.cshtml index cfef88c..8f73b28 100644 --- a/Hotel/HostrelHeadwaiterApp/Views/Home/UpdateDinner.cshtml +++ b/Hotel/HostrelHeadwaiterApp/Views/Home/UpdateDinner.cshtml @@ -5,9 +5,6 @@ ViewData["Title"] = "UpdateDinner"; } - - -
diff --git a/Hotel/HostrelHeadwaiterApp/Views/Home/UpdateRoom.cshtml b/Hotel/HostrelHeadwaiterApp/Views/Home/UpdateRoom.cshtml new file mode 100644 index 0000000..38cf530 --- /dev/null +++ b/Hotel/HostrelHeadwaiterApp/Views/Home/UpdateRoom.cshtml @@ -0,0 +1,92 @@ +@using HotelContracts.ViewModels; +@using HotelDataModels.Models; + +@{ + ViewData["Title"] = "UpdateRoom"; +} + + + +
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + + + + + + + + + + +
+ Участники + + Гражданство +
+
+
+
+ +
+
+
+ + +@section Scripts + { + +} \ No newline at end of file diff --git a/Hotel/HostrelHeadwaiterApp/Views/Shared/_Layout.cshtml b/Hotel/HostrelHeadwaiterApp/Views/Shared/_Layout.cshtml index fae9055..222d6b3 100644 --- a/Hotel/HostrelHeadwaiterApp/Views/Shared/_Layout.cshtml +++ b/Hotel/HostrelHeadwaiterApp/Views/Shared/_Layout.cshtml @@ -29,7 +29,7 @@ Обеды - Комната + Комнаты Бронирование конференций diff --git a/Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs b/Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs index 8f42701..22d1efd 100644 --- a/Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs +++ b/Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs @@ -3,6 +3,7 @@ using HotelContracts.BusinessLogicsContracts; using HotelContracts.SearchModels; using HotelContracts.StoragesContracts; using HotelContracts.ViewModels; +using HotelDataModels.Models; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -25,6 +26,7 @@ namespace HotelBusinessLogic.BusinessLogics public bool Create(RoomBindingModel model) { CheckModel(model); + model.RoomDinners = new(); if (_roomStorage.Insert(model) == null) { @@ -89,6 +91,40 @@ namespace HotelBusinessLogic.BusinessLogics return list; } + public bool AddDinnerToRoom(RoomSearchModel model, IDinnerModel dinner) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("AddDinnerToRoom. RoomName:{RoomName}.Id:{ Id}", model.RoomName, model.Id); + var element = _roomStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("AddDinnerToRoom element not found"); + return false; + } + + _logger.LogInformation("AddDinnerToRoom find. Id:{Id}", element.Id); + + element.RoomDinners[dinner.Id] = dinner; + + _roomStorage.Update(new() + { + Id = element.Id, + RoomName = element.RoomName, + RoomPrice = element.RoomPrice, + RoomFrame = element.RoomFrame, + MealPlanId = element.MealPlanId, + HeadwaiterId = element.HeadwaiterId, + RoomDinners = element.RoomDinners, + }); + + return true; + } + public bool Update(RoomBindingModel model) { CheckModel(model); diff --git a/Hotel/HotelContracts/BindingModels/RoomBindingModel.cs b/Hotel/HotelContracts/BindingModels/RoomBindingModel.cs index 0cdba5b..dc58f59 100644 --- a/Hotel/HotelContracts/BindingModels/RoomBindingModel.cs +++ b/Hotel/HotelContracts/BindingModels/RoomBindingModel.cs @@ -15,6 +15,6 @@ namespace HotelContracts.BindingModels public int MealPlanId { get; set; } public int Id { get; set; } - public Dictionary RoomDinners { get; set; } + public Dictionary RoomDinners { get; set; } = new(); } } diff --git a/Hotel/HotelContracts/BusinessLogicsContracts/IRoomLogic.cs b/Hotel/HotelContracts/BusinessLogicsContracts/IRoomLogic.cs index 0490efe..a149fad 100644 --- a/Hotel/HotelContracts/BusinessLogicsContracts/IRoomLogic.cs +++ b/Hotel/HotelContracts/BusinessLogicsContracts/IRoomLogic.cs @@ -1,6 +1,7 @@ using HotelContracts.BindingModels; using HotelContracts.SearchModels; using HotelContracts.ViewModels; +using HotelDataModels.Models; namespace HotelContracts.BusinessLogicsContracts { @@ -8,6 +9,7 @@ namespace HotelContracts.BusinessLogicsContracts { List? ReadList(RoomSearchModel? model); RoomViewModel? ReadElement(RoomSearchModel model); + bool AddDinnerToRoom(RoomSearchModel model, IDinnerModel dinner); bool Create(RoomBindingModel model); bool Update(RoomBindingModel model); bool Delete(RoomBindingModel model); diff --git a/Hotel/HotelContracts/ViewModels/MealPlanViewModel.cs b/Hotel/HotelContracts/ViewModels/MealPlanViewModel.cs index 017cf6e..cc04383 100644 --- a/Hotel/HotelContracts/ViewModels/MealPlanViewModel.cs +++ b/Hotel/HotelContracts/ViewModels/MealPlanViewModel.cs @@ -21,7 +21,7 @@ namespace HotelContracts.ViewModels public MealPlanViewModel() { } - [JsonConstructor] + //[JsonConstructor] public MealPlanViewModel(Dictionary MealPlanMembers) { this.MealPlanMembers = MealPlanMembers.ToDictionary(x => x.Key, x => x.Value as IMemberModel); diff --git a/Hotel/HotelContracts/ViewModels/RoomViewModel.cs b/Hotel/HotelContracts/ViewModels/RoomViewModel.cs index c8bb7c8..bc9d51b 100644 --- a/Hotel/HotelContracts/ViewModels/RoomViewModel.cs +++ b/Hotel/HotelContracts/ViewModels/RoomViewModel.cs @@ -1,6 +1,7 @@  using HotelDataModels.Models; using System.ComponentModel; +using Newtonsoft.Json; namespace HotelContracts.ViewModels { @@ -20,7 +21,14 @@ namespace HotelContracts.ViewModels [DisplayName("Стоимость комнаты")] public double RoomPrice { get; set; } - public Dictionary RoomDinners { get; set; } + public Dictionary RoomDinners { get; set; } = new(); + public RoomViewModel() { } + + [JsonConstructor] + public RoomViewModel(Dictionary RoomMembers) + { + this.RoomDinners = RoomDinners.ToDictionary(x => x.Key, x => x.Value as IDinnerModel); + } } } diff --git a/Hotel/HotelDataBaseImplement/HotelDataBase.cs b/Hotel/HotelDataBaseImplement/HotelDataBase.cs index 6d7ccea..b29b026 100644 --- a/Hotel/HotelDataBaseImplement/HotelDataBase.cs +++ b/Hotel/HotelDataBaseImplement/HotelDataBase.cs @@ -9,7 +9,7 @@ namespace HotelDataBaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-VG5USAH\SQLEXPRESS;Initial Catalog=HotelDataBaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-V0ON61E\SQLEXPRESS;Initial Catalog=HotelDataBaseFul;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/Hotel/HotelDataBaseImplement/Implemets/ConferenceBookingStorage.cs b/Hotel/HotelDataBaseImplement/Implemets/ConferenceBookingStorage.cs index 4529d97..5eb3011 100644 --- a/Hotel/HotelDataBaseImplement/Implemets/ConferenceBookingStorage.cs +++ b/Hotel/HotelDataBaseImplement/Implemets/ConferenceBookingStorage.cs @@ -44,7 +44,7 @@ namespace HotelDataBaseImplement.Implemets return context.ConferenceBookings .Include(x => x.Dinners) .ThenInclude(x => x.Dinner) - .ThenInclude(x => x.RoomDinner) + .ThenInclude(x => x.RoomDinners) .ThenInclude(x => x.Room) .Include(x => x.Conference) .Include(x => x.Headwaiter) @@ -65,7 +65,7 @@ namespace HotelDataBaseImplement.Implemets return context.ConferenceBookings .Include(x => x.Dinners) .ThenInclude(x => x.Dinner) - .ThenInclude(x => x.RoomDinner) + .ThenInclude(x => x.RoomDinners) .ThenInclude(x => x.Room) .Include(x => x.Conference) .Include(x => x.Headwaiter) @@ -77,7 +77,7 @@ namespace HotelDataBaseImplement.Implemets return context.ConferenceBookings .Include(x => x.Dinners) .ThenInclude(x => x.Dinner) - .ThenInclude(x => x.RoomDinner) + .ThenInclude(x => x.RoomDinners) .ThenInclude(x => x.Room) .Include(x => x.Conference) .Include(x => x.Headwaiter) @@ -88,7 +88,7 @@ namespace HotelDataBaseImplement.Implemets return context.ConferenceBookings .Include(x => x.Dinners) .ThenInclude(x => x.Dinner) - .ThenInclude(x => x.RoomDinner) + .ThenInclude(x => x.RoomDinners) .ThenInclude(x => x.Room) .Include(x => x.Conference) .Include(x => x.Headwaiter) @@ -104,7 +104,7 @@ namespace HotelDataBaseImplement.Implemets return context.ConferenceBookings .Include(x => x.Dinners) .ThenInclude(x => x.Dinner) - .ThenInclude(x => x.RoomDinner) + .ThenInclude(x => x.RoomDinners) .ThenInclude(x => x.Room) .Include(x => x.Conference) .Include(x => x.Headwaiter) @@ -128,7 +128,7 @@ namespace HotelDataBaseImplement.Implemets return context.ConferenceBookings .Include(x => x.Dinners) .ThenInclude(x => x.Dinner) - .ThenInclude(x => x.RoomDinner) + .ThenInclude(x => x.RoomDinners) .ThenInclude(x => x.Room) .Include(x => x.Conference) .Include(x => x.Headwaiter) @@ -144,7 +144,7 @@ namespace HotelDataBaseImplement.Implemets var conferenceBooking = context.ConferenceBookings .Include(x => x.Dinners) .ThenInclude(x => x.Dinner) - .ThenInclude(x => x.RoomDinner) + .ThenInclude(x => x.RoomDinners) .ThenInclude(x => x.Room) .Include(x => x.Conference) .Include(x => x.Headwaiter) diff --git a/Hotel/HotelDataBaseImplement/Implemets/DinnerStorage.cs b/Hotel/HotelDataBaseImplement/Implemets/DinnerStorage.cs index e1ea424..66ccf18 100644 --- a/Hotel/HotelDataBaseImplement/Implemets/DinnerStorage.cs +++ b/Hotel/HotelDataBaseImplement/Implemets/DinnerStorage.cs @@ -41,7 +41,7 @@ namespace HotelDataBaseImplement.Implemets using var context = new HotelDataBase(); return context.Dinners - .Include(x => x.RoomDinner) + .Include(x => x.RoomDinners) .ThenInclude(x => x.Room) .Include(x => x.ConferenceBookingDinner) .ThenInclude(x => x.ConferenceBooking) @@ -61,25 +61,23 @@ namespace HotelDataBaseImplement.Implemets if (model.HeadwaiterId.HasValue) { return context.Dinners - .Include(x => x.RoomDinner) + .Include(x => x.RoomDinners) .ThenInclude(x => x.Room) .Include(x => x.ConferenceBookingDinner) .ThenInclude(x => x.ConferenceBooking) .Include(x => x.Headwaiter) .Where(x => x.HeadwaiterId == model.HeadwaiterId) - .ToList() .Select(x => x.GetViewModel) .ToList(); } return context.Dinners - .Include(x => x.RoomDinner) + .Include(x => x.RoomDinners) .ThenInclude(x => x.Room) .Include(x => x.ConferenceBookingDinner) .ThenInclude(x => x.ConferenceBooking) .Include(x => x.Headwaiter) .Where(x => x.DinnerName.Contains(model.DinnerName)) - .ToList() .Select(x => x.GetViewModel) .ToList(); } @@ -89,12 +87,11 @@ namespace HotelDataBaseImplement.Implemets using var context = new HotelDataBase(); return context.Dinners - .Include(x => x.RoomDinner) + .Include(x => x.RoomDinners) .ThenInclude(x => x.Room) .Include(x => x.ConferenceBookingDinner) .ThenInclude(x => x.ConferenceBooking) .Include(x => x.Headwaiter) - .ToList() .Select(x => x.GetViewModel) .ToList(); } diff --git a/Hotel/HotelDataBaseImplement/Implemets/RoomStorage.cs b/Hotel/HotelDataBaseImplement/Implemets/RoomStorage.cs index 2421e6a..1173d2b 100644 --- a/Hotel/HotelDataBaseImplement/Implemets/RoomStorage.cs +++ b/Hotel/HotelDataBaseImplement/Implemets/RoomStorage.cs @@ -19,6 +19,7 @@ namespace HotelDataBaseImplement.Implemets using var context = new HotelDataBase(); var element = context.Rooms + .Include(x => x.Dinners) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) @@ -34,13 +35,33 @@ namespace HotelDataBaseImplement.Implemets public RoomViewModel? GetElement(RoomSearchModel model) { - if (!model.Id.HasValue && !model.HeadwaiterId.HasValue) + if (!model.Id.HasValue && string.IsNullOrEmpty(model.RoomName)) { return null; } using var context = new HotelDataBase(); + return context.Rooms + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.ConferenceBookingDinner) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.MealPlan) + .Include(x => x.Headwaiter) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.RoomName) && x.RoomName == model.RoomName) || (model.Id.HasValue && x.Id == model.Id))? + .GetViewModel; + } + + public List GetFilteredList(RoomSearchModel model) + { + if (!model.Id.HasValue && !model.HeadwaiterId.HasValue) + { + return new(); + } + + using var context = new HotelDataBase(); + if (model.HeadwaiterId.HasValue) { return context.Rooms @@ -50,30 +71,12 @@ namespace HotelDataBaseImplement.Implemets .ThenInclude(x => x.ConferenceBooking) .Include(x => x.MealPlan) .Include(x => x.Headwaiter) - .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))? - .GetViewModel; + .Where(x => x.HeadwaiterId == model.HeadwaiterId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } - return context.Rooms - .Include(x => x.Dinners) - .ThenInclude(x => x.Dinner) - .ThenInclude(x => x.ConferenceBookingDinner) - .ThenInclude(x => x.ConferenceBooking) - .Include(x => x.MealPlan) - .Include(x => x.Headwaiter) - .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))? - .GetViewModel; - } - - public List GetFilteredList(RoomSearchModel model) - { - if (string.IsNullOrEmpty(model.RoomName)) - { - return new(); - } - - using var context = new HotelDataBase(); - return context.Rooms .Include(x => x.Dinners) .ThenInclude(x => x.Dinner) @@ -82,6 +85,7 @@ namespace HotelDataBaseImplement.Implemets .Include(x => x.MealPlan) .Include(x => x.Headwaiter) .Where(x => x.RoomName.Contains(model.RoomName)) + .ToList() .Select(x => x.GetViewModel) .ToList(); } @@ -96,6 +100,7 @@ namespace HotelDataBaseImplement.Implemets .ThenInclude(x => x.ConferenceBooking) .Include(x => x.MealPlan) .Include(x => x.Headwaiter) + .ToList() .Select(x => x.GetViewModel) .ToList(); } @@ -112,20 +117,41 @@ namespace HotelDataBaseImplement.Implemets context.Rooms.Add(newRoom); context.SaveChanges(); - return newRoom.GetViewModel; + + return context.Rooms + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.ConferenceBookingDinner) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.MealPlan) + .Include(x => x.Headwaiter) + .FirstOrDefault(x => x.Id == newRoom.Id) + ?.GetViewModel; } public RoomViewModel? Update(RoomBindingModel model) { using var context = new HotelDataBase(); - var room = context.Rooms.FirstOrDefault(x => x.Id == model.Id); - if (room == null) + using var transaction = context.Database.BeginTransaction(); + try { - return null; + var elem = context.Rooms.FirstOrDefault(rec => rec.Id == model.Id); + if (elem == null) + { + return null; + } + elem.Update(model); + context.SaveChanges(); + if (model.RoomDinners != null) + elem.UpdateDinners(context, model); + transaction.Commit(); + return elem.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; } - room.Update(model); - context.SaveChanges(); - return room.GetViewModel; } } } diff --git a/Hotel/HotelDataBaseImplement/Models/ConferenceBooking.cs b/Hotel/HotelDataBaseImplement/Models/ConferenceBooking.cs index c743a1d..042bf36 100644 --- a/Hotel/HotelDataBaseImplement/Models/ConferenceBooking.cs +++ b/Hotel/HotelDataBaseImplement/Models/ConferenceBooking.cs @@ -24,6 +24,7 @@ namespace HotelDataBaseImplement.Models private Dictionary _conferenceBookingDinners = null; + [NotMapped] public Dictionary ConferenceBookingDinners { get diff --git a/Hotel/HotelDataBaseImplement/Models/Dinner.cs b/Hotel/HotelDataBaseImplement/Models/Dinner.cs index b100aba..261e55c 100644 --- a/Hotel/HotelDataBaseImplement/Models/Dinner.cs +++ b/Hotel/HotelDataBaseImplement/Models/Dinner.cs @@ -25,7 +25,7 @@ namespace HotelDataBaseImplement.Models public virtual Headwaiter Headwaiter { get; set; } [ForeignKey("DinnerId")] - public virtual List RoomDinner { get; set; } + public virtual List RoomDinners { get; set; } = new(); [ForeignKey("DinnercId")] public virtual List ConferenceBookingDinner { get; set; } diff --git a/Hotel/HotelDataBaseImplement/Models/Room.cs b/Hotel/HotelDataBaseImplement/Models/Room.cs index 7f5ef76..cc65fc2 100644 --- a/Hotel/HotelDataBaseImplement/Models/Room.cs +++ b/Hotel/HotelDataBaseImplement/Models/Room.cs @@ -30,13 +30,17 @@ namespace HotelDataBaseImplement.Models public virtual List Dinners { get; set; } private Dictionary _roomDinners = null; + [NotMapped] public Dictionary RoomDinners { get { if (_roomDinners == null) { - _roomDinners = Dinners.ToDictionary(recPC => recPC.DinnerId, recPC => (recPC.Dinner as IDinnerModel)); + using var context = new HotelDataBase(); + _roomDinners = Dinners + .ToDictionary(x => x.DinnerId, x => (context.Dinners + .FirstOrDefault(y => y.Id == x.DinnerId)! as IDinnerModel)); } return _roomDinners; } @@ -83,10 +87,16 @@ namespace HotelDataBaseImplement.Models { var roomDinners = context.RoomDinners.Where(rec => rec.RoomId == model.Id).ToList(); - if (roomDinners != null) + if (roomDinners != null && roomDinners.Count > 0) { context.RoomDinners.RemoveRange(roomDinners.Where(rec => !model.RoomDinners.ContainsKey(rec.DinnerId))); context.SaveChanges(); + + foreach (var updateDinner in roomDinners) + { + model.RoomDinners.Remove(updateDinner.DinnerId); + } + context.SaveChanges(); } var room = context.Rooms.First(x => x.Id == Id); @@ -96,7 +106,7 @@ namespace HotelDataBaseImplement.Models context.RoomDinners.Add(new RoomDinner { Room = room, - Dinner = context.Dinners.First(x => x.Id == cm.Key) + Dinner = context.Dinners.First(x => x.Id == cm.Key), }); context.SaveChanges(); } diff --git a/Hotel/HotelDataBaseImplement/Models/RoomDinner.cs b/Hotel/HotelDataBaseImplement/Models/RoomDinner.cs index 46ea020..a4a14e3 100644 --- a/Hotel/HotelDataBaseImplement/Models/RoomDinner.cs +++ b/Hotel/HotelDataBaseImplement/Models/RoomDinner.cs @@ -15,7 +15,7 @@ namespace HotelDataBaseImplement.Models [Required] public int Count { get; set; } - public virtual Room Room { get; set; } - public virtual Dinner Dinner { get; set; } + public virtual Room Room { get; set; } = new(); + public virtual Dinner Dinner { get; set; } = new(); } } diff --git a/Hotel/HotelRestApi/Controllers/MainController.cs b/Hotel/HotelRestApi/Controllers/MainController.cs index af1f260..9426d17 100644 --- a/Hotel/HotelRestApi/Controllers/MainController.cs +++ b/Hotel/HotelRestApi/Controllers/MainController.cs @@ -118,7 +118,7 @@ namespace HotelRestApi.Controllers } [HttpGet] - public List? GetRoomPlanList(int headwaiterId) + public List? GetRoomList(int headwaiterId) { try { @@ -427,5 +427,65 @@ namespace HotelRestApi.Controllers throw; } } + + [HttpPost] + public void UpdateRoom(RoomBindingModel model) + { + try + { + model.RoomDinners = null!; + _room.Update(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка обновления данных"); + throw; + } + } + + [HttpGet] + public Tuple>>? GetRoom(int roomId) + { + try + { + var elem = _room.ReadElement(new RoomSearchModel { Id = roomId }); + if (elem == null) + return null; + return Tuple.Create(elem, elem.RoomDinners.Select(x => Tuple.Create(x.Value.DinnerName, x.Value.DinnerPrice)).ToList()); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения конференции по id={Id}", roomId); + throw; + } + } + + [HttpPost] + public void DeleteRoom(RoomBindingModel model) + { + try + { + _room.Delete(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления конференции"); + throw; + } + } + + [HttpPost] + public void AddDinnerToRoom(Tuple model) + { + try + { + _room.AddDinnerToRoom(model.Item1, model.Item2); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка добавления участника в конференцию."); + throw; + } + } } } diff --git a/Hotel/HotelRestApi/HotelRestApi.csproj b/Hotel/HotelRestApi/HotelRestApi.csproj index 6b15cdf..992212d 100644 --- a/Hotel/HotelRestApi/HotelRestApi.csproj +++ b/Hotel/HotelRestApi/HotelRestApi.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -7,6 +7,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive +