Beispiel #1
0
        public ConceptControllerTest()
        {
            _tokenHelper = A.Fake <ITokenHelper>();
            A.CallTo(() => _tokenHelper.ReturnScope(A <ClaimsPrincipal> ._)).Returns(_allowedScope);
            A.CallTo(() => _tokenHelper.GetUserInfo()).Returns(new UserInfo());

            _service       = A.Fake <IConceptService>();
            _controller    = new ConceptController(_service, _tokenHelper);
            _createConcept = new CreateConceptDto
            {
                Title   = "Title",
                Content = "Content",
                Created = DateTime.Now,
                Updated = DateTime.Now,
            };
            _updateConcept = new UpdateConceptDto
            {
                Title   = "Title",
                Content = "Content",
                Created = DateTime.Now,
                Updated = DateTime.Now,
            };

            _searchQuery = new ConceptSearchQuery {
                Title = "title", MetaIds = new List <int> {
                    1, 2
                }
            };

            _errorResponse = new Response
            {
                Errors = new ModelStateDictionary()
            };
            _errorResponse.Errors.TryAddModelError("err", "err");

            _listResponse = new Response {
                Data = new List <ConceptDto>()
            };
            _singleResponse = new Response {
                Data = new ConceptDto()
            };
            _userInfo = new UserInfo
            {
                Email    = _allowedUserEmail,
                FullName = "Name"
            };

            _listQuery = BaseListQuery.DefaultValues(language);
        }
Beispiel #2
0
        public async Task <ActionResult <Response> > CreateConcept([Required][FromBody] CreateConceptDto concept)
        {
            if (concept == null)
            {
                var errors = new ModelStateDictionary();
                errors.TryAddModelError("errorMessage", "Concept is required");
                return(BadRequest(new ModelStateErrorResponse(errors)));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(new ModelStateErrorResponse(ModelState)));
            }

            UserInfo userInfo;

            try
            {
                userInfo = await _tokenHelper.GetUserInfo();
            }
            catch
            {
                return(Unauthorized());
            }

            var viewModel = _service.CreateConcept(concept, userInfo);

            if (viewModel == null)
            {
                return(NotFound());
            }

            if (viewModel.Data != null)
            {
                return(Ok(viewModel));
            }

            if (viewModel.HasErrors())
            {
                return(BadRequest(viewModel));
            }

            return(InternalServerError());
        }
Beispiel #3
0
        public Response CreateConcept(CreateConceptDto newConcept, UserInfo userInfo)
        {
            var viewModel = new Response();
            var concept   = Mapper.Map <Concept>(newConcept);
            var media     = new List <ConceptMedia>();

            if (userInfo == null || string.IsNullOrEmpty(userInfo.Email) || string.IsNullOrEmpty(userInfo.FullName))
            {
                viewModel.Errors.TryAddModelError("errorMessage", "Could not get user information");
                return(viewModel);
            }

            var language = GetLanguage(viewModel, newConcept.MetaIds);

            if (language == null)
            {
                return(viewModel);
            }

            // Readonly fields
            concept.LanguageId   = language.Id;
            concept.AuthorEmail  = userInfo.Email;
            concept.AuthorName   = userInfo.FullName;
            concept.UrlToContent = string.Empty;

            // Create a language variation
            if (concept.GroupId != Guid.Empty)
            {
                if (!CanCreateLanguageVariation(viewModel, concept))
                {
                    return(viewModel);
                }
            }
            else
            {
                concept.GroupId = Guid.NewGuid();
            }

            try
            {
                concept = _conceptRepository.Insert(concept);
            }
            catch (Exception e)
            {
                viewModel.Errors.TryAddModelError("errorMessage", "Could not create concept");
                return(viewModel);
            }

            try
            {
                media = _conceptMediaRepository.InsertMediaForConcept(concept.Id, newConcept.Media);
            }
            catch (Exception e)
            {
                viewModel.Errors.TryAddModelError("errorMessage", "Could not insert media for concept");
                return(viewModel);
            }

            concept.Media  = media.Select(x => x.Media).ToList();
            viewModel.Data = Mapper.Map <ConceptDto>(concept);

            return(viewModel);
        }