public IHttpActionResult Post(BookingType bookingType) { // Check for bad values, done by the data annotations in the model class. if (!ModelState.IsValid) { return BadRequest(ModelState); } // Try to save bookingType try { bookingTypeService.SaveBookingType(bookingType); } catch (DataBaseEntryNotFoundException) { return NotFound(); } catch (DuplicateNameException) { return Conflict(); } catch (ApprovedException exception) { return BadRequest(exception.Message); } catch { return InternalServerError(); } // Respond that the booking was created and redirect return Ok(bookingType); //CreatedAtRoute("DefaultApi", new { id = bookingType.BookingTypeId }, bookingType); }
public void SaveBookingType(BookingType BookingType) { // Preparare validation return data ICollection<ValidationResult> validationResults; // Try to validate given data if (BookingType.Validate(out validationResults)) { // If a new BookingType should be created if (BookingType.BookingTypeId == 0) { BookingTypeDAL.InsertBookingType(BookingType); } // Existing BookingType should be updated else { // Check that the BookingType exists before update if (BookingTypeDAL.GetBookingTypeById(BookingType.BookingTypeId) == null) { throw new DataBaseEntryNotFoundException(); } // Update BookingType BookingTypeDAL.UpdateBookingType(BookingType); } } // Validation failed else { // Create exception ApplicationException exception = new ApplicationException("BookingType object contained invalid values."); // Add validation data to exception. exception.Data.Add("ValidationResults", validationResults); throw exception; } }
// Methods public void BookingTypeDelete(BookingType BookingType) { BookingTypeDelete(BookingType.BookingTypeId); }
public void UpdateBookingType(BookingType BookingType) { // Create connection object using (this.CreateConnection()) { try { SqlCommand cmd; // Connect to database cmd = this.Setup("appSchema.usp_BookingTypeUpdate", DALOptions.closedConnection); // Add in parameters for Stored procedure cmd.Parameters.Add("@BookingTypeId", SqlDbType.SmallInt).Value = BookingType.BookingTypeId; cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = BookingType.Name; cmd.Parameters.Add("@HasLocation", SqlDbType.Bit).Value = BookingType.HasLocation; cmd.Parameters.Add("@MinutesMarginBeforeBooking", SqlDbType.SmallInt).Value = BookingType.MinutesMarginBeforeBooking; cmd.Parameters.Add("@MinutesMarginAfterBooking", SqlDbType.SmallInt).Value = BookingType.MinutesMarginAfterBooking; // Open DB connection connection.Open(); // Execute insert to database cmd.ExecuteNonQuery(); } catch (Exception exception) { if (exception.Message == "There is already a bookingtype with the given name.") { throw new DuplicateNameException(exception.Message); } // Throw exception throw new ApplicationException(DAL_ERROR_MSG); } } }