public async Task <IActionResult> Update([FromRoute] string email, [FromBody] AccBooking booking) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { _context.Entry(booking).State = EntityState.Modified; Console.WriteLine("State change, yet to save."); await _context.SaveChangesAsync(); Console.WriteLine("Saved."); } catch (DbUpdateConcurrencyException ex) { if (!BookingExists(booking.UserId)) { return(NotFound()); } else { Console.WriteLine("Error updating: " + ex); } } catch (Exception ex) { Console.WriteLine(ex); } return(NoContent()); }
public async Task <IActionResult> New([FromBody] AccBooking booking) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (User.Identity.Name.Equals(booking.UserId.ToString()) || User.IsInRole("Admin")) { try { _context.AccBooking.Add(booking); /// Need to inner join details inorder to create receipt var detail = _context.AccDetail.Where(s => s.DetailId.Equals(booking.DetailId)) .Include(s => s.Prop).ThenInclude(s => s.Acc); var user = await _userDB.User.SingleOrDefaultAsync(s => s.UserId.Equals(booking.UserId)); if (detail.First().AvailableRooms > 0) { detail.First().AvailableRooms -= booking.RoomsBooked; } else { throw new Exception("No rooms available for booking to proceed!"); } if (await TryUpdateModelAsync <AccDetail>(detail.First())) { try { await _context.SaveChangesAsync(); } catch (DbUpdateException ex) { throw ex; } catch (Exception ex) { throw ex; } } EmailMessage message = new EmailMessage("Accommodation Booking.", "Hi " + user.Name + ",<br/><br/>" + "You have just booked for an accommodation using our a web services, the full details of the booking are: <br/>" + detail.First().Prop.Acc.Country + "<br/>" + detail.First().Prop.Acc.Location + "<br/>" + detail.First().Prop.PropName + "<br/>Booking date: " + booking.BookDate + "<br/>Number of nights booked: " + booking.NumOfNights + "<br/>Number of rooms booked: " + booking.RoomsBooked + "<br/>Total: R " + booking.Total + "<br/><br/>Kind Regards,<br/>Booking.com"); message.FromAddresses.Add(new EmailAddress("BookingServer.com", "*****@*****.**")); message.ToAddresses.Add(new EmailAddress(user.Name, user.Email)); new Send(message, _emailConfiguration); await _hubContext.Clients.All.BroadcastMessage("A user has just book for " + detail.First().Prop.PropName + ", " + detail.First().AvailableRooms + " left."); return(CreatedAtAction("GetBooking", new { id = booking.BookingId }, booking)); } catch (Exception ex) { Console.WriteLine("Error: " + ex.InnerException); Console.WriteLine(ex.Message); Console.WriteLine(ex.Source); Console.WriteLine(ex.StackTrace); Console.WriteLine(ex.TargetSite); if (ex.Message.Contains("No rooms available")) { return(NotFound("Rooms are unavailable.")); } return(BadRequest("Internal error.")); } } return(Unauthorized()); }