Room (без отображения обедов)

This commit is contained in:
Вячеслав Иванов 2024-04-30 18:09:55 +04:00
parent b969347e3e
commit 77e3b7c3d4
9 changed files with 139 additions and 250 deletions

View File

@ -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);
}
}

View File

@ -9,7 +9,6 @@ namespace HotelContracts.BusinessLogicsContracts
{
List<RoomViewModel>? 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);

View File

@ -22,13 +22,5 @@ namespace HotelContracts.ViewModels
public int HeadwaiterId { get; set; }
public Dictionary<int, ILunchModel> RoomLunches { get; set; }
public RoomViewModel() { }
[JsonConstructor]
public RoomViewModel(Dictionary<int, LunchViewModel> RoomLunches)
{
this.RoomLunches = RoomLunches.ToDictionary(x => x.Key, x => x.Value as ILunchModel);
}
}
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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<List<RoomViewModel>>($"api/room/getroomlist?headwaiterId={APIClient.Headwaiter.Id}"));
return View(APIClient.GetRequest<List<RoomViewModel>>($"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<List<LunchViewModel>>($"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<int> lunches)
{
if (APIClient.Headwaiter == null)
{
@ -266,12 +268,18 @@ namespace HotelHeadwaiterApp.Controllers
{
throw new Exception("Цена не может быть отрицательной");
}
Dictionary<int, ILunchModel> a = new Dictionary<int, ILunchModel>();
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<List<RoomViewModel>>($"api/room/getroomlist?headwaiterId={APIClient.Headwaiter.Id}");
ViewBag.Rooms = APIClient.GetRequest<List<RoomViewModel>>($"api/room/getrooms?headwaiterId={APIClient.Headwaiter.Id}");
ViewBag.Lunches = APIClient.GetRequest<List<LunchViewModel>>($"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<int> lunches)
{
if (APIClient.Headwaiter == null)
{
@ -305,70 +314,40 @@ namespace HotelHeadwaiterApp.Controllers
{
throw new Exception("Цена не может быть отрицательной");
}
Dictionary<int, ILunchModel> a = new Dictionary<int, ILunchModel>();
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<RoomViewModel, string>? GetRoom(int roomId)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
var result = APIClient.GetRequest<Tuple<RoomViewModel, List<Tuple<string, string>>>>($"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 += "<tr style=\"height: 44px\">";
table += $"<td class=\"u-border-1 u-border-grey-30 u-table-cell\">{lunchName}</td>";
table += $"<td class=\"u-border-1 u-border-grey-30 u-table-cell\">{lunchPrice}</td>";
table += "</tr>";
}
return Tuple.Create(result.Item1, table);
}
[HttpGet]
public Tuple<RoomViewModel, List<string>>? GetRoom(int roomId)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
var result = APIClient.GetRequest<Tuple<RoomViewModel, List<string>>>($"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<List<RoomViewModel>>($"api/room/getroomlist?headwaiterId={APIClient.Headwaiter.Id}"),
APIClient.GetRequest<List<LunchViewModel>>($"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)
{

View File

@ -1,7 +1,4 @@
@using HotelContracts.ViewModels;
@using HotelDataModels.Models;
@{
@{
ViewData["Title"] = "CreateRoom";
}
@ -16,15 +13,6 @@
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>
@ -32,51 +20,27 @@
placeholder="Введите корпус комнаты"
name="roomFrame"
class="form-control" />
<br>
<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="table">
<thead class="thead-dark">
<tr style="height: 44px">
<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" id="table-elements">
</tbody>
</table>
<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" />
<div class="form-group">
<label class="u-label u-text-custom-color-1 u-label-2">Обеды</label>
</div>
<div class="form-group">
<div class="col-8">
<select name="lunches" class="form-control" multiple size="6" id="lunches">
@foreach (var lunch in ViewBag.Lunches)
{
<option value="@lunch.Id">@lunch.LunchName</option>
}
</select>
</div>
</div>
<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>
@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);
$('#roomPrice').val(result.item1.roomPrice);
$('#roomFrame').val(result.item1.roomFrame);
$('#table-elements').html(result.item2);
}
});
};
}
check();
$('#room').on('change', function () {
check();
});
</script>
}

View File

@ -21,6 +21,14 @@
name="roomName"
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="form-group">
<label class="u-label u-text-custom-color-1 u-label-2">Цена комнаты</label>
<input type="number"
@ -30,29 +38,17 @@
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" />
<label class="u-label u-text-custom-color-1 u-label-2">Обеды</label>
</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="table">
<thead class="thead-dark">
<tr style="height: 44px">
<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" id="table-elements">
</tbody>
</table>
<div class="form-group">
<div class="col-8">
<select name="lunches" class="form-control" multiple size="6" id="lunches">
@foreach (var lunch in ViewBag.Lunches)
{
<option value="@lunch.Id">@lunch.LunchName</option>
}
</select>
</div>
</div>
<br>
<div class="u-container-layout u-container-layout-2">
@ -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")
});
}
});
};

View File

@ -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<RoomViewModel>? GetRoomList(int headwaiterId)
{
try
{
return _room.ReadList(new RoomSearchModel
[HttpGet]
public List<RoomViewModel> GetRooms(int? headwaiterId = null)
{
try
{
List<RoomViewModel> res;
if (!headwaiterId.HasValue)
{
HeadwaiterId = headwaiterId,
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка номеров");
throw;
}
}
[HttpGet]
public List<RoomViewModel>? 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<RoomViewModel, List<string>>? 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<RoomSearchModel, LunchViewModel> model)
{
try
{
_room.AddLunchToRoom(model.Item1, model.Item2);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка добавления обеда к номеру.");
throw;
}
}
}
}