Beispiel #1
0
        public async Task <object> UpdateAsync(
            [FromBody] WriteNewsItemRequest body)
        {
            var newsItem = await _newsItemRepository.GetAsync(body.Id);

            if (newsItem == null)
            {
                return(NotFound(JResponse.Error("News item not found.")));
            }

            newsItem.Modified = DateTime.UtcNow;
            if (body.Title != null)
            {
                newsItem.Title = body.Title;
            }
            if (body.Html != null)
            {
                newsItem.Html = body.Html;
            }
            if (body.Published == true)
            {
                newsItem.Published = DateTime.UtcNow;
            }
            _logger.LogInformation($"Updating news item: {newsItem.Title} (updated by {User.Identity.Name}");
            await _newsItemRepository.UpdateAsync(newsItem);

            return(JResponse.OK());
        }
Beispiel #2
0
        public async Task <object> DeleteAsync(
            [FromBody] WriteNewsItemRequest body)
        {
            if (body.Id == null)
            {
                return(BadRequest(JResponse.Error("News item id not specified.")));
            }

            _logger.LogInformation($"Deleting news item: {body.Id} (deleted by {User.Identity.Name}");
            await _newsItemRepository.DeleteAsync(body.Id);

            return(JResponse.OK());
        }
Beispiel #3
0
        public async Task <JResponse> CreateAsync(
            [FromBody] WriteNewsItemRequest body)
        {
            var currentUser = User.Identity as AuthenticatedUser;
            var now         = DateTime.UtcNow;
            var newsItem    = new NewsItem()
            {
                Title    = body.Title,
                AuthorId = currentUser.Id,
                Created  = now,
                Modified = now,
                Html     = body.Html
            };

            _logger.LogInformation($"Creating news item: {newsItem.Title} by {currentUser.Name}");
            await _newsItemRepository.UpdateAsync(newsItem);

            return(JResponse.OK());
        }