переделал обновление
This commit is contained in:
parent
73068876ef
commit
95a732593a
@ -293,16 +293,13 @@ namespace HotelAdministratorApp.Controllers
|
||||
{
|
||||
return Redirect("~/Home/Enter");
|
||||
}
|
||||
var list = _dinner.ReadList(new DinnerSearchModel { AdministratorId = APIClient.Administrator.Id });
|
||||
var simpDinner = list.Select(x => new { DinnerId = x.Id, DinnerName = x.DinnerName });
|
||||
ViewBag.Dinners = new MultiSelectList(simpDinner, "DinnerId", "DinnerName");
|
||||
|
||||
ViewBag.Rooms = APIClient.GetRequest<List<RoomViewModel>>($"api/main/getroomlist?administratorId={APIClient.Administrator.Id}");
|
||||
|
||||
return View();
|
||||
ViewBag.Rooms = APIClient.GetRequest<List<RoomViewModel>>($"api/main/getroomlist?administratorid={APIClient.Administrator.Id}");
|
||||
ViewBag.Dinners = APIClient.GetRequest<List<DinnerViewModel>>($"api/main/getdinnerlist?administratorid={APIClient.Administrator.Id}");
|
||||
return View();
|
||||
}
|
||||
[HttpPost]
|
||||
public void UpdateRoom(int room, int roomNumber, double roomPrice, DateTime dateCreate)
|
||||
public void UpdateRoom(int room, int roomNumber, double roomPrice, DateTime dateCreate, List<int> dinners)
|
||||
{
|
||||
if (APIClient.Administrator == null)
|
||||
{
|
||||
@ -312,7 +309,11 @@ namespace HotelAdministratorApp.Controllers
|
||||
{
|
||||
throw new Exception("Введите данные");
|
||||
}
|
||||
|
||||
Dictionary<int, IDinnerModel> dinner = new Dictionary<int, IDinnerModel>();
|
||||
foreach (int din in dinners)
|
||||
{
|
||||
dinner.Add(din, new DinnerSearchModel { Id = din } as IDinnerModel);
|
||||
}
|
||||
APIClient.PostRequest("api/main/updateroom", new RoomBindingModel
|
||||
{
|
||||
Id = room,
|
||||
@ -320,6 +321,7 @@ namespace HotelAdministratorApp.Controllers
|
||||
RoomPrice = roomPrice,
|
||||
DateCreate = dateCreate,
|
||||
AdministratorId = APIClient.Administrator.Id,
|
||||
RoomDinners = dinner,
|
||||
});
|
||||
Response.Redirect("Rooms");
|
||||
}
|
||||
@ -345,6 +347,21 @@ namespace HotelAdministratorApp.Controllers
|
||||
});
|
||||
Response.Redirect("Rooms");
|
||||
}
|
||||
[HttpGet]
|
||||
public Tuple<RoomViewModel, List<Tuple<string, double>>>? GetRoom(int roomId)
|
||||
{
|
||||
if (APIClient.Administrator == null)
|
||||
{
|
||||
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
|
||||
}
|
||||
var result = APIClient.GetRequest<Tuple<RoomViewModel, List<Tuple<string, double>>>>($"api/main/getroom?roomid={roomId}");
|
||||
if (result == null)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
public IActionResult CreateConferenceBooking()
|
||||
{
|
||||
if (APIClient.Administrator == null)
|
||||
@ -395,16 +412,13 @@ namespace HotelAdministratorApp.Controllers
|
||||
{
|
||||
return Redirect("~/Home/Enter");
|
||||
}
|
||||
var list = _dinner.ReadList(new DinnerSearchModel { AdministratorId = APIClient.Administrator.Id });
|
||||
var simpDinner = list.Select(x => new { DinnerId = x.Id, DinnerName = x.DinnerName });
|
||||
ViewBag.Dinners = new MultiSelectList(simpDinner, "DinnerId", "DinnerName");
|
||||
|
||||
ViewBag.ConferenceBookings = APIClient.GetRequest<List<ConferenceBookingViewModel>>($"api/main/getConferenceBookinglist?administratorId={APIClient.Administrator.Id}");
|
||||
ViewBag.ConferenceBookings = APIClient.GetRequest<List<ConferenceBookingViewModel>>($"api/main/getconferencebookinglist?administratorid={APIClient.Administrator.Id}");
|
||||
ViewBag.Dinners = APIClient.GetRequest<List<DinnerViewModel>>($"api/main/getdinnerlist?administratorid={APIClient.Administrator.Id}");
|
||||
|
||||
return View();
|
||||
}
|
||||
[HttpPost]
|
||||
public void UpdateConferenceBooking(int conferencebooking, string placeСonference, DateTime dateСonference)
|
||||
public void UpdateConferenceBooking(int conferencebooking, string placeСonference, DateTime dateСonference, List<int> dinners)
|
||||
{
|
||||
if (APIClient.Administrator == null)
|
||||
{
|
||||
@ -414,13 +428,18 @@ namespace HotelAdministratorApp.Controllers
|
||||
{
|
||||
throw new Exception("Введите данные");
|
||||
}
|
||||
|
||||
Dictionary<int, IDinnerModel> dinner = new Dictionary<int, IDinnerModel>();
|
||||
foreach (int din in dinners)
|
||||
{
|
||||
dinner.Add(din, new DinnerSearchModel { Id = din } as IDinnerModel);
|
||||
}
|
||||
APIClient.PostRequest("api/main/updateConferenceBooking", new ConferenceBookingBindingModel
|
||||
{
|
||||
Id = conferencebooking,
|
||||
PlaceСonference = placeСonference,
|
||||
DateСonference = dateСonference,
|
||||
AdministratorId = APIClient.Administrator.Id,
|
||||
ConferenceBookingDinners = dinner,
|
||||
});
|
||||
Response.Redirect("ConferenceBookings");
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
@{
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "AddNumberToMealPlan";
|
||||
}
|
||||
<head>
|
||||
|
@ -13,12 +13,24 @@
|
||||
<select id="conferencebooking" name="conferencebooking" class="form-control" asp-items="@(new SelectList(@ViewBag.ConferenceBookings, "Id", "PlaceСonference"))"></select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<input name="placeСonference" style="margin-bottom: 20px" type="text" placeholder="Введите место конференции" class="form-control" />
|
||||
<input id="placeСonference" name="placeСonference" style="margin-bottom: 20px" type="text" placeholder="Введите место конференции" class="form-control" />
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<input name="dateСonference" type="datetime-local" class="form-control">
|
||||
<input id="dateСonference" name="dateСonference" type="datetime-local" class="form-control">
|
||||
</div>
|
||||
|
||||
<br>
|
||||
<div class="row">
|
||||
<div class="col-4">Обеды:</div>
|
||||
<div class="col-8">
|
||||
<select name="dinners" class="form-control" multiple size="5" id="dinners">
|
||||
@foreach (var dinner in ViewBag.Dinners)
|
||||
{
|
||||
<option value="@dinner.Id" data-name="@dinner.Id">@dinner.DinnerName</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<br>
|
||||
<div class="button">
|
||||
<button class="button-action">Сохранить</button>
|
||||
</div>
|
||||
@ -38,8 +50,8 @@
|
||||
url: "/Home/GetConferenceBooking",
|
||||
data: { conferencebookingId: conferencebooking },
|
||||
success: function (result) {
|
||||
$('#placeСonference').val(result.PlaceСonference);
|
||||
$('#dateСonference').val(result.DateСonference);
|
||||
$('#placeСonference').val(result.placeСonference);
|
||||
$('#dateСonference').val(result.dateСonference);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
@ -1,4 +1,5 @@
|
||||
@{
|
||||
@using HotelContracts.ViewModels;
|
||||
@{
|
||||
ViewData["Title"] = "UpdateRoom";
|
||||
}
|
||||
<head>
|
||||
@ -23,6 +24,18 @@
|
||||
<input type="datetime-local" name="dateCreate" class="form-control" placeholder="Выберите дату начала периода">
|
||||
</div>
|
||||
<br>
|
||||
<div class="row">
|
||||
<div class="col-4">Обеды:</div>
|
||||
<div class="col-8">
|
||||
<select name="dinners" class="form-control" multiple size="5" id="dinners">
|
||||
@foreach (var dinner in ViewBag.Dinners)
|
||||
{
|
||||
<option value="@dinner.Id" data-name="@dinner.Id">@dinner.DinnerName</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<br>
|
||||
<div class="button">
|
||||
<button class="button-action">Сохранить</button>
|
||||
</div>
|
||||
@ -36,6 +49,7 @@
|
||||
<script>
|
||||
function check() {
|
||||
var room = $('#room').val();
|
||||
$("#dinners option:selected").removeAttr("selected");
|
||||
if (room) {
|
||||
$.ajax({
|
||||
method: "GET",
|
||||
@ -45,6 +59,9 @@
|
||||
$('#roomNumber').val(result.roomNumber);
|
||||
$('#roomPrice').val(result.RoomPrice);
|
||||
$('#dateCreate').val(result.DateCreate);
|
||||
$.map(result.item2, function (n) {
|
||||
$(`option[data-name=${n.item2}]`).attr("selected", "selected")
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
@ -20,12 +20,15 @@ namespace HotelContracts.ViewModels
|
||||
public int AdministratorId { get; set; }
|
||||
public int? MealPlanId { get; set; }
|
||||
public Dictionary<int, IDinnerModel> RoomDinners { get; set; } = new();
|
||||
public RoomViewModel() { }
|
||||
|
||||
[JsonConstructor]
|
||||
public RoomViewModel(Dictionary<int, DinnerViewModel> RoomDinners)
|
||||
{
|
||||
this.RoomDinners = RoomDinners.ToDictionary(x => x.Key, x => x.Value as IDinnerModel);
|
||||
public RoomViewModel() {
|
||||
RoomDinners = new Dictionary<int, IDinnerModel>();
|
||||
}
|
||||
}
|
||||
|
||||
[JsonConstructor]
|
||||
public RoomViewModel(Dictionary<int, DinnerViewModel> RoomDinners)
|
||||
{
|
||||
this.RoomDinners = RoomDinners?.ToDictionary(x => x.Key, x => x.Value as IDinnerModel)
|
||||
?? new Dictionary<int, IDinnerModel>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -101,8 +101,7 @@ namespace HotelDataBaseImplement.Implements
|
||||
.FirstOrDefault(x => x.Id == newConferenceBooking.Id)
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public ConferenceBookingViewModel? Update(ConferenceBookingBindingModel model)
|
||||
public ConferenceBookingViewModel? Update(ConferenceBookingBindingModel model)
|
||||
{
|
||||
using var context = new HotelDataBase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
@ -118,11 +117,11 @@ namespace HotelDataBaseImplement.Implements
|
||||
{
|
||||
return null;
|
||||
}
|
||||
elem.Update(model);
|
||||
elem.UpdateDinners(context, model);
|
||||
context.SaveChanges();
|
||||
elem.Update(model);
|
||||
context.SaveChanges();
|
||||
if (model.ConferenceBookingDinners != null)
|
||||
elem.UpdateDinners(context, model);
|
||||
transaction.Commit();
|
||||
transaction.Commit();
|
||||
return elem.GetViewModel;
|
||||
}
|
||||
catch
|
||||
|
@ -142,10 +142,10 @@ namespace HotelDataBaseImplement.Implements
|
||||
{
|
||||
return null;
|
||||
}
|
||||
elem.Update(model);
|
||||
elem.UpdateDinners(context, model);
|
||||
context.SaveChanges();
|
||||
elem.Update(model);
|
||||
context.SaveChanges();
|
||||
if (model.RoomDinners != null)
|
||||
elem.UpdateDinners(context, model);
|
||||
transaction.Commit();
|
||||
return elem.GetViewModel;
|
||||
}
|
||||
|
@ -32,11 +32,9 @@ namespace HotelDataBaseImplement.Models
|
||||
{
|
||||
if (_conferenceBookingDinners == null)
|
||||
{
|
||||
using var context = new HotelDataBase();
|
||||
_conferenceBookingDinners = Dinners
|
||||
.ToDictionary(x => x.DinnerId, x => (context.Dinners
|
||||
.FirstOrDefault(y => y.Id == x.DinnerId)! as IDinnerModel));
|
||||
}
|
||||
_conferenceBookingDinners = Dinners
|
||||
.ToDictionary(x => x.DinnerId, x => (x.Dinner as IDinnerModel));
|
||||
}
|
||||
return _conferenceBookingDinners;
|
||||
}
|
||||
}
|
||||
@ -75,8 +73,12 @@ namespace HotelDataBaseImplement.Models
|
||||
public void UpdateDinners(HotelDataBase context, ConferenceBookingBindingModel model)
|
||||
{
|
||||
var conferenceBookingDinners = context.ConferenceBookingDinners.Where(rec => rec.ConferenceBookingId == model.Id).ToList();
|
||||
|
||||
if (conferenceBookingDinners != null && conferenceBookingDinners.Count > 0)
|
||||
var list = new List<int>();
|
||||
foreach (var x in model.ConferenceBookingDinners)
|
||||
{
|
||||
list.Add(x.Key);
|
||||
}
|
||||
if (conferenceBookingDinners != null && conferenceBookingDinners.Count > 0)
|
||||
{
|
||||
context.ConferenceBookingDinners.RemoveRange(conferenceBookingDinners.Where(rec => !model.ConferenceBookingDinners.ContainsKey(rec.DinnerId)));
|
||||
context.SaveChanges();
|
||||
|
@ -3,6 +3,7 @@ using HotelContracts.ViewModels;
|
||||
using HotelDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Diagnostics;
|
||||
|
||||
|
||||
namespace HotelDataBaseImplement.Models
|
||||
@ -33,11 +34,10 @@ namespace HotelDataBaseImplement.Models
|
||||
{
|
||||
if (_roomDinners == null)
|
||||
{
|
||||
using var context = new HotelDataBase();
|
||||
_roomDinners = Dinners
|
||||
.ToDictionary(x => x.DinnerId, x => (context.Dinners
|
||||
.FirstOrDefault(y => y.Id == x.DinnerId)! as IDinnerModel));
|
||||
}
|
||||
|
||||
_roomDinners = Dinners
|
||||
.ToDictionary(x => x.DinnerId, x => (x.Dinner as IDinnerModel));
|
||||
}
|
||||
return _roomDinners;
|
||||
}
|
||||
}
|
||||
@ -81,8 +81,12 @@ namespace HotelDataBaseImplement.Models
|
||||
public void UpdateDinners(HotelDataBase context, RoomBindingModel model)
|
||||
{
|
||||
var roomDinners = context.RoomDinners.Where(rec => rec.RoomId == model.Id).ToList();
|
||||
|
||||
if (roomDinners != null && roomDinners.Count > 0)
|
||||
var list = new List<int>();
|
||||
foreach (var x in model.RoomDinners)
|
||||
{
|
||||
list.Add(x.Key);
|
||||
}
|
||||
if (roomDinners != null && roomDinners.Count > 0)
|
||||
{
|
||||
context.RoomDinners.RemoveRange(roomDinners.Where(rec => !model.RoomDinners.ContainsKey(rec.DinnerId)));
|
||||
context.SaveChanges();
|
||||
@ -106,6 +110,6 @@ namespace HotelDataBaseImplement.Models
|
||||
context.SaveChanges();
|
||||
}
|
||||
_roomDinners = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -415,7 +415,6 @@ namespace HotelRestApi.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
model.RoomDinners = null!;
|
||||
_room.Update(model);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -433,7 +432,9 @@ namespace HotelRestApi.Controllers
|
||||
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());
|
||||
var res = Tuple.Create(elem, elem.RoomDinners.Select(x => Tuple.Create(x.Value.DinnerName, x.Value.DinnerPrice)).ToList());
|
||||
res.Item1.RoomDinners = null!;
|
||||
return res;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -487,7 +488,6 @@ namespace HotelRestApi.Controllers
|
||||
|
||||
try
|
||||
{
|
||||
model.ConferenceBookingDinners = null!;
|
||||
_conferenceBooking.Update(model);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
Loading…
Reference in New Issue
Block a user