public async Task <CreateThreadResponse> Handle(CreateThreadRequest request) { if (string.IsNullOrWhiteSpace(request.Title)) { return(new CreateThreadResponse(CreateThreadError.TitleIsEmpty)); } if (request.Title.Length > TITLE_MAX_LENGTH) { return(new CreateThreadResponse(CreateThreadError.TitleIsTooLong)); } var auth = await authRetriever.Retrieve(request.AuthId); if (auth == null || auth.IsExpired) { return(new CreateThreadResponse(CreateThreadError.AuthenticationIsInvalid)); } var author = await userRetriever.RetrieveById(auth.UserId); if (!author.Active) { return(new CreateThreadResponse(CreateThreadError.UserIsInactive)); } var tags = new List <Tag>(); foreach (var tagId in request.TagsIds) { var tag = await tagRetriever.Retrieve(tagId); if (tag == null) { return(new CreateThreadResponse(CreateThreadError.TagDoesNotExist)); } tags.Add(tag); } var thread = new Thread(author, tags, request.Title, request.Content); await threadCreator.Create(thread); return(new CreateThreadResponse(thread.Id)); }
public async Task <GetThreadsListResponse> Handle(GetThreadsListRequest request) { var canSeeInvisibleThreads = false; if (request.AuthId != null) { var auth = await authRetriever.Retrieve(request.AuthId.Value); if (auth == null || auth.IsExpired) { return(new GetThreadsListResponse(GetThreadsListError.AuthenticationIsInvalid)); } var user = await userRetriever.RetrieveById(auth.UserId); if (!user.Active) { return(new GetThreadsListResponse(GetThreadsListError.UserIsInactive)); } if (user.Level > 0) { canSeeInvisibleThreads = true; } } var offset = PAGE_LENGTH * request.Page; var count = PAGE_LENGTH; var threads = await threadRetriever.RetrieveSortedByLastPost(offset, count, canSeeInvisibleThreads); var threadsDTO = new List <GetThreadsListResponse.ThreadDTO>(); foreach (var thread in threads) { var author = await userRetriever.RetrieveById(thread.AuthorId); var commentsCount = await commentRetriever.Count(thread.Id); var mostRecentComment = await commentRetriever.RetrieveMostRecent(thread.Id); var mostRecentCommentAuthor = await userRetriever.RetrieveById(mostRecentComment.AuthorId); var tagNames = new List <string>(); foreach (var tagId in thread.TagsIds) { var tag = await tagRetriever.Retrieve(tagId); tagNames.Add(tag.Name); } var dto = new GetThreadsListResponse.ThreadDTO( thread.Id, thread.Title, thread.Visible, tagNames, author.Name, commentsCount, mostRecentComment.CreatedAt, mostRecentCommentAuthor.Name); threadsDTO.Add(dto); } return(new GetThreadsListResponse(threadsDTO)); }