From 77e3b7c3d40d84fc5ae9aa7b975b6011ce836abc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Tue, 30 Apr 2024 18:09:55 +0400 Subject: [PATCH] =?UTF-8?q?Room=20(=D0=B1=D0=B5=D0=B7=20=D0=BE=D1=82=D0=BE?= =?UTF-8?q?=D0=B1=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BE=D0=B1?= =?UTF-8?q?=D0=B5=D0=B4=D0=BE=D0=B2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/RoomLogic.cs | 53 ++------- .../BusinessLogicsContracts/IRoomLogic.cs | 1 - .../ViewModels/RoomViewModel.cs | 8 -- Hotel/HotelDataBaseImplement/HotelDataBase.cs | 2 +- Hotel/HotelDataBaseImplement/Models/Room.cs | 4 +- .../Controllers/HomeController.cs | 93 ++++++--------- .../Views/Home/CreateRoom.cshtml | 76 ++++--------- .../Views/Home/UpdateRoom.cshtml | 45 ++++---- .../Controllers/RoomController.cs | 107 ++++++++---------- 9 files changed, 139 insertions(+), 250 deletions(-) diff --git a/Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs b/Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs index 02c06c0..f94c610 100644 --- a/Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs +++ b/Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs @@ -58,48 +58,11 @@ namespace HotelBusinessLogic.BusinessLogics return element; } - public bool AddLunchToRoom(RoomSearchModel model, ILunchModel lunch) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - - _logger.LogInformation("AddLunchToRoom. RoomName:{RoomName}.Id:{ Id}", model.RoomName, model.Id); - var element = _roomStorage.GetElement(model); - - if (element == null) - { - _logger.LogWarning("AddLunchToRoom element not found"); - return false; - } - - _logger.LogInformation("AddLunchToRoom find. Id:{Id}", element.Id); - - element.RoomLunches[lunch.Id] = lunch; - - _roomStorage.Update(new() - { - Id = element.Id, - RoomName = element.RoomName, - RoomPrice = element.RoomPrice, - RoomFrame = element.RoomFrame, - MealPlanId = element.MealPlanId, - HeadwaiterId = element.HeadwaiterId, - RoomLunches = element.RoomLunches, - }); - - return true; - } - public bool Create(RoomBindingModel model) { CheckModel(model); - model.RoomLunches = new(); - var result = _roomStorage.Insert(model); - - if (result == null) + if (_roomStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; @@ -127,9 +90,7 @@ namespace HotelBusinessLogic.BusinessLogics _logger.LogInformation("Delete. Id:{Id}", model.Id); - var result = _roomStorage.Delete(model); - - if (result == null) + if (_roomStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; @@ -166,6 +127,16 @@ namespace HotelBusinessLogic.BusinessLogics throw new ArgumentNullException("Стоимость комнаты не может быть меньше 0", nameof(model.RoomPrice)); } + var element = _roomStorage.GetElement(new RoomSearchModel + { + RoomName = model.RoomName + }); + + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Услуга с таким названием уже есть"); + } + _logger.LogInformation("Room. RoomName:{RoomName}.RoomFrame:{ RoomFrame}.RoomPrice:{ RoomPrice}. Id: { Id}", model.RoomName, model.RoomFrame, model.RoomPrice, model.Id); } } diff --git a/Hotel/HotelContracts/BusinessLogicsContracts/IRoomLogic.cs b/Hotel/HotelContracts/BusinessLogicsContracts/IRoomLogic.cs index 0abdf76..d975476 100644 --- a/Hotel/HotelContracts/BusinessLogicsContracts/IRoomLogic.cs +++ b/Hotel/HotelContracts/BusinessLogicsContracts/IRoomLogic.cs @@ -9,7 +9,6 @@ namespace HotelContracts.BusinessLogicsContracts { List? ReadList(RoomSearchModel? model); RoomViewModel? ReadElement(RoomSearchModel model); - bool AddLunchToRoom(RoomSearchModel model, ILunchModel lunch); bool Create(RoomBindingModel model); bool Update(RoomBindingModel model); bool Delete(RoomBindingModel model); diff --git a/Hotel/HotelContracts/ViewModels/RoomViewModel.cs b/Hotel/HotelContracts/ViewModels/RoomViewModel.cs index e5b19d8..7495984 100644 --- a/Hotel/HotelContracts/ViewModels/RoomViewModel.cs +++ b/Hotel/HotelContracts/ViewModels/RoomViewModel.cs @@ -22,13 +22,5 @@ namespace HotelContracts.ViewModels public int HeadwaiterId { get; set; } public Dictionary RoomLunches { get; set; } - - public RoomViewModel() { } - - [JsonConstructor] - public RoomViewModel(Dictionary RoomLunches) - { - this.RoomLunches = RoomLunches.ToDictionary(x => x.Key, x => x.Value as ILunchModel); - } } } diff --git a/Hotel/HotelDataBaseImplement/HotelDataBase.cs b/Hotel/HotelDataBaseImplement/HotelDataBase.cs index bd1a1f2..d5436cd 100644 --- a/Hotel/HotelDataBaseImplement/HotelDataBase.cs +++ b/Hotel/HotelDataBaseImplement/HotelDataBase.cs @@ -10,7 +10,7 @@ namespace HotelDataBaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-7DB3VEN\SQLEXPRESS;Initial Catalog=HotelDataBase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-M2G96S06\SQLEXPRESS;Initial Catalog=HotelDataBase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/Hotel/HotelDataBaseImplement/Models/Room.cs b/Hotel/HotelDataBaseImplement/Models/Room.cs index be1422d..d79a13c 100644 --- a/Hotel/HotelDataBaseImplement/Models/Room.cs +++ b/Hotel/HotelDataBaseImplement/Models/Room.cs @@ -37,9 +37,7 @@ namespace HotelDataBaseImplement.Models { if (_roomLunches == null) { - using var context = new HotelDataBase(); - _roomLunches = Lunches.ToDictionary(x => x.LunchId, x => (context.Lunches - .FirstOrDefault(y => y.Id == x.LunchId)! as ILunchModel)); + _roomLunches = Lunches.ToDictionary(x => x.LunchId, x => (x.Lunch as ILunchModel)); } return _roomLunches; } diff --git a/Hotel/HotelHeadwaiterApp/Controllers/HomeController.cs b/Hotel/HotelHeadwaiterApp/Controllers/HomeController.cs index c2f8253..bbd3282 100644 --- a/Hotel/HotelHeadwaiterApp/Controllers/HomeController.cs +++ b/Hotel/HotelHeadwaiterApp/Controllers/HomeController.cs @@ -4,6 +4,7 @@ using HotelContracts.BindingModels; using HotelContracts.SearchModels; using HotelContracts.ViewModels; using HotelDataBaseImplement.Models; +using HotelDataModels.Models; using HotelHeadwaiterApp.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; @@ -235,20 +236,21 @@ namespace HotelHeadwaiterApp.Controllers { return Redirect("~/Home/Enter"); } - return View(APIClient.GetRequest>($"api/room/getroomlist?headwaiterId={APIClient.Headwaiter.Id}")); + return View(APIClient.GetRequest>($"api/room/getrooms?headwaiterId={APIClient.Headwaiter.Id}")); } - public IActionResult CreateRoom() + public IActionResult CreateRoom() { if (APIClient.Headwaiter == null) { return Redirect("~/Home/Enter"); } + ViewBag.Lunches = APIClient.GetRequest>($"api/lunch/getlunches"); return View(); } [HttpPost] - public void CreateRoom(string roomName, double roomPrice, string roomFrame) + public void CreateRoom(string roomName, string roomFrame, double roomPrice, List lunches) { if (APIClient.Headwaiter == null) { @@ -266,12 +268,18 @@ namespace HotelHeadwaiterApp.Controllers { throw new Exception("Цена не может быть отрицательной"); } + Dictionary a = new Dictionary(); + foreach (int lunch in lunches) + { + a.Add(lunch, new LunchSearchModel { Id = lunch } as ILunchModel); + } APIClient.PostRequest("api/room/createroom", new RoomBindingModel { RoomName = roomName, RoomPrice = roomPrice, RoomFrame = roomFrame, HeadwaiterId = APIClient.Headwaiter.Id, + RoomLunches = a }); Response.Redirect("ListRooms"); } @@ -282,12 +290,13 @@ namespace HotelHeadwaiterApp.Controllers { return Redirect("~/Home/Enter"); } - ViewBag.Rooms = APIClient.GetRequest>($"api/room/getroomlist?headwaiterId={APIClient.Headwaiter.Id}"); + ViewBag.Rooms = APIClient.GetRequest>($"api/room/getrooms?headwaiterId={APIClient.Headwaiter.Id}"); + ViewBag.Lunches = APIClient.GetRequest>($"api/lunch/getlunches"); return View(); } [HttpPost] - public void UpdateRoom(int room, string roomName, double roomPrice, string roomFrame) + public void UpdateRoom(int room, string roomName, double roomPrice, string roomFrame, List lunches) { if (APIClient.Headwaiter == null) { @@ -305,70 +314,40 @@ namespace HotelHeadwaiterApp.Controllers { throw new Exception("Цена не может быть отрицательной"); } + Dictionary a = new Dictionary(); + foreach (int lunch in lunches) + { + a.Add(lunch, new LunchSearchModel { Id = lunch } as ILunchModel); + } APIClient.PostRequest("api/room/updateroom", new RoomBindingModel { Id = room, RoomName = roomName, RoomPrice = roomPrice, RoomFrame = roomFrame, - HeadwaiterId = APIClient.Headwaiter.Id + HeadwaiterId = APIClient.Headwaiter.Id, + RoomLunches = a }); Response.Redirect("ListRooms"); } - [HttpGet] - public Tuple? GetRoom(int roomId) - { - if (APIClient.Headwaiter == null) - { - throw new Exception("Необходима авторизация"); - } - var result = APIClient.GetRequest>>>($"api/room/getroombyid?roomId={roomId}"); - if (result == null) - { - return default; - } - string table = ""; - for (int i = 0; i < result.Item2.Count; i++) - { - var lunchName = result.Item2[i].Item1; - var lunchPrice = result.Item2[i].Item2; - table += ""; - table += $"{lunchName}"; - table += $"{lunchPrice}"; - table += ""; - } - return Tuple.Create(result.Item1, table); - } + [HttpGet] + public Tuple>? GetRoom(int roomId) + { + if (APIClient.Headwaiter == null) + { + throw new Exception("Необходима авторизация"); + } + var result = APIClient.GetRequest>>($"api/room/getroom?roomId={roomId}"); + if (result == null) + { + return default; + } - public IActionResult AddLunchToRoom() - { - if (APIClient.Headwaiter == null) - { - return Redirect("~/Home/Enter"); - } - return View(Tuple.Create(APIClient.GetRequest>($"api/room/getroomlist?headwaiterId={APIClient.Headwaiter.Id}"), - APIClient.GetRequest>($"api/lunch/getlunchlist?headwaiterId={APIClient.Headwaiter.Id}"))); - } + return result; + } - [HttpPost] - public void AddLunchToRoom(int room, int[] lunch) - { - if (APIClient.Headwaiter == null) - { - throw new Exception("Необходима авторизация"); - } - for (int i = 0; i < lunch.Length; i++) - { - APIClient.PostRequest("api/room/AddLunchToRoom", Tuple.Create( - new RoomSearchModel() { Id = room }, - new LunchViewModel() { Id = lunch[i] } - )); - } - Response.Redirect("ListRooms"); - } - - public IActionResult DeleteRoom() + public IActionResult DeleteRoom() { if (APIClient.Headwaiter == null) { diff --git a/Hotel/HotelHeadwaiterApp/Views/Home/CreateRoom.cshtml b/Hotel/HotelHeadwaiterApp/Views/Home/CreateRoom.cshtml index 4e8f4a2..696ecae 100644 --- a/Hotel/HotelHeadwaiterApp/Views/Home/CreateRoom.cshtml +++ b/Hotel/HotelHeadwaiterApp/Views/Home/CreateRoom.cshtml @@ -1,7 +1,4 @@ -@using HotelContracts.ViewModels; -@using HotelDataModels.Models; - -@{ +@{ ViewData["Title"] = "CreateRoom"; } @@ -16,15 +13,6 @@ placeholder="Введите название комнаты" name="roomName" class="form-control" /> -
-
- -
- -
@@ -32,51 +20,27 @@ placeholder="Введите корпус комнаты" name="roomFrame" class="form-control" /> -
-
- - - - - - - - - - -
- Обед - - Цена -
+
+ +
+ +
+ +
+
+
+ +
- -@section Scripts -{ - -} \ No newline at end of file diff --git a/Hotel/HotelHeadwaiterApp/Views/Home/UpdateRoom.cshtml b/Hotel/HotelHeadwaiterApp/Views/Home/UpdateRoom.cshtml index 1aa7186..93ebf2d 100644 --- a/Hotel/HotelHeadwaiterApp/Views/Home/UpdateRoom.cshtml +++ b/Hotel/HotelHeadwaiterApp/Views/Home/UpdateRoom.cshtml @@ -21,6 +21,14 @@ name="roomName" class="form-control" />
+
+ + +
- - +
-
- - - - - - - - - - -
- Обед - - Цена -
+
+
+ +

@@ -74,7 +70,10 @@ $('#roomName').val(result.item1.roomName); $('#roomPrice').val(result.item1.roomPrice); $('#roomFrame').val(result.item1.roomFrame); - $('#table-elements').html(result.item2); + $.map(result.item2, function (n) { + console.log("#" + n); + $("#" + n).attr("selected", "selected") + }); } }); }; diff --git a/Hotel/HotelRestApi/Controllers/RoomController.cs b/Hotel/HotelRestApi/Controllers/RoomController.cs index f34dd86..4ed78ed 100644 --- a/Hotel/HotelRestApi/Controllers/RoomController.cs +++ b/Hotel/HotelRestApi/Controllers/RoomController.cs @@ -2,6 +2,7 @@ using HotelContracts.BusinessLogicsContracts; using HotelContracts.SearchModels; using HotelContracts.ViewModels; +using HotelDataBaseImplement.Models; using Microsoft.AspNetCore.Mvc; namespace HotelRestApi.Controllers @@ -19,55 +20,55 @@ namespace HotelRestApi.Controllers _room = room; } - [HttpGet] - public List? GetRoomList(int headwaiterId) - { - try - { - return _room.ReadList(new RoomSearchModel + [HttpGet] + public List GetRooms(int? headwaiterId = null) + { + try + { + List res; + if (!headwaiterId.HasValue) { - HeadwaiterId = headwaiterId, - }); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка получения списка номеров"); - throw; - } - } - - [HttpGet] - public List? GetRooms() - { - try - { - return _room.ReadList(null); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка получения списка номеров"); - throw; - } - } - - [HttpGet] - public RoomViewModel? GetRoomById(int roomId) - { - try - { - return _room.ReadElement(new RoomSearchModel + res = _room.ReadList(null); + } + else { - Id = roomId - }); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка получения номера по id={Id}", roomId); - throw; - } - } + res = _room.ReadList(new RoomSearchModel { HeadwaiterId = headwaiterId }); + } + foreach (var service in res) + { + service.RoomLunches = null; + } + return res; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка номеров"); + throw; + } + } - [HttpPost] + [HttpGet] + public Tuple>? GetRoom(int roomId) + { + try + { + var elem = _room.ReadElement(new RoomSearchModel { Id = roomId }); + if (elem == null) + { + return null; + } + var res = Tuple.Create(elem, elem.RoomLunches.Select(x => x.Value.LunchName).ToList()); + res.Item1.RoomLunches = null!; + return res; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения номера по id={Id}", roomId); + throw; + } + } + + [HttpPost] public void CreateRoom(RoomBindingModel model) { try @@ -109,19 +110,5 @@ namespace HotelRestApi.Controllers throw; } } - - [HttpPost] - public void AddLunchToRoom(Tuple model) - { - try - { - _room.AddLunchToRoom(model.Item1, model.Item2); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка добавления обеда к номеру."); - throw; - } - } } }