public async Task <ActionResult <Countries> > AddCountry(AddCountryDto addCountryDto)
        {
            var newcountry = _mapper.Map <Countries>(addCountryDto);

            _projectRepository.Add(newcountry);

            if (await _projectRepository.SaveAllAsync())
            {
                var messageToReturn = _mapper.Map <AddCountryDto>(newcountry);
                return(CreatedAtRoute("GetCountry",
                                      new { countryid = newcountry.Id }, newcountry));
            }
            throw new Exception("Creating the City failed on save");
        }
        public async Task <ActionResult> Put(int id, AddCountryDto addCountryDto)
        {
            var country = await _projectRepository.GetCountryByIdAsync(id);

            // country = _mapper.Map<Countries>(addCountryDto);
            _mapper.Map(addCountryDto, country);
            // await _projectRepository.SaveAllAsync();

            if (await _projectRepository.SaveAllAsync())
            {
                return(NoContent());
            }

            throw new Exception($"Updating user {id} failed on save");
        }
Пример #3
0
        public async Task <ActionResult> AddCountry([FromBody] AddCountryDto dto, CancellationToken token = default)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.Values.SelectMany(c => c.Errors)));
            }

            var country = new Country {
                Name = dto.Name, Iso = dto.Iso
            };

            Context.Countries.Add(country);
            await Context.SaveChangesAsync(token);

            return(Ok(country));
        }