public IHttpActionResult PostRestaurant([FromBody] CreateRestaurantApiModel createRestaurantApiModel) { var restaurantReviewsV1Manager = new RestaurantReviewsV1Manager(); int newRestaurantApiId; ResponseBllModel responseBllModel = restaurantReviewsV1Manager.PostRestaurant(createRestaurantApiModel, out newRestaurantApiId); if (responseBllModel.Result == ResultEnum.Success) { return(Ok(newRestaurantApiId)); } else { return(GetResponseApiActionResult(responseBllModel)); } }
internal static Restaurant GetEntity(CreateRestaurantApiModel createRestaurantApiModel) { DateTime utcNow = DateTime.UtcNow; var itemEntity = new Restaurant { Name = createRestaurantApiModel.Name, AddressLine1 = createRestaurantApiModel.AddressLine1, AddressLine2 = createRestaurantApiModel.AddressLine2, City = createRestaurantApiModel.City, StateProvince = createRestaurantApiModel.StateProvince, PostalCode = createRestaurantApiModel.PostalCode, Country = createRestaurantApiModel.Country, CreatedDate = utcNow, ModifiedDate = utcNow }; return(itemEntity); }
public ResponseBllModel PostRestaurant(CreateRestaurantApiModel createRestaurantApiModel, out int newRestaurantApiId) { newRestaurantApiId = 0; try { //Validation checks: string parameterName; if (createRestaurantApiModel == null) { parameterName = "createRestaurantApiModel"; return(new ResponseBllModel { Result = ResultEnum.MissingParameter, ParameterName = parameterName, ErrorMessage = MessageHelper.GetMissingParameterErrorMessage(parameterName) }); } if (string.IsNullOrWhiteSpace(createRestaurantApiModel.Name)) { parameterName = "createRestaurantApiModel.Name"; return(new ResponseBllModel { Result = ResultEnum.MissingParameter, ParameterName = parameterName, ErrorMessage = MessageHelper.GetMissingParameterErrorMessage(parameterName) }); } if (string.IsNullOrWhiteSpace(createRestaurantApiModel.AddressLine1)) { parameterName = "createRestaurantApiModel.AddressLine1"; return(new ResponseBllModel { Result = ResultEnum.MissingParameter, ParameterName = parameterName, ErrorMessage = MessageHelper.GetMissingParameterErrorMessage(parameterName) }); } if (string.IsNullOrWhiteSpace(createRestaurantApiModel.City)) { parameterName = "createRestaurantApiModel.City"; return(new ResponseBllModel { Result = ResultEnum.MissingParameter, ParameterName = parameterName, ErrorMessage = MessageHelper.GetMissingParameterErrorMessage(parameterName) }); } if (string.IsNullOrWhiteSpace(createRestaurantApiModel.PostalCode)) { parameterName = "createRestaurantApiModel.PostalCode"; return(new ResponseBllModel { Result = ResultEnum.MissingParameter, ParameterName = parameterName, ErrorMessage = MessageHelper.GetMissingParameterErrorMessage(parameterName) }); } if (string.IsNullOrWhiteSpace(createRestaurantApiModel.Country)) { parameterName = "createRestaurantApiModel.Country"; return(new ResponseBllModel { Result = ResultEnum.MissingParameter, ParameterName = parameterName, ErrorMessage = MessageHelper.GetMissingParameterErrorMessage(parameterName) }); } //Now if the following 2 parameters are Null or whitespace, we then always store string.Empty instead (NOT NULL DB columns) if (string.IsNullOrWhiteSpace(createRestaurantApiModel.AddressLine2)) { createRestaurantApiModel.AddressLine2 = string.Empty; } if (string.IsNullOrWhiteSpace(createRestaurantApiModel.StateProvince)) { createRestaurantApiModel.StateProvince = string.Empty; } using (var dbContext = new RestaurantReviewsDbEntities()) { //Validation check for a restaurant with duplicate address. var duplicateRestaurant = (from r in dbContext.Restaurants where r.AddressLine1 == createRestaurantApiModel.AddressLine1 && r.AddressLine2 == createRestaurantApiModel.AddressLine2 && r.City == createRestaurantApiModel.City && r.StateProvince == createRestaurantApiModel.StateProvince && r.PostalCode == createRestaurantApiModel.PostalCode && r.Country == createRestaurantApiModel.Country select new { r.SystemId, r.Name }).FirstOrDefault(); if (duplicateRestaurant != null) { return(new ResponseBllModel { Result = ResultEnum.DuplicateRestaurantAddress, ErrorMessage = string.Format("This address is already used by this restaurant: RestaurantApiId: {0} Name:'{1}'", duplicateRestaurant.SystemId, duplicateRestaurant.Name) }); } Restaurant newRestaurant = ModelMapper.GetEntity(createRestaurantApiModel); dbContext.Restaurants.Add(newRestaurant); dbContext.SaveChanges(); newRestaurantApiId = newRestaurant.SystemId; } return(new ResponseBllModel { Result = ResultEnum.Success }); } catch (Exception ex) { return(GetUnexpectedErrorResponseBllModel(ex)); } }