public IActionResult PutCountry(int continentId, int countryId, [FromBody] CountryInApi countryInApi) { logger.LogInformation($"Post api/continent/{continentId}/country/{countryId} called"); if (countryInApi == null) { return(BadRequest()); } try { if (countryManager.Find(countryId) == null) { Domain.Models.Country country = CountryMapper.CountryInMapper(continentManager, countryInApi); Domain.Models.Country countryCreated = countryManager.AddCountry(country); CountryOutApi countryOut = CountryMapper.CountryOutMapper(hostUrl, countryCreated); return(CreatedAtAction(nameof(GetCountry), new { continentId = countryCreated.Continent.Id, countryId = countryCreated.Id }, countryOut)); } Domain.Models.Country countryUpdate = CountryMapper.CountryInMapper(continentManager, countryInApi); countryManager.UpdateCountry(countryId, countryUpdate, countryUpdate.Continent.Id); return(new NoContentResult()); } catch (Exception ex) { logger.LogError(ex.Message); return(BadRequest(ex.Message)); } }
public ActionResult <CountryInApi> PostCountry(int continentId, [FromBody] CountryInApi countryInApi) { logger.LogInformation($"Post api/continent/{continentId}/country/ called"); try { Domain.Models.Country country = CountryMapper.CountryInMapper(continentManager, countryInApi); Domain.Models.Country countryCreated = countryManager.AddCountry(country); if (countryCreated != null) { CountryOutApi countryOut = CountryMapper.CountryOutMapper(hostUrl, countryCreated); return(CreatedAtAction(nameof(GetCountry), new { continentId = countryCreated.Continent.Id, countryId = countryCreated.Id }, countryOut)); } else { logger.LogError("Country can not be null."); return(NotFound("Country can not be null.")); } } catch (Exception ex) { logger.LogError(ex.Message); return(NotFound(ex.Message)); } }
public static CountryOutApi CountryOutMapper(string hostUrl, Country country) { CountryOutApi countryOut = new CountryOutApi(); countryOut.Id = hostUrl + "/api/continent/" + country.Continent.Id + "/country/" + country.Id; countryOut.Name = country.Name; countryOut.Population = country.Population; countryOut.Surface = country.Surface; countryOut.Continent = hostUrl + "/api/continent/" + country.Continent.Id; return(countryOut); }
public ActionResult <CountryOutApi> GetCountry(int continentId, int countryId) { logger.LogInformation(countryId, $"Get api/continent/{continentId}/country/{countryId} called"); try { if (continentManager.Find(continentId) != null && countryManager.Find(countryId) != null && countryManager.Find(countryId).Continent.Id == continentId) { Domain.Models.Country country = countryManager.Find(continentId, countryId); CountryOutApi countryOut = CountryMapper.CountryOutMapper(hostUrl, country); return(Ok(countryOut)); } else { logger.LogError($"Country with id{countryId} is not found"); return(NotFound($"Country with id{countryId} is not found")); } } catch (Exception ex) { logger.LogError(ex.Message); return(NotFound(ex.Message)); } }