public async Task <IActionResult> Create(TopicCreateDto model, string forumId) { if (!ModelState.IsValid) { return(View(model)); } var authorId = User.Claims.FirstOrDefault(c => c.Type == "Id").Value; var topic = new Topic { Id = Guid.NewGuid().ToString("N"), Title = model.Title, Description = model.Description, AuthorId = authorId, ForumId = forumId }; try { await topicRepository.CreateAsync(topic); return(RedirectToAction("Show", "Forum", new { id = forumId })); } catch (Exception e) { ViewBag.Error = e.Message; return(View(model)); } }
public ActionResult <TopicCreateDto> CreateTopic(TopicCreateDto topicCreateDto) { var topicModel = _mapper.Map <Topic>(topicCreateDto); _repo.CreateTopic(topicModel); _repo.SaveChanges(); var topicReadDto = _mapper.Map <TopicReadDto>(topicModel); return(CreatedAtRoute(nameof(GetTopicById), new { Id = topicReadDto.TopicID }, topicReadDto)); }
public void CreateValidTopicReturnsCreatedAtRoutePassed() { var validTopic = new TopicCreateDto() { Title = "Test Title", Body = "Test Body", UserId = 1 }; var createdResponse = _controller.CreateTopic(validTopic); Assert.IsType <CreatedAtRouteResult>(createdResponse.Result); }
public void CreateInvalidTopicReturnsBadRequestPassed() { var topicWithoutTitle = new TopicCreateDto() { Body = "test body", UserId = 1 }; _controller.ModelState.AddModelError("Title", "Required"); var badResponse = _controller.CreateTopic(topicWithoutTitle); Assert.IsType <BadRequestObjectResult>(badResponse.Result); }
public void CreateValidTopicReturnsCreatedTopicPassed() { var validTopic = new TopicCreateDto() { Title = "Test Title", Body = "Test Body", UserId = 1 }; var createdResponse = _controller.CreateTopic(validTopic).Result as CreatedAtRouteResult; var topicCreated = createdResponse.Value as TopicReadDto; Assert.IsType <TopicReadDto>(topicCreated); Assert.Equal("Test Title", topicCreated.Title); }
public IActionResult CreateTopic([FromBody] TopicCreateDto createTopic) { if (createTopic == null) { return(BadRequest()); } if (createTopic.Title == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var newTopic = _mapper.Map <TraTopic>(createTopic); string userGuid = UserHelper.GetUserGuid(_httpContextAccessor); var getUser = _transActionRepo.GetUsers().FirstOrDefault(c => c.Guid == userGuid); newTopic.UserId = getUser.UserId; _transActionRepo.CreateTopic(newTopic); if (!_transActionRepo.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } var message = new TraTopicMessage(); message.UserId = newTopic.UserId; message.TopicId = newTopic.TopicId; message.Body = createTopic.Body; _transActionRepo.CreateTopicMessage(message); if (!_transActionRepo.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } var createdPointOfInterestToReturn = _mapper.Map <TopicDto>(newTopic); return(CreatedAtRoute("GetThatTopic", new { id = createdPointOfInterestToReturn.TopicId }, createdPointOfInterestToReturn)); }
public ActionResult <TopicReadDto> CreateTopic(TopicCreateDto topicCreateDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var topicModel = _mapper.Map <Topic>(topicCreateDto); _repository.CreateTopic(topicModel); _repository.SaveChanges(); var topicReadDto = _mapper.Map <TopicReadDto>(topicModel); //TODO: clean that up if (topicReadDto.user.Comments != null) { topicReadDto.user.Comments = null; topicReadDto.user.Topics = null; } return(CreatedAtRoute(nameof(GetTopicById), new { Id = topicReadDto.Id }, topicReadDto)); }
public IActionResult CreateTopic([FromBody] TopicCreateDto createTopic) { if (!ModelState.IsValid) { return(BadRequest(new TransActionResponse(ModelState))); } var newTopic = _mapper.Map <TraTopic>(createTopic); string userGuid = UserHelper.GetUserGuid(_httpContextAccessor); var getUser = _unitOfWork.User.GetByGuid(userGuid); newTopic.UserId = getUser.UserId; newTopic.LastMessageTimestamp = DateTime.Now; _unitOfWork.Topic.Create(newTopic); if (!_unitOfWork.Save()) { return(StatusCode(500, new TransActionResponse("A problem happened while handling your request."))); } var message = new TraTopicMessage(); message.UserId = newTopic.UserId; message.TopicId = newTopic.TopicId; message.Body = createTopic.Body; _unitOfWork.Message.Create(message); if (!_unitOfWork.Save()) { return(StatusCode(500, new TransActionResponse("A problem happened while handling your request."))); } var createTopicResult = _mapper.Map <TopicDto>(newTopic); return(CreatedAtRoute("GetTopic", new { id = createTopicResult.TopicId }, new TransActionResponse(createTopicResult))); }