Beispiel #1
0
        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));
            }
        }
Beispiel #2
0
 public static Site ToDomain(this SiteUpsertDto dto)
 {
     return(new Site
     {
         Title = dto.Title,
         Description = dto.Description
     });
 }
Beispiel #3
0
        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;
        }
Beispiel #4
0
        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));
            }
        }