public void SaveLocationBooking(LocationBooking locationBooking) { // Preparare validation return data ICollection<ValidationResult> validationResults; // Try to validate given data if (locationBooking.Validate(out validationResults)) { // If a new booking should be created if (locationBooking.LocationBookingId == 0) { LocationBookingDAL.InsertLocationBooking(locationBooking); } // Existing booking should be updated else { // Check that the booking exists before update if (LocationBookingDAL.GetLocationBookingById(locationBooking.LocationBookingId) == null) { throw new ApplicationException("The location booking that was to be updated does not exist anymore."); } // Update booking LocationBookingDAL.UpdateLocationBooking(locationBooking); } } // Validation failed else { // Create exception ApplicationException exception = new ApplicationException("The location booking contained invalid values."); // Add validation data to exception. exception.Data.Add("ValidationResults", validationResults); throw exception; } }
public IHttpActionResult Post(LocationBooking locationBooking) { // Check for bad values, done by the data annotations in the model class. if (!ModelState.IsValid) { return BadRequest(ModelState); } // Try to save locationBooking try { locationBookingService.SaveLocationBooking(locationBooking); } catch (DoubleBookingException) { return Conflict(); } catch (ApprovedException exception) { return BadRequest(exception.Message); } catch { return InternalServerError(); } // Respond that the booking was created and redirect return Ok(locationBooking); }
public void UpdateLocationBooking(LocationBooking locationBooking) { // Create connection object using (this.CreateConnection()) { try { SqlCommand cmd; // Connect to database cmd = this.Setup("appSchema.usp_LocationBookingUpdate", DALOptions.closedConnection); // Add in parameters for Stored procedure cmd.Parameters.Add("@LocationBookingId", SqlDbType.Int).Value = locationBooking.LocationBookingId; cmd.Parameters.Add("@BookingId", SqlDbType.Int).Value = locationBooking.BookingId; cmd.Parameters.Add("@LocationId", SqlDbType.Int).Value = locationBooking.LocationId; cmd.Parameters.Add("@FurnituringId", SqlDbType.Int).Value = locationBooking.FurnituringId; cmd.Parameters.Add("@StartTime", SqlDbType.SmallDateTime).Value = locationBooking.StartTime; cmd.Parameters.Add("@EndTime", SqlDbType.SmallDateTime).Value = locationBooking.EndTime; cmd.Parameters.Add("@NumberOfPeople", SqlDbType.SmallInt).Value = locationBooking.NumberOfPeople; // Open DB connection connection.Open(); // Execute insert to database cmd.ExecuteNonQuery(); } catch (Exception exception) { if (exception.Message == "The location is already booked at the given time.") { throw new DoubleBookingException(exception.Message); } // Throw exception throw new ApplicationException(DAL_ERROR_MSG); } } }