// POST api/Country public HttpResponseMessage PostCountry( CountrySmallModel countryModel, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMessage = PerformOperationAndHandleExceptions( () => { PlacesContext context = new PlacesContext(); var country = new Country() { Name = countryModel.Name, Id = countryModel.Id }; if (!context.Countries.Any(c => c.Name.ToLower() == countryModel.Name.ToLower())) { context.Countries.Add(country); context.SaveChanges(); } countryModel.Id = country.Id; return this.Request.CreateResponse(HttpStatusCode.Created, countryModel); }); return responseMessage; }
public HttpResponseMessage PostTown(TownModel model, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new PlacesContext(); using (context) { Town townEntity = new Town(); CountrySmallModel countryModel = new CountrySmallModel(); var countryEntity = context.Countries.Where(c => c.Id == model.CountryId).FirstOrDefault(); if (countryEntity != null) { countryModel.Id = countryEntity.Id; countryModel.Name = countryEntity.Name; townEntity.Country = countryEntity; } else { throw new ArgumentException("No town with this id was found"); } townEntity.Name = model.Name; context.Towns.Add(townEntity); context.SaveChanges(); // get the id from the entity added in the db model.Id = townEntity.Id; return this.Request.CreateResponse(HttpStatusCode.Created, model); } } ); return responseMsg; }