public IActionResponse <Domain.Models.Country> Delete(Domain.Models.Country entity)
        {
            var returnValue = new ActionResponse <Domain.Models.Country>
            {
                Success = true,
                Data    = entity
            };
            var existing = _context.Countries.Where(c => (entity.Id > 0 && c.Id.Equals(entity.Id)) ||
                                                    c.EnglishName.Equals(entity.EnglishName, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();

            if (existing?.Id > 0)
            {
                //Delete
                _context.Countries.Remove(existing);
                returnValue.Data = existing;
                return(returnValue);
            }
            else
            {
                //Item not found or already deleted
                //return true with warning
                returnValue.WarningMessages.Add($"Country Not Found");
                return(returnValue);
            }
        }
        public IActionResponse <Domain.Models.Country> Persist(Domain.Models.Country entity)
        {
            var returnValue = new ActionResponse <Domain.Models.Country>
            {
                Success = true,
                Data    = entity
            };
            var existing = _context.Countries.Where(c => (entity.Id > 0 && c.Id.Equals(entity.Id)) ||
                                                    c.EnglishName.Equals(entity.EnglishName, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();

            if (existing?.Id > 0)
            {
                //Update
                entity.Id = existing.Id;
                existing.Merge(entity);
                _context.Countries.Update(existing);
                returnValue.Data = existing;
                return(returnValue);
            }

            if (entity.Id > 0)
            {
                //Invalid Id
                returnValue.Success = false;
                returnValue.ErrorMessages.Add("Country with this Id Not Found");
                return(returnValue);
            }
            //Insert
            _context.Countries.Add(entity);
            return(returnValue);
        }
 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 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));
     }
 }