Контроллеры: фулл круд

Через сайт: показ, удаление
Ошибка: что то там ограчение планпитания id
Ну и дезигн сомо сабой сделан :3
This commit is contained in:
Кашин Максим 2023-05-17 02:08:51 +04:00
parent 031c9815d7
commit b0cde0c395
23 changed files with 623 additions and 60 deletions

View File

@ -1,5 +1,6 @@
using HostrelHeadwaiterApp.Models; using HostrelHeadwaiterApp.Models;
using HotelContracts.BindingModels; using HotelContracts.BindingModels;
using HotelContracts.SearchModels;
using HotelContracts.ViewModels; using HotelContracts.ViewModels;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Diagnostics; using System.Diagnostics;
@ -137,6 +138,159 @@ namespace HostrelHeadwaiterApp.Controllers
return View(APIClient.GetRequest<List<DinnerViewModel>>($"api/main/getdinnerlist?headwaiterId={APIClient.Headwaiter.Id}")); return View(APIClient.GetRequest<List<DinnerViewModel>>($"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<List<RoomViewModel>>($"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<List<RoomViewModel>>($"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<RoomViewModel, string>? GetRoom(int roomId)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
var result = APIClient.GetRequest<Tuple<RoomViewModel, List<Tuple<string, string>>>>($"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 += "<tr style=\"height: 44px\">";
table += $"<td class=\"u-border-1 u-border-grey-30 u-table-cell\">{dinnerName}</td>";
table += $"<td class=\"u-border-1 u-border-grey-30 u-table-cell\">{dinnerPrice}</td>";
table += "</tr>";
}
return Tuple.Create(result.Item1, table);
}
public IActionResult AddDinnerToRoom()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Rooms = APIClient.GetRequest<List<RoomViewModel>>($"api/main/getroomlist?headwaiterId={APIClient.Headwaiter.Id}");
ViewBag.Dinners = APIClient.GetRequest<List<DinnerViewModel>>($"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<List<RoomViewModel>>($"api/main/getroomlist?headwaiterId={APIClient.Headwaiter.Id}"));
}
[HttpGet] [HttpGet]
public IActionResult Privacy() public IActionResult Privacy()
{ {

View File

@ -0,0 +1,27 @@
@using HotelContracts.ViewModels;
@using HotelDataModels.Models;
@{
ViewData["Title"] = "AddDinnerToRoom";
}
@model Dictionary<int, IDinnerModel>
<form method="post">
<div class="u-form-group u-form-name u-label-top">
<label class="u-label u-text-custom-color-1 u-label-1">Комнаты: </label>
<div class="u-input u-input-rectangle">
<select id="room" name="room" class="form-control" asp-items="@(new SelectList(@ViewBag.Rooms, "Id", "RoomName"))"></select>
</div>
</div>
<div class="u-form-group u-form-name u-label-top">
<label class="u-label u-text-custom-color-1 u-label-1">Обеды: </label>
<div class="u-input u-input-rectangle">
<select id="dinner" name="dinner" class="form-control" asp-items="@(new SelectList(@ViewBag.Dinners, "Id", "DinnerName"))"></select>
</div>
</div>
<div class="u-align-right u-form-group u-form-submit u-label-top">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Добавить участника" class="u-active-custom-color-6 u-border-none u-btn u-btn-submit u-button-style u-custom-color-1 u-hover-custom-color-2 u-btn-1" /></div>
</div>
</form>

View File

@ -0,0 +1,36 @@
@{
ViewData["Title"] = "CreateRoom";
}
<form method="post">
<div class="form-group">
<label class="u-label u-text-custom-color-1 u-label-1">Название комнаты</label>
</div>
<input
type="text"
placeholder="Введите название комнаты"
name="roomName"
class="form-control"/>
<br>
<div class="form-group">
<label class="u-label u-text-custom-color-1 u-label-2">Цена комнаты</label>
</div>
<input
type="number"
placeholder="Введите цену комнаты"
name="roomPrice"
class="form-control"/>
<br>
<div class="form-group">
<label class="u-label u-text-custom-color-1 u-label-2">Корпус</label>
</div>
<input
type="text"
placeholder="Введите корпус комнаты"
name="roomFrame"
class="form-control"/>
<br>
<div class="u-container-layout u-container-layout-2">
<input type="submit" value="Сохранить" class="btn btn-outline-dark text-center d-flex justify-content-md-center" /> </div>
</form>

View File

@ -0,0 +1,18 @@
@{
ViewData["Title"] = "DeleteRoom";
}
<form method="post">
<div class="container d-flex justify-content-center align-items-center0">
<div class="card-body">
<div class="form-group">
<label class="u-label u-text-custom-color-1 u-label-1">Комнаты: </label>
<select id="room" name="room" class="form-control" asp-items="@(new SelectList(@ViewBag.Rooms, "Id", "RoomName"))"></select>
</div>
<br>
<div class="u-container-layout u-container-layout-2">
<input type="submit" value="Сохранить" class="btn btn-outline-dark text-center d-flex justify-content-md-center" />
</div>
</div>
</div>
</form>

View File

@ -0,0 +1,95 @@
@using HotelContracts.ViewModels
@model List<RoomViewModel>
@{
ViewData["Title"] = "ListRooms";
}
<section class="u-clearfix u-section-1" id="sec-e38b">
<div class="u-clearfix u-sheet u-sheet-1">
<div class="u-clearfix u-layout-wrap u-layout-wrap-1">
<div class="u-layout">
<div class="u-layout-row">
<div
class="u-container-style u-layout-cell u-size-48 u-layout-cell-1">
<div class="u-container-layout u-container-layout-1">
<div class="u-table u-table-responsive u-table-1">
<table class="table">
<thead class="thead-dark">
<tr style="height: 31px">
<th class="u-border-1 u-border-grey-50 u-table-cell">
Номер
</th>
<th class="u-border-1 u-border-grey-50 u-table-cell">
Корпус
</th>
<th class="u-border-1 u-border-grey-50 u-table-cell">
Название
</th>
<th class="u-border-1 u-border-grey-50 u-table-cell">
Цена
</th>
</tr>
</thead>
<tbody class="u-table-body">
@foreach (var item in Model)
{
<tr style="height: 75px">
<td
class="u-border-1 u-border-grey-40 u-border-no-left u-border-no-right u-table-cell">
@Html.DisplayFor(modelItem => item.Id)
</td>
<td
class="u-border-1 u-border-grey-40 u-border-no-left u-border-no-right u-table-cell"
>
@Html.DisplayFor(modelItem => item.RoomFrame)
</td>
<td
class="u-border-1 u-border-grey-40 u-border-no-left u-border-no-right u-table-cell"
>
@Html.DisplayFor(modelItem => item.RoomName)
</td>
<td
class="u-border-1 u-border-grey-40 u-border-no-left u-border-no-right u-table-cell"
>
@Html.DisplayFor(modelItem => item.RoomPrice)
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
<div
class="u-container-style u-layout-cell u-size-12 u-layout-cell-2"
>
<div class="u-container-layout u-container-layout-2">
<a
asp-area="" asp-controller="Home" asp-action="CreateRoom"
class="btn btn-outline-dark text-center d-flex justify-content-md-center"
>Добавить</a>
&nbsp;
<a
asp-area="" asp-controller="Home" asp-action="UpdateRoom"
class="btn btn-outline-dark text-center d-flex justify-content-md-center"
>Изменить</a>
&nbsp;
<a
asp-area="" asp-controller="Home" asp-action="DeleteRoom"
class="btn btn-outline-dark text-center d-flex justify-content-md-center"
>Удалить</a>
&nbsp;
<a
asp-area="" asp-controller="Home" asp-action="AddDinnerToRoom"
style="padding: 10 px"
class="btn btn-outline-dark text-center d-flex justify-content-md-center"
>Добавить комнату</a>
</div>
</div>
</div>
</div>
</div>
</div>
</section>

View File

@ -5,9 +5,6 @@
ViewData["Title"] = "UpdateDinner"; ViewData["Title"] = "UpdateDinner";
} }
<head>
<link rel="stylesheet" href="~/css/createdinner.css" asp-append-version="true" />
</head>
<form method="post"> <form method="post">
<div class="container d-flex justify-content-center align-items-center"> <div class="container d-flex justify-content-center align-items-center">

View File

@ -0,0 +1,92 @@
@using HotelContracts.ViewModels;
@using HotelDataModels.Models;
@{
ViewData["Title"] = "UpdateRoom";
}
<form method="post">
<div class="container d-flex justify-content-center align-items-center">
<div class="card-body">
<div class="form-group">
<label class="u-label u-text-custom-color-1 u-label-1">Комнаты: </label>
<select id="room" name="room" class="form-control" asp-items="@(new SelectList(@ViewBag.Rooms, "Id", "RoomName"))"></select>
</div>
<div class="form-group">
<label class="u-label u-text-custom-color-1 u-label-1">Название комнаты</label>
<input
type="text"
id="roomName"
placeholder="Введите название конференции"
name="roomName"
class="form-control"/>
</div>
<div class="form-group">
<label class="u-label u-text-custom-color-1 u-label-2">Цена комнаты</label>
<input
type="number"
id="roomPrice"
placeholder="Выберите начало конференции"
name="roomPrice"
class="form-control"/>
</div>
<div class="form-group">
<label class="u-label u-text-custom-color-1 u-label-1">Корпус</label>
<input
type="text"
id="roomFrame"
placeholder="Введите название конференции"
name="roomFrame"
class="form-control"/>
</div>
<div class="u-table u-table-responsive u-table-1">
<label class="u-label u-text-custom-color-1 u-label-1">Участники конференции</label>
<table class="u-table-entity">
<thead class="u-custom-color-1 u-table-header u-table-header-1">
<tr style="height: 44px">
<th class="u-border-1 u-border-black u-table-cell">
Участники
</th>
<th class="u-border-1 u-border-black u-table-cell">
Гражданство
</th>
</tr>
</thead>
<tbody class="u-table-body" id="table-elements">
</tbody>
</table>
</div>
<br>
<div class="u-container-layout u-container-layout-2">
<input type="submit" value="Сохранить" class="btn btn-outline-dark text-center d-flex justify-content-md-center" />
</div>
</div>
</div>
</form>
@section Scripts
{
<script>
function check() {
var room = $('#room').val();
if (room) {
$.ajax({
method: "GET",
url: "/Home/GetRoom",
data: { roomId: room },
success: function (result) {
$('#roomName').val(result.item1.roomName);
$('#startDate').val(result.item1.startDate);
$('#table-elements').html(result.item2);
}
});
};
}
check();
$('#room').on('change', function () {
check();
});
</script>
}

View File

@ -29,7 +29,7 @@
<a class="nav-link " asp-area="" asp-controller="Home" asp-action="ListDinners">Обеды</a> <a class="nav-link " asp-area="" asp-controller="Home" asp-action="ListDinners">Обеды</a>
<a class="nav-link " asp-area="" asp-controller="Home" asp-action="ShapingDinnerIntoRooms">Комната</a> <a class="nav-link " asp-area="" asp-controller="Home" asp-action="ListRooms">Комнаты</a>
<a class="nav-link " asp-area="" asp-controller="Home" asp-action="FormationOfDinnerInConferenceBookings">Бронирование конференций</a> <a class="nav-link " asp-area="" asp-controller="Home" asp-action="FormationOfDinnerInConferenceBookings">Бронирование конференций</a>

View File

@ -3,6 +3,7 @@ using HotelContracts.BusinessLogicsContracts;
using HotelContracts.SearchModels; using HotelContracts.SearchModels;
using HotelContracts.StoragesContracts; using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels; using HotelContracts.ViewModels;
using HotelDataModels.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -25,6 +26,7 @@ namespace HotelBusinessLogic.BusinessLogics
public bool Create(RoomBindingModel model) public bool Create(RoomBindingModel model)
{ {
CheckModel(model); CheckModel(model);
model.RoomDinners = new();
if (_roomStorage.Insert(model) == null) if (_roomStorage.Insert(model) == null)
{ {
@ -89,6 +91,40 @@ namespace HotelBusinessLogic.BusinessLogics
return list; 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) public bool Update(RoomBindingModel model)
{ {
CheckModel(model); CheckModel(model);

View File

@ -15,6 +15,6 @@ namespace HotelContracts.BindingModels
public int MealPlanId { get; set; } public int MealPlanId { get; set; }
public int Id { get; set; } public int Id { get; set; }
public Dictionary<int, IDinnerModel> RoomDinners { get; set; } public Dictionary<int, IDinnerModel> RoomDinners { get; set; } = new();
} }
} }

View File

@ -1,6 +1,7 @@
using HotelContracts.BindingModels; using HotelContracts.BindingModels;
using HotelContracts.SearchModels; using HotelContracts.SearchModels;
using HotelContracts.ViewModels; using HotelContracts.ViewModels;
using HotelDataModels.Models;
namespace HotelContracts.BusinessLogicsContracts namespace HotelContracts.BusinessLogicsContracts
{ {
@ -8,6 +9,7 @@ namespace HotelContracts.BusinessLogicsContracts
{ {
List<RoomViewModel>? ReadList(RoomSearchModel? model); List<RoomViewModel>? ReadList(RoomSearchModel? model);
RoomViewModel? ReadElement(RoomSearchModel model); RoomViewModel? ReadElement(RoomSearchModel model);
bool AddDinnerToRoom(RoomSearchModel model, IDinnerModel dinner);
bool Create(RoomBindingModel model); bool Create(RoomBindingModel model);
bool Update(RoomBindingModel model); bool Update(RoomBindingModel model);
bool Delete(RoomBindingModel model); bool Delete(RoomBindingModel model);

View File

@ -21,7 +21,7 @@ namespace HotelContracts.ViewModels
public MealPlanViewModel() { } public MealPlanViewModel() { }
[JsonConstructor] //[JsonConstructor]
public MealPlanViewModel(Dictionary<int, MemberViewModel> MealPlanMembers) public MealPlanViewModel(Dictionary<int, MemberViewModel> MealPlanMembers)
{ {
this.MealPlanMembers = MealPlanMembers.ToDictionary(x => x.Key, x => x.Value as IMemberModel); this.MealPlanMembers = MealPlanMembers.ToDictionary(x => x.Key, x => x.Value as IMemberModel);

View File

@ -1,6 +1,7 @@
 
using HotelDataModels.Models; using HotelDataModels.Models;
using System.ComponentModel; using System.ComponentModel;
using Newtonsoft.Json;
namespace HotelContracts.ViewModels namespace HotelContracts.ViewModels
{ {
@ -20,7 +21,14 @@ namespace HotelContracts.ViewModels
[DisplayName("Стоимость комнаты")] [DisplayName("Стоимость комнаты")]
public double RoomPrice { get; set; } public double RoomPrice { get; set; }
public Dictionary<int, IDinnerModel> RoomDinners { get; set; } public Dictionary<int, IDinnerModel> RoomDinners { get; set; } = new();
public RoomViewModel() { }
[JsonConstructor]
public RoomViewModel(Dictionary<int, DinnerViewModel> RoomMembers)
{
this.RoomDinners = RoomDinners.ToDictionary(x => x.Key, x => x.Value as IDinnerModel);
}
} }
} }

View File

@ -9,7 +9,7 @@ namespace HotelDataBaseImplement
{ {
if (optionsBuilder.IsConfigured == false) 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); base.OnConfiguring(optionsBuilder);
} }

View File

@ -44,7 +44,7 @@ namespace HotelDataBaseImplement.Implemets
return context.ConferenceBookings return context.ConferenceBookings
.Include(x => x.Dinners) .Include(x => x.Dinners)
.ThenInclude(x => x.Dinner) .ThenInclude(x => x.Dinner)
.ThenInclude(x => x.RoomDinner) .ThenInclude(x => x.RoomDinners)
.ThenInclude(x => x.Room) .ThenInclude(x => x.Room)
.Include(x => x.Conference) .Include(x => x.Conference)
.Include(x => x.Headwaiter) .Include(x => x.Headwaiter)
@ -65,7 +65,7 @@ namespace HotelDataBaseImplement.Implemets
return context.ConferenceBookings return context.ConferenceBookings
.Include(x => x.Dinners) .Include(x => x.Dinners)
.ThenInclude(x => x.Dinner) .ThenInclude(x => x.Dinner)
.ThenInclude(x => x.RoomDinner) .ThenInclude(x => x.RoomDinners)
.ThenInclude(x => x.Room) .ThenInclude(x => x.Room)
.Include(x => x.Conference) .Include(x => x.Conference)
.Include(x => x.Headwaiter) .Include(x => x.Headwaiter)
@ -77,7 +77,7 @@ namespace HotelDataBaseImplement.Implemets
return context.ConferenceBookings return context.ConferenceBookings
.Include(x => x.Dinners) .Include(x => x.Dinners)
.ThenInclude(x => x.Dinner) .ThenInclude(x => x.Dinner)
.ThenInclude(x => x.RoomDinner) .ThenInclude(x => x.RoomDinners)
.ThenInclude(x => x.Room) .ThenInclude(x => x.Room)
.Include(x => x.Conference) .Include(x => x.Conference)
.Include(x => x.Headwaiter) .Include(x => x.Headwaiter)
@ -88,7 +88,7 @@ namespace HotelDataBaseImplement.Implemets
return context.ConferenceBookings return context.ConferenceBookings
.Include(x => x.Dinners) .Include(x => x.Dinners)
.ThenInclude(x => x.Dinner) .ThenInclude(x => x.Dinner)
.ThenInclude(x => x.RoomDinner) .ThenInclude(x => x.RoomDinners)
.ThenInclude(x => x.Room) .ThenInclude(x => x.Room)
.Include(x => x.Conference) .Include(x => x.Conference)
.Include(x => x.Headwaiter) .Include(x => x.Headwaiter)
@ -104,7 +104,7 @@ namespace HotelDataBaseImplement.Implemets
return context.ConferenceBookings return context.ConferenceBookings
.Include(x => x.Dinners) .Include(x => x.Dinners)
.ThenInclude(x => x.Dinner) .ThenInclude(x => x.Dinner)
.ThenInclude(x => x.RoomDinner) .ThenInclude(x => x.RoomDinners)
.ThenInclude(x => x.Room) .ThenInclude(x => x.Room)
.Include(x => x.Conference) .Include(x => x.Conference)
.Include(x => x.Headwaiter) .Include(x => x.Headwaiter)
@ -128,7 +128,7 @@ namespace HotelDataBaseImplement.Implemets
return context.ConferenceBookings return context.ConferenceBookings
.Include(x => x.Dinners) .Include(x => x.Dinners)
.ThenInclude(x => x.Dinner) .ThenInclude(x => x.Dinner)
.ThenInclude(x => x.RoomDinner) .ThenInclude(x => x.RoomDinners)
.ThenInclude(x => x.Room) .ThenInclude(x => x.Room)
.Include(x => x.Conference) .Include(x => x.Conference)
.Include(x => x.Headwaiter) .Include(x => x.Headwaiter)
@ -144,7 +144,7 @@ namespace HotelDataBaseImplement.Implemets
var conferenceBooking = context.ConferenceBookings var conferenceBooking = context.ConferenceBookings
.Include(x => x.Dinners) .Include(x => x.Dinners)
.ThenInclude(x => x.Dinner) .ThenInclude(x => x.Dinner)
.ThenInclude(x => x.RoomDinner) .ThenInclude(x => x.RoomDinners)
.ThenInclude(x => x.Room) .ThenInclude(x => x.Room)
.Include(x => x.Conference) .Include(x => x.Conference)
.Include(x => x.Headwaiter) .Include(x => x.Headwaiter)

View File

@ -41,7 +41,7 @@ namespace HotelDataBaseImplement.Implemets
using var context = new HotelDataBase(); using var context = new HotelDataBase();
return context.Dinners return context.Dinners
.Include(x => x.RoomDinner) .Include(x => x.RoomDinners)
.ThenInclude(x => x.Room) .ThenInclude(x => x.Room)
.Include(x => x.ConferenceBookingDinner) .Include(x => x.ConferenceBookingDinner)
.ThenInclude(x => x.ConferenceBooking) .ThenInclude(x => x.ConferenceBooking)
@ -61,25 +61,23 @@ namespace HotelDataBaseImplement.Implemets
if (model.HeadwaiterId.HasValue) if (model.HeadwaiterId.HasValue)
{ {
return context.Dinners return context.Dinners
.Include(x => x.RoomDinner) .Include(x => x.RoomDinners)
.ThenInclude(x => x.Room) .ThenInclude(x => x.Room)
.Include(x => x.ConferenceBookingDinner) .Include(x => x.ConferenceBookingDinner)
.ThenInclude(x => x.ConferenceBooking) .ThenInclude(x => x.ConferenceBooking)
.Include(x => x.Headwaiter) .Include(x => x.Headwaiter)
.Where(x => x.HeadwaiterId == model.HeadwaiterId) .Where(x => x.HeadwaiterId == model.HeadwaiterId)
.ToList()
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
} }
return context.Dinners return context.Dinners
.Include(x => x.RoomDinner) .Include(x => x.RoomDinners)
.ThenInclude(x => x.Room) .ThenInclude(x => x.Room)
.Include(x => x.ConferenceBookingDinner) .Include(x => x.ConferenceBookingDinner)
.ThenInclude(x => x.ConferenceBooking) .ThenInclude(x => x.ConferenceBooking)
.Include(x => x.Headwaiter) .Include(x => x.Headwaiter)
.Where(x => x.DinnerName.Contains(model.DinnerName)) .Where(x => x.DinnerName.Contains(model.DinnerName))
.ToList()
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
} }
@ -89,12 +87,11 @@ namespace HotelDataBaseImplement.Implemets
using var context = new HotelDataBase(); using var context = new HotelDataBase();
return context.Dinners return context.Dinners
.Include(x => x.RoomDinner) .Include(x => x.RoomDinners)
.ThenInclude(x => x.Room) .ThenInclude(x => x.Room)
.Include(x => x.ConferenceBookingDinner) .Include(x => x.ConferenceBookingDinner)
.ThenInclude(x => x.ConferenceBooking) .ThenInclude(x => x.ConferenceBooking)
.Include(x => x.Headwaiter) .Include(x => x.Headwaiter)
.ToList()
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
} }

View File

@ -19,6 +19,7 @@ namespace HotelDataBaseImplement.Implemets
using var context = new HotelDataBase(); using var context = new HotelDataBase();
var element = context.Rooms var element = context.Rooms
.Include(x => x.Dinners)
.FirstOrDefault(rec => rec.Id == model.Id); .FirstOrDefault(rec => rec.Id == model.Id);
if (element != null) if (element != null)
@ -34,13 +35,33 @@ namespace HotelDataBaseImplement.Implemets
public RoomViewModel? GetElement(RoomSearchModel model) public RoomViewModel? GetElement(RoomSearchModel model)
{ {
if (!model.Id.HasValue && !model.HeadwaiterId.HasValue) if (!model.Id.HasValue && string.IsNullOrEmpty(model.RoomName))
{ {
return null; return null;
} }
using var context = new HotelDataBase(); 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<RoomViewModel> GetFilteredList(RoomSearchModel model)
{
if (!model.Id.HasValue && !model.HeadwaiterId.HasValue)
{
return new();
}
using var context = new HotelDataBase();
if (model.HeadwaiterId.HasValue) if (model.HeadwaiterId.HasValue)
{ {
return context.Rooms return context.Rooms
@ -50,30 +71,12 @@ namespace HotelDataBaseImplement.Implemets
.ThenInclude(x => x.ConferenceBooking) .ThenInclude(x => x.ConferenceBooking)
.Include(x => x.MealPlan) .Include(x => x.MealPlan)
.Include(x => x.Headwaiter) .Include(x => x.Headwaiter)
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))? .Where(x => x.HeadwaiterId == model.HeadwaiterId)
.GetViewModel; .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<RoomViewModel> GetFilteredList(RoomSearchModel model)
{
if (string.IsNullOrEmpty(model.RoomName))
{
return new();
}
using var context = new HotelDataBase();
return context.Rooms return context.Rooms
.Include(x => x.Dinners) .Include(x => x.Dinners)
.ThenInclude(x => x.Dinner) .ThenInclude(x => x.Dinner)
@ -82,6 +85,7 @@ namespace HotelDataBaseImplement.Implemets
.Include(x => x.MealPlan) .Include(x => x.MealPlan)
.Include(x => x.Headwaiter) .Include(x => x.Headwaiter)
.Where(x => x.RoomName.Contains(model.RoomName)) .Where(x => x.RoomName.Contains(model.RoomName))
.ToList()
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
} }
@ -96,6 +100,7 @@ namespace HotelDataBaseImplement.Implemets
.ThenInclude(x => x.ConferenceBooking) .ThenInclude(x => x.ConferenceBooking)
.Include(x => x.MealPlan) .Include(x => x.MealPlan)
.Include(x => x.Headwaiter) .Include(x => x.Headwaiter)
.ToList()
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
} }
@ -112,20 +117,41 @@ namespace HotelDataBaseImplement.Implemets
context.Rooms.Add(newRoom); context.Rooms.Add(newRoom);
context.SaveChanges(); 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) public RoomViewModel? Update(RoomBindingModel model)
{ {
using var context = new HotelDataBase(); using var context = new HotelDataBase();
var room = context.Rooms.FirstOrDefault(x => x.Id == model.Id); using var transaction = context.Database.BeginTransaction();
if (room == null) 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;
} }
} }
} }

View File

@ -24,6 +24,7 @@ namespace HotelDataBaseImplement.Models
private Dictionary<int, IDinnerModel> _conferenceBookingDinners = null; private Dictionary<int, IDinnerModel> _conferenceBookingDinners = null;
[NotMapped]
public Dictionary<int, IDinnerModel> ConferenceBookingDinners public Dictionary<int, IDinnerModel> ConferenceBookingDinners
{ {
get get

View File

@ -25,7 +25,7 @@ namespace HotelDataBaseImplement.Models
public virtual Headwaiter Headwaiter { get; set; } public virtual Headwaiter Headwaiter { get; set; }
[ForeignKey("DinnerId")] [ForeignKey("DinnerId")]
public virtual List<RoomDinner> RoomDinner { get; set; } public virtual List<RoomDinner> RoomDinners { get; set; } = new();
[ForeignKey("DinnercId")] [ForeignKey("DinnercId")]
public virtual List<ConferenceBookingDinner> ConferenceBookingDinner { get; set; } public virtual List<ConferenceBookingDinner> ConferenceBookingDinner { get; set; }

View File

@ -30,13 +30,17 @@ namespace HotelDataBaseImplement.Models
public virtual List<RoomDinner> Dinners { get; set; } public virtual List<RoomDinner> Dinners { get; set; }
private Dictionary<int, IDinnerModel> _roomDinners = null; private Dictionary<int, IDinnerModel> _roomDinners = null;
[NotMapped]
public Dictionary<int, IDinnerModel> RoomDinners public Dictionary<int, IDinnerModel> RoomDinners
{ {
get get
{ {
if (_roomDinners == null) 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; return _roomDinners;
} }
@ -83,10 +87,16 @@ namespace HotelDataBaseImplement.Models
{ {
var roomDinners = context.RoomDinners.Where(rec => rec.RoomId == model.Id).ToList(); 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.RoomDinners.RemoveRange(roomDinners.Where(rec => !model.RoomDinners.ContainsKey(rec.DinnerId)));
context.SaveChanges(); context.SaveChanges();
foreach (var updateDinner in roomDinners)
{
model.RoomDinners.Remove(updateDinner.DinnerId);
}
context.SaveChanges();
} }
var room = context.Rooms.First(x => x.Id == Id); var room = context.Rooms.First(x => x.Id == Id);
@ -96,7 +106,7 @@ namespace HotelDataBaseImplement.Models
context.RoomDinners.Add(new RoomDinner context.RoomDinners.Add(new RoomDinner
{ {
Room = room, Room = room,
Dinner = context.Dinners.First(x => x.Id == cm.Key) Dinner = context.Dinners.First(x => x.Id == cm.Key),
}); });
context.SaveChanges(); context.SaveChanges();
} }

View File

@ -15,7 +15,7 @@ namespace HotelDataBaseImplement.Models
[Required] [Required]
public int Count { get; set; } public int Count { get; set; }
public virtual Room Room { get; set; } public virtual Room Room { get; set; } = new();
public virtual Dinner Dinner { get; set; } public virtual Dinner Dinner { get; set; } = new();
} }
} }

View File

@ -118,7 +118,7 @@ namespace HotelRestApi.Controllers
} }
[HttpGet] [HttpGet]
public List<RoomViewModel>? GetRoomPlanList(int headwaiterId) public List<RoomViewModel>? GetRoomList(int headwaiterId)
{ {
try try
{ {
@ -427,5 +427,65 @@ namespace HotelRestApi.Controllers
throw; throw;
} }
} }
[HttpPost]
public void UpdateRoom(RoomBindingModel model)
{
try
{
model.RoomDinners = null!;
_room.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
[HttpGet]
public Tuple<RoomViewModel, List<Tuple<string, double>>>? 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<RoomSearchModel, DinnerViewModel> model)
{
try
{
_room.AddDinnerToRoom(model.Item1, model.Item2);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка добавления участника в конференцию.");
throw;
}
}
} }
} }

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
@ -7,6 +7,10 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="6.1.0" /> <PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="6.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup> </ItemGroup>