Ejemplo n.º 1
0
        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));
            }
        }
Ejemplo n.º 2
0
        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));
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 7
0
        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));
        }
Ejemplo n.º 8
0
        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)));
        }