Beispiel #1
0
        public static Country ToCountry(this CountryRequestModel requestModel, Country source)
        {
            source.Name    = requestModel.Name;
            source.ISOCode = requestModel.ISOCode;

            return(source);
        }
        // POST api/countries
        public HttpResponseMessage PostCountry(CountryRequestModel requestModel) {

            Country country = _mapper.Map<CountryRequestModel, Country>(requestModel);
            country.CreatedOn = DateTimeOffset.Now;

            _countryRepository.Add(country);
            _countryRepository.Save();

            CountryDto countryDto = _mapper.Map<Country, CountryDto>(country);
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, countryDto);
            response.Headers.Location = GetCountryLink(country.Id);

            return response;
        }
        // PUT api/countries/1
        public CountryDto PutCountry(int id, CountryRequestModel requestModel) {

            Country country = _countryRepository.GetSingle(id);
            if (country == null) {

                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            // NOTE: AutoMapper goes bananas over this as 
            // EF creates the poroxy class for Country
            // Country updatedCountry = Mapper.Map<CountryRequestModel, Country>(requestModel, country);

            Country updatedCountry = requestModel.ToCountry(country);
            _countryRepository.Edit(updatedCountry);
            _countryRepository.Save();

            CountryDto countryDto = _mapper.Map<Country, CountryDto>(country);
            return countryDto;
        }