Beispiel #1
0
        public async Task <IActionResult> CreateCountry([FromBody] CountryDetailsDto countryResource)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (_countryRepo.CheckCountryCode(countryResource.Code1, countryResource.Code1))
                {
                    ModelState.AddModelError("Error", "The Country with this Code already exists.");
                    return(BadRequest(ModelState));
                }

                var country = _mapper.Map <CountryDetailsDto, Country>(countryResource);
                country.Deleted   = false;
                country.Protected = false;
                _countryRepo.Add(country);
                await _unitOfWork.CompleteAsync();

                country = await _countryRepo.GetCountry(country.Id);

                var result = _mapper.Map <Country, CountryDetailsDto>(country);
                return(Ok(result));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Error", "An error occured while performing this operation.");
                return(BadRequest(ModelState));
            }
        }
Beispiel #2
0
        public async Task <CountryDetailsModel> GetCountry(int countryId)
        {
            CountryDetailsDto countryDetailsDto = await _countryService.GetCountryAsync(countryId);

            CountryDetailsModel countryDetailsModel = _mapper.Map <CountryDetailsModel>(countryDetailsDto);

            return(countryDetailsModel);
        }
Beispiel #3
0
        public async Task <CountryDetailsDto> GetCountryAsync(int countryId)
        {
            CountryDetailsDto countryDetailsDto = await _unitOfWork.Repository <Country>().Entities.Where(d => d.CountryId == countryId)
                                                  .Select(d => new CountryDetailsDto
            {
                CountryId         = d.CountryId,
                CountryName       = d.CountryName,
                IsActive          = d.IsActive,
                CreatedAtUtc      = d.CreatedAtUtc,
                LastModifiedAtUtc = d.LastModifiedAtUtc
            }).FirstOrDefaultAsync();

            return(countryDetailsDto);
        }
Beispiel #4
0
        public async Task <IActionResult> EditCountry([FromRoute] Guid id, [FromBody] CountryDetailsDto countryResource)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                var country = await _countryRepo.GetCountry(id);

                if (country == null)
                {
                    return(NotFound());
                }

                if (country.Protected == true)
                {
                    ModelState.AddModelError("Error", "This record is a protected record. It can not be edited");
                    return(BadRequest(ModelState));
                }

                countryResource.Code1 = country.Code1;
                _mapper.Map <CountryDetailsDto, Country>(countryResource, country);
                await _unitOfWork.CompleteAsync();

                country = await _countryRepo.GetCountry(id);

                var result = _mapper.Map <Country, CountryDetailsDto>(country);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Error", "An error occured while performing this operation.");
                return(BadRequest(ModelState));
            }
        }