public IHttpActionResult RentMovie(RentalRequestContract request) { if (!RentalService.Instance.AuthenticateUser(request.UserID, ActionContext.Request?.Headers?.Authorization)) { return(Unauthorized()); } //authenicate user RentalResponseContract response = RentalService.Instance.HandleRentalRequest(request); return(Ok(response)); }
public RentalResponseContract HandleRentalRequest(RentalRequestContract request) { if (request == null) { throw new Exception(); } var response = new RentalResponseContract(); byte rentalDurationHours = GetRentalDuration(request); long?rentalID; using (var conn = new SqlConnection(ConfigSettings.ConnectionString)) { conn.Open(); //transaction necessary to make sure multiple don't checkout the same dvd/vhs SqlTransaction transaction = conn.BeginTransaction(); try { int?inventoryID = null; if (request.IsPhysicalRental) { inventoryID = RentalRepository.Instance.CheckoutMovie(request.MovieID, conn, transaction); if (inventoryID == null) { throw new Exception("Movie out of stock. Could not fulfill physical rental request"); } } var rental = new RentalEntity(); rental.MovieID = request.MovieID; rental.UserID = request.UserID; rental.InventoryID = inventoryID; rental.RentalDurationHours = rentalDurationHours; rentalID = RentalRepository.Instance.RentMovie(rental, conn, transaction); if (rentalID == null) { throw new Exception("Something went wrong during movie checkout"); } response.IsSuccess = true; response.ConfirmationID = rentalID; //This is probably where you would fire-off a service bus event //to another service dedicated to mailing off a movie transaction.Commit(); conn.Close(); } catch (Exception ex) { transaction.Rollback(); conn.Close(); response.IsSuccess = false; response.Message = ex.Message; } } return(response); }