public async Task <IActionResult> CreateThread(CreateThreadRequest request) { // Returns error if either parameter is null if (request.Title == null || request.Content == null) { return(BadRequest("Title and content cannot be null")); } // Creates and adds thread to database User user = await _repository.GetUserAsync(User); Thread thread = new Thread { Title = request.Title, Content = request.Content, DatePosted = DateTime.Now, User = user }; await _repository.AddThreadAsync(thread); await _repository.SaveChangesAsync(); // Returns JSON response return(Json(new ApiThread(thread))); }
private async Task CreateThread(string fragment, CreateThreadRequest req, long?customerId = null) { object data = null; if (customerId != null) { data = new { req.Text, req.Attachments, Customer = new { Id = customerId } } } ; else { data = new { req.Text, req.Attachments } }; var response = await RequestSingle <object>($"conversations/{conversationId}/{fragment}", data, HttpMethod.Post).ConfigureAwait(false); response.WithValidation(); }
public static CreateThreadDto Map(this CreateThreadRequest request, string username) => new CreateThreadDto { Title = request.Title, Content = request.Content, SectionName = request.SectionName, AuthorUsername = username };
public static Thread FromCreateRequest(CreateThreadRequest request) { return(new Thread { Title = request.Title, Body = request.Body, CategoryId = request.CategoryId, }); }
public object Post(CreateThreadRequest request) { var thread = request.ConvertTo <ForumThread>(); var threadId = Db.Insert(thread, true); return(Post(new CreatePostRequest { ForumThreadId = (int)threadId, Text = request.Text })); }
public async Task WithEmptyTitle_ShouldFail() { // Arrange var title = ""; var content = "some content"; var tagsIds = new [] { tag.Id }; var req = new CreateThreadRequest(userActiveAuth.Id, tagsIds, title, content); var createThreadUseCase = ServiceProvider.GetService <CreateThreadUseCase>(); // Act var res = await createThreadUseCase.Handle(req); // Assert Assert.Single(res.ValidationErrors); Assert.Contains(CreateThreadError.TitleIsEmpty, res.ValidationErrors); }
public async Task WithInexistentTag_ShouldFail() { // Arrange var title = "some title"; var content = "some content"; var unknownTags = new [] { Guid.NewGuid() }; var req = new CreateThreadRequest(userActiveAuth.Id, unknownTags, title, content); var createThreadUseCase = ServiceProvider.GetService <CreateThreadUseCase>(); // Act var res = await createThreadUseCase.Handle(req); // Assert Assert.Single(res.ValidationErrors); Assert.Contains(CreateThreadError.TagDoesNotExist, res.ValidationErrors); }
public async Task WithEmptyContent_ShouldWork() { // Arrange var title = "title"; var content = ""; var tagsIds = new [] { tag.Id }; var req = new CreateThreadRequest(userActiveAuth.Id, tagsIds, title, content); var createThreadUseCase = ServiceProvider.GetService <CreateThreadUseCase>(); // Act var res = await createThreadUseCase.Handle(req); // Assert Assert.Empty(res.ValidationErrors); Assert.NotEqual(Guid.Empty, res.ThreadId); }
public async Task WithInexistentAuth_ShouldFail() { // Arrange var authId = Guid.NewGuid(); var title = "some title"; var content = "some content"; var tagsIds = new [] { tag.Id }; var req = new CreateThreadRequest(authId, tagsIds, title, content); var createThreadUseCase = ServiceProvider.GetService <CreateThreadUseCase>(); // Act var res = await createThreadUseCase.Handle(req); // Assert Assert.Single(res.ValidationErrors); Assert.Contains(CreateThreadError.AuthenticationIsInvalid, res.ValidationErrors); }
public async Task <IActionResult> Create([FromBody] CreateThreadRequest threadRequest) { int id = int.Parse(HttpContext.User.Claims.FirstOrDefault(claim => claim.Type == "Id").Value); if (_categoryRepository.Find(threadRequest.CategoryId) != null) { Thread thread = ConvertFromDTO.FromCreateRequest(threadRequest); thread.UserId = id; _threadRepository.Create(thread); await _context.SaveChangesAsync(); return(CreatedAtAction(nameof(Get), new { id = thread.Id }, ConvertToDTO.ToCreateResponse(thread))); } else { return(NotFound()); } }
public async Task WithExpiredAuth_ShouldFail() { // Arrange var auth = new UserAuthentication(userActive, TimeSpan.FromSeconds(-1)); var authCreator = ServiceProvider.GetService <ICreateAuthenticationRepository>(); await authCreator.Create(auth); var title = "some title"; var content = "some content"; var tagsIds = new [] { tag.Id }; var req = new CreateThreadRequest(auth.Id, tagsIds, title, content); var createThreadUseCase = ServiceProvider.GetService <CreateThreadUseCase>(); // Act var res = await createThreadUseCase.Handle(req); // Assert Assert.Single(res.ValidationErrors); Assert.Contains(CreateThreadError.AuthenticationIsInvalid, res.ValidationErrors); }
public async Task WithValidData_ShouldWork() { // Arrange var title = "title"; var content = "some content"; var tagsIds = new [] { tag.Id }; var req = new CreateThreadRequest(userActiveAuth.Id, tagsIds, title, content); var createThreadUseCase = ServiceProvider.GetService <CreateThreadUseCase>(); var threadRetriever = ServiceProvider.GetService <IRetrieveThreadRepository>(); // Act var res = await createThreadUseCase.Handle(req); // Assert Assert.Empty(res.ValidationErrors); Assert.NotEqual(Guid.Empty, res.ThreadId); var thread = await threadRetriever.Retrieve(res.ThreadId); Assert.Equal(userActive.Id, thread.AuthorId); Assert.Equal(tagsIds, thread.TagsIds); }
public async Task <IActionResult> CreateThread([FromBody, Required] CreateThreadRequest request) { _logger.LogInformation($"Creating thread for user with username {User.Identity.Name}"); var createThreadResult = await _threadsManagementService.CreateThread(request.Map(User.Identity.Name)); if (createThreadResult.IsSuccess) { _logger.LogInformation($"Created thread for user with username {User.Identity.Name}"); return(Ok(new ThreadInfoResult(createThreadResult.Value))); } var statusCodeResult = GetErrorResult(createThreadResult.ErrorType); _logger.LogWarning(statusCodeResult.StatusCode.HasValue ? $"Failed thread creation for user with username {User.Identity.Name}; Status code - {statusCodeResult.StatusCode.Value}, reason - {statusCodeResult.Value}" : $"Failed thread creation for user with username {User.Identity.Name}; Reason - {statusCodeResult.Value}"); return(statusCodeResult); }
public async Task WithLongTitle_ShouldFail() { // Arrange var title = @"Vou te contar uma historinha sobre posse de bola. Teve um cara que pegou uma mulher bonita e levou ela para jantar. Levou para jantar a luz de velas, conversou bastante. Saiu do restaurante, foi na boate e ficou até às 5h da manhã com ela. Gastou uma saliva monstruosa. Aí, na boate, chegou um amigo meu, conversou com ela 15 minutos e levou ela para o motel. Entendeu? Se não entendeu outra hora eu explico. Meu amigo ganhou o jogo. Feliz Ano Novo"; var content = ""; var tagsIds = new [] { tag.Id }; var req = new CreateThreadRequest(userActiveAuth.Id, tagsIds, title, content); var createThreadUseCase = ServiceProvider.GetService <CreateThreadUseCase>(); // Act var res = await createThreadUseCase.Handle(req); // Assert Assert.Single(res.ValidationErrors); Assert.Contains(CreateThreadError.TitleIsTooLong, res.ValidationErrors); }
public async Task Should_be_able_to_create_various_threads_and_verify() { var req = Setup.ConversationCreateRequest(); var conversationId = await conversationEndpoint.Create(req); var threadEndpoint = conversationEndpoint.Endpoints.Threads(conversationId); var customer = Setup.CreateCustomerRequest(); var customerId = await Client.Customers.Create(customer); var thread = new CreateThreadRequest { Text = Faker.Rant.Review("iphone") }; await threadEndpoint.CreateCustomerThread(thread, customerId); thread.Text = Faker.Rant.Review(); await threadEndpoint.CreateChatThread(thread, customerId); thread.Text = Faker.Rant.Review(); await threadEndpoint.CreateNoteThread(thread); thread.Text = Faker.Rant.Review(); await threadEndpoint.CreatePhoneThread(thread, customerId); thread.Text = "Look I have attachment!"; thread.Attachments = new List <Attachment> { await GetAttachment() }; await threadEndpoint.CreateReplyThread(thread, customerId); //verify var threadList = await threadEndpoint.List(); threadList.Items.Should().HaveCountGreaterOrEqualTo(2); }
public async Task <Response> CreateThread(CreateThreadRequest request) { var apiHandler = GetAPIhandlerForPost <CreateThreadRequest, Empty>(APIEndPoints.CreateNewThread, request); return(await apiHandler.Execute()); }
public Task <IHttpResponse <Thread> > CreateThread(CreateThreadRequest thread) { return(this.HttpClient.Post <Thread, CreateThreadRequest>(ResourceUri, thread)); }
public async Task CreateReplyThread(CreateThreadRequest req, long customerId) { await CreateThread("reply", req, customerId).ConfigureAwait(false); }
public async Task CreatePhoneThread(CreateThreadRequest req, long customerId) { await CreateThread("phones", req, customerId).ConfigureAwait(false); }
public async Task CreateNoteThread(CreateThreadRequest req) { await CreateThread("notes", req).ConfigureAwait(false); }