public async Task <Post> Create(CreatePostDto dto) { User user = await _sessionService.GetUser(); Validate("create", dto.Content, dto.Title, user); var post = new Post { Title = dto.Title, Content = dto.Content, CreationTime = DateTime.Now, LastUpdateTime = DateTime.Now, AuthorId = user.Id, Likes = 0, Dislikes = 0, Tags = TagHelpers.GetTagsFromText(dto.Content), Id = Guid.NewGuid().ToString() }; await _postRepository.Create(post); _logger.LogInformation($"Post {post.Id} has been created"); await _tagRepository.Create(post.Tags.Select(x => new Tag { Name = x, PostsNumber = 1 }).ToArray()); _logger.LogInformation("Tags have been added"); return(post); }
public async Task Update(string id, UpdatePostDto dto) { User user = await _sessionService.GetUser(); Validate("modify", dto.Content, dto.Title, user); Post post = await _postRepository.GetById(id); if (post == null) { _logger.LogWarning($"Post {id} does not exist"); throw HttpError.NotFound($"Post {id} does not exist"); } if (post.AuthorId != user.Id) { _logger.LogWarning($"Post {id} does not belong to user"); throw HttpError.Forbidden($"Post {id} does not belong to user"); } post.Title = dto.Title; post.Content = dto.Content; post.LastUpdateTime = DateTime.Now; post.Tags = TagHelpers.GetTagsFromText(dto.Content); bool success = await _postRepository.Update(post); if (!success) { _logger.LogWarning("Error during update post"); throw HttpError.InternalServerError(""); } await _tagRepository.Create(post.Tags.Select(x => new Tag { Name = x, PostsNumber = 1 }).ToArray()); _logger.LogInformation("Tags have been added"); }