internal IFacadeUpdateResult <BlockData> SaveBlock(BlockData dto) { ArgumentValidator.IsNotNull("dto", dto); FacadeUpdateResult <BlockData> result = new FacadeUpdateResult <BlockData>(); IBlockService service = UnitOfWork.GetService <IBlockService>(); Block instance = RetrieveOrNew <BlockData, Block, IBlockService>(result.ValidationResult, dto.Id); if (result.IsSuccessful) { instance.Name = dto.Name; instance.Description = dto.Description; instance.IsBuiltIn = dto.IsBuiltIn; instance.WidgetName = dto.WidgetName; instance.ModifiedDate = DateTime.Now; var saveQuery = service.Save(instance); result.AttachResult(instance.RetrieveData <BlockData>()); result.Merge(saveQuery); } return(result); }
public async Task <ActionResult> ChangeBookingAsync([FromBody] BookingCM model) { if (CanBook(model.CustomerId)) { return(Ok(false)); } try { var customer = _customerService.GetCustomer(model.CustomerId); if (customer != null) { string currentUserName = (await _userManager.GetUserAsync(User)).UserName; //BlockId da duoc check isFull va time lon hon time hien tai var tickets = _ticketService.GetTickets(t => t.BlockId == model.BlockId).ToList(); List <Ticket> newTickets = new List <Ticket>(); //Check xem Khach hang này đã đặt trong block nay chưa foreach (var item in tickets) { if (item.CustomerId == null) { newTickets.Add(item); } else if (item.CustomerId.Equals(model.CustomerId.ToString())) { return(Ok(false)); } } if (newTickets.Count != 0) { //Xoa customer tu Ticket cu va check xem isFull=true thi set isFull = false var currentTicket = _ticketService.GetTickets(t => t.CustomerId == model.CustomerId.ToString() && t.Block.Date.Date >= DateTime.Now.Date && t.Status == 0).FirstOrDefault(); currentTicket.CustomerId = null; currentTicket.Note = null; if (currentTicket.Block.IsFull) { currentTicket.Block.IsFull = false; } _ticketService.UpdateTicket(currentTicket, currentUserName); //Gan customer vao ticket va check xem neu con 1 ticket thi set block isFull = true newTickets[0].CustomerId = model.CustomerId.ToString(); newTickets[0].BookingDate = DateTime.Now; newTickets[0].Note = model.Note; _ticketService.UpdateTicket(newTickets[0], currentUserName); if (newTickets.Count == 1) // Chỉ còn 1 ticket thì update isFull { var block = _blockService.GetBlock(model.BlockId); block.IsFull = true; _blockService.UpdateBlock(block, currentUserName); } _blockService.Save(); return(Ok(true)); } else { return(Ok(false)); } } else { return(BadRequest("Customer does not existed!")); } } catch (Exception e) { return(BadRequest(e.Message)); } }