public IActionResult PutContinent(int id, [FromBody] ContinentInApi continentIn) { logger.LogInformation($"Put api/continent/ called"); if (continentIn == null) { return(BadRequest()); } try { if (continentManager.Find(id) == null) { Domain.Models.Continent continent = ContinentMapper.ContinentInMapper(countryManager, continentIn); Domain.Models.Continent continentCreated = continentManager.AddContinent(continent); ContinentOutApi continentOut = ContinentMapper.ContinentOutMapper(hostUrl, continentCreated); return(CreatedAtAction(nameof(GetContinent), new { id = continentOut.Id }, continentOut)); } Domain.Models.Continent continentUpdate = ContinentMapper.ContinentInMapper(countryManager, continentIn); continentUpdate.Id = id; continentManager.UpdateContinent(id, continentUpdate); return(new NoContentResult()); } catch (Exception ex) { logger.LogError(ex.Message); return(BadRequest(ex.Message)); } }
public ActionResult <ContinentInApi> PostContinent([FromBody] ContinentInApi continentAPI) { logger.LogInformation($"Post api/continent/ called"); try { if (continentAPI == null) { return(BadRequest()); } Domain.Models.Continent continent = ContinentMapper.ContinentInMapper(countryManager, continentAPI); Domain.Models.Continent continentCreated = continentManager.AddContinent(continent); if (continentCreated != null) { ContinentOutApi continentOut = ContinentMapper.ContinentOutMapper(hostUrl, continentCreated); return(CreatedAtAction(nameof(GetContinent), new { id = continentOut.Id }, continentOut)); } else { logger.LogError("Continent can not be null."); return(NotFound("Continent can not be null.")); } } catch (Exception ex) { logger.LogError(ex.Message); return(NotFound(ex.Message)); } }
public static ContinentOutApi ContinentOutMapper(string hostUrl, Continent continent) { ContinentOutApi continentOut = new ContinentOutApi(); continentOut.Id = hostUrl + "/api/continent/" + continent.Id; continentOut.Name = continent.Name; continentOut.Population = continent.Population(); if (continent.Countries != null) { foreach (Country country in continent.Countries) { string url = hostUrl + "/api/continent/" + continent.Id + "/country/" + country.Id; continentOut.Countries.Add(url); } } return(continentOut); }
public ActionResult <ContinentOutApi> GetContinent(int id) { logger.LogInformation(id, $"Get api/continent/{id} called"); try { Domain.Models.Continent continent = continentManager.Find(id); if (continent != null) { ContinentOutApi continentOut = ContinentMapper.ContinentOutMapper(hostUrl, continent); return(Ok(continentOut)); } else { logger.LogError($"Continent with {id} is not found"); return(NotFound($"Continent with {id} is not found")); } } catch (Exception ex) { logger.LogError(ex.Message); return(NotFound(ex.Message)); } }