public void SaveLocation(Location Location) { // Preparare validation return data ICollection<ValidationResult> validationResults; // Try to validate given data if (Location.Validate(out validationResults)) { // If a new Location should be created if (Location.LocationId == 0) { LocationDAL.InsertLocation(Location); } // Existing Location should be updated else { // Check that the Location exists before update if (LocationDAL.GetLocationById(Location.LocationId) == null) { throw new DataBaseEntryNotFoundException(); } // Update Location LocationDAL.UpdateLocation(Location); } } // Validation failed else { // Create exception ApplicationException exception = new ApplicationException("Location object contained invalid values."); // Add validation data to exception. exception.Data.Add("ValidationResults", validationResults); throw exception; } }
public void UpdateLocation(Location location) { // Create connection object using (this.CreateConnection()) { try { SqlCommand cmd; // Connect to database cmd = this.Setup("appSchema.usp_LocationUpdate", DALOptions.closedConnection); // Add in parameters for Stored procedure cmd.Parameters.Add("@LocationId", SqlDbType.Int).Value = location.LocationId; cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = location.Name; cmd.Parameters.Add("@MaxPeople", SqlDbType.SmallInt).Value = location.MaxPeople; cmd.Parameters.Add("@GPSLongitude", SqlDbType.Decimal).Value = location.GPSLongitude; cmd.Parameters.Add("@GPSLatitude", SqlDbType.Decimal).Value = location.GPSLatitude; cmd.Parameters.Add("@ImageSrc", SqlDbType.VarChar, 50).Value = location.ImageSrc; cmd.Parameters.Add("@BookingPricePerHour", SqlDbType.Decimal).Value = location.BookingPricePerHour; cmd.Parameters.Add("@MinutesMarginAfterBooking", SqlDbType.Decimal).Value = location.MinutesMarginAfterBooking; // Open DB connection connection.Open(); // Execute insert to database cmd.ExecuteNonQuery(); } catch (Exception exception) { if (exception.Message == "There is already a location with the given name.") { throw new DuplicateNameException(exception.Message); } // Throw exception throw new ApplicationException(DAL_ERROR_MSG); } } }
public IHttpActionResult Post(Location location) { // Check for bad values, done by the data annotations in the model class. if (!ModelState.IsValid) { return BadRequest(ModelState); } // Try to save location try { locationService.SaveLocation(location); } 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(location); }
// Methods public void LocationDelete(Location Location) { LocationDelete(Location.LocationId); }