Esempio n. 1
0
        public IActionResult GetAuthorsFromACountry(int countryId)

        {
            if (!_countryRepository.CountryExist(countryId))
            {
                return(NotFound());
            }
            var authors = _countryRepository.GetAuthorsFromACountry(countryId);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            var authorDto = new List <AuthorDto>();

            foreach (var author in authors)
            {
                authorDto.Add(new AuthorDto()
                {
                    id        = author.Id,
                    FirstName = author.FirstName,
                    LastName  = author.LastName
                });
            }

            return(Ok(authorDto));
        }
Esempio n. 2
0
        public IActionResult CreateAuthor([FromBody] Author authorToCreate)
        {
            if (authorToCreate == null)
            {
                return(BadRequest(ModelState));
            }

            if (!_countryRepository.CountryExist(authorToCreate.Country.Id))
            {
                ModelState.AddModelError("", $"Sorry Country doesnot exist.  does not exist.");
                return(StatusCode(404, ModelState));
            }


            authorToCreate.Country = _countryRepository.GetCountry(authorToCreate.Country.Id);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            if (!_authorRepository.CreateAuthor(authorToCreate))
            {
                ModelState.AddModelError("", $"Something went wrong creating {authorToCreate.FirstName}{authorToCreate.LastName}.");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetAuthor", new { authorId = authorToCreate.Id }, authorToCreate));
        }
Esempio n. 3
0
        public IActionResult CreateAuthor([FromBody] Author authorToCreate)
        {
            if (authorToCreate == null)
            {
                return(BadRequest(ModelState));
            }
            //If we look the relationship it is clear that author is linked with country. therefore we need to check
            //the country exist or not for this first add CountryRepository and then check coutry existence

            if (!_countryRepository.CountryExist(authorToCreate.Country.Id))
            {
                ModelState.AddModelError("", $"Country Does not exist");
                return(StatusCode(404, ModelState));
            }
            authorToCreate.Country = _countryRepository.GetCountry(authorToCreate.Country.Id);
            if (!_authorRepository.CreateAuthor(authorToCreate))
            {
                ModelState.AddModelError("", $"something went wrong to save" +
                                         $"{authorToCreate.FirstName}{authorToCreate.LastName}");
                return(StatusCode(500, ModelState));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            return(CreatedAtRoute("GetAuthor", new { authorId = authorToCreate.Id }, authorToCreate));
        }
        public async Task <IActionResult> GetCountry(int countryId)
        {
            if (!await _repo.CountryExist(countryId))
            {
                return(NotFound("This country is not found!"));
            }

            var country = await _repo.GetCountry(countryId);

            var countryToReturn = _mapper.Map <CountriesToGetDto>(country);

            return(Ok(countryToReturn));
        }
        public IActionResult GetCountry(int countryId)
        {
            if (!_countryRepository.CountryExist(countryId))
            {
                return(NotFound());
            }
            var c = _countryRepository.GetCountry(countryId);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var countriesDto = new CountryDto()
            {
                Id = c.CountryId, Name = c.Name
            };

            return(Ok(countriesDto));
        }
Esempio n. 6
0
        public IActionResult GetCountryById(int countryId)

        {
            var country = _countriesRepository.GetCountry(countryId);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                if (!_countriesRepository.CountryExist(countryId))
                {
                    return(NotFound($"the country with the countryId {countryId} could not be found"));
                }
                //if (country == null)
                //{
                //    return NotFound($"the country with the countryId {countryId} could not be found");
                //}
            }


            catch (Exception ex)
            {
                _logger.LogError("an error occured in GetCountryById", ex);

                return(StatusCode(500));
            }
            var countrydto = new CountriesDto()
            {
                Id   = country.Id,
                Name = country.Name
            };

            return(Ok(countrydto));
        }
        public async Task <ActionResult <Response> > CreateCountry(CountryModel model)
        {
            var country = model.ToEntity();

            if (country.Invalid)
            {
                return(BadRequest(ResponseHelper.CreateResponse("Informações inválidas para cadastrar um país", model)));
            }

            if (await _countryRepository.CountryExist(country))
            {
                return(BadRequest(ResponseHelper.CreateResponse("Esse país já foi cadastrado", model)));
            }

            _countryRepository.Add(country);

            return(Ok(ResponseHelper.CreateResponse("País cadastrado com sucesso!", (CountryModel)country)));
        }
Esempio n. 8
0
 private bool CountryExists(int id)
 {
     return(_countryRepository.CountryExist(id));
 }