public async Task <ActionResult <TopicViewModel> > CreateTopic(
     SaveTopicViewModel topic)
 {
     return(await _topicService
            .CreateTopic(topic)
            .HandleFailuresOrOk());
 }
        public async Task <Either <ActionResult, TopicViewModel> > UpdateTopic(
            Guid topicId,
            SaveTopicViewModel updatedTopic)
        {
            return(await _userService.CheckCanManageAllTaxonomy()
                   .OnSuccess(() => _persistenceHelper.CheckEntityExists <Topic>(topicId))
                   .OnSuccessDo(() => ValidateSelectedTheme(updatedTopic.ThemeId))
                   .OnSuccess(
                       async topic =>
            {
                if (_contentContext.Topics.Any(
                        t => t.Slug == updatedTopic.Slug &&
                        t.Id != topicId &&
                        t.ThemeId == updatedTopic.ThemeId
                        ))
                {
                    return ValidationActionResult(ValidationErrorMessages.SlugNotUnique);
                }

                topic.Title = updatedTopic.Title;
                topic.Slug = updatedTopic.Slug;
                topic.ThemeId = updatedTopic.ThemeId;

                _contentContext.Topics.Update(topic);
                await _contentContext.SaveChangesAsync();

                await _publishingService.TaxonomyChanged();

                return await GetTopic(topic.Id);
            }
                       ));
        }
        public async Task <Either <ActionResult, TopicViewModel> > CreateTopic(SaveTopicViewModel createdTopic)
        {
            return(await _userService.CheckCanManageAllTaxonomy()
                   .OnSuccessDo(() => ValidateSelectedTheme(createdTopic.ThemeId))
                   .OnSuccess(
                       async _ =>
            {
                if (_contentContext.Topics.Any(
                        topic => topic.Slug == createdTopic.Slug &&
                        topic.ThemeId == createdTopic.ThemeId
                        ))
                {
                    return ValidationActionResult(ValidationErrorMessages.SlugNotUnique);
                }

                var saved = await _contentContext.Topics.AddAsync(
                    new Topic
                {
                    Title = createdTopic.Title,
                    Slug = createdTopic.Slug,
                    ThemeId = createdTopic.ThemeId,
                }
                    );

                await _contentContext.SaveChangesAsync();

                await _publishingService.TaxonomyChanged();

                return await GetTopic(saved.Entity.Id);
            }
                       ));
        }
 public async Task <ActionResult <TopicViewModel> > UpdateTopic(
     Guid topicId,
     SaveTopicViewModel topic)
 {
     return(await _topicService
            .UpdateTopic(topicId, topic)
            .HandleFailuresOrOk());
 }
Ejemplo n.º 5
0
        public async void CreateTopic_Returns_Ok()
        {
            var request = new SaveTopicViewModel
            {
                Title   = "Test topic",
                ThemeId = Guid.NewGuid()
            };

            var viewModel = new TopicViewModel
            {
                Id = Guid.NewGuid()
            };

            var topicService = new Mock <ITopicService>();

            topicService.Setup(s => s.CreateTopic(request)).ReturnsAsync(viewModel);

            var controller = new TopicController(topicService.Object);

            var result = await controller.CreateTopic(request);

            Assert.Equal(viewModel, result.Value);
        }