public IActionResult Post([FromBody] SiteUpsertDto siteDto) { try { if (ModelState.IsValid) { var site = siteDto.ToDomain(); var createdDto = _siteRepository.Insert(site).ToDto(); createdDto.State = SiteState.Undefined; return(CreatedAtAction(nameof(Get), new { id = createdDto.Id }, createdDto)); } else { return(BadRequest()); } } catch (Exception e) { //TODO: Log the exception _logger.LogError($"An error occured while inserting new site with given exception: {e}"); return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public static Site ToDomain(this SiteUpsertDto dto) { return(new Site { Title = dto.Title, Description = dto.Description }); }
public static void Update(this Site site, SiteUpsertDto dto) { if (dto == null) { throw new ArgumentNullException($"Dto object cannot be null"); } if (site == null) { throw new ArgumentNullException($"Domain object cannot be null"); } site.Title = dto.Title; site.Description = dto.Description; }
public IActionResult Put(Guid id, [FromBody] SiteUpsertDto siteDto) { var site = _siteRepository.GetByID(id); if (site == null) { return(ResponseHelpers.NotFoundResponse(ResourceType, id)); } try { site.Update(siteDto); _siteRepository.Update(site); return(Ok()); } catch (Exception e) { _logger.LogError($"An error occured while updating site with id: {id} with given exception: {e}"); return(StatusCode(StatusCodes.Status500InternalServerError)); } }