Пример #1
0
        public async Task <ActionResult <Topic> > PostTopic(AddTopicDto topic)
        {
            var alreadyExists = _context.Topics.Any(t => t.Name == topic.Name);

            if (alreadyExists)
            {
                return(BadRequest("This topic already exists"));
            }

            var newTopic = new Topic()
            {
                TopicId   = Guid.NewGuid().ToString(),
                Name      = topic.Name,
                CreatedAt = DateTime.Now,
                UpdatedAt = DateTime.Now
            };

            _context.Topics.Add(newTopic);

            var topicDto = new TopicDto()
            {
                TopicId = newTopic.TopicId,
                Name    = newTopic.Name
            };

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (TopicExists(newTopic.TopicId))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetTopic", topicDto));
        }
Пример #2
0
        public IActionResult CreateTopic(AddTopicDto addTopicDto)
        {
            var result = this._topicService.CreateTopic(addTopicDto);



            if (result.ResultType == ResultType.UnAuthorized)
            {
                return(Unauthorized());
            }


            if (result.ResultType == ResultType.Success)
            {
                return(Ok(result.Message));
            }


            return(BadRequest(result.Message));
        }
        public IResult CreateTopic(AddTopicDto addTopicDto)
        {
            var errorResult = BusinessRules.Run(CheckAuthenticatedUserExist());

            if (errorResult != null)
            {
                return(errorResult);
            }

            var user = _authService.GetAuthenticatedUser().Result.Data;

            var topic = new Topic()
            {
                Title = addTopicDto.Title, Content = addTopicDto.Content, UserId = user.Id
            };

            _uow.Topics.Add(topic);
            _uow.Commit();

            return(new SuccessResult(Message.TopicCreated));
        }