Beispiel #1
0
        public void AddWord_WhenTheWordDoesntStartWithCorrectCharachter_ShouldThrowExceptionWithCorrectMessage()
        {
            var wordsInTopic = new List <Word>
            {
                new Word {
                    WordContent = "dog", TopicId = 1, IsDeleted = false
                }
            };
            var topic = new Topic
            {
                Id         = 1,
                WordsCount = 0,
                Words      = wordsInTopic
            };
            var requestModel = new WordRequestModel {
                Word = "cat"
            };

            this.mockedUnitOfWork.Setup(x => x.Topics.Get(It.IsAny <Expression <Func <Topic, bool> > >(), null, It.IsAny <string>()))
            .Returns(new List <Topic> {
                topic
            });
            this.mockedUnitOfWork.Setup(x => x.Words.Insert(It.IsAny <Word>()));
            this.mockedUnitOfWork.Setup(x => x.Commit());

            Assert.Throws <InvalidWordException>(() => this.service.AddWord(1, "userId", requestModel), message: "The new word should start with t.");
            this.mockedUnitOfWork.Verify(x => x.Words.Insert(It.IsAny <Word>()), Times.Never);
            this.mockedUnitOfWork.Verify(x => x.Commit(), Times.Never);
        }
Beispiel #2
0
        public void AddWord_WhenTheWordIsMarkedAsInappropriate_ShouldThrowExceptionWithCorrectMessage()
        {
            var wordsInTopic = new List <Word>
            {
                new Word {
                    WordContent = "dog", TopicId = 1, IsDeleted = true
                }
            };
            var topic = new Topic
            {
                Id         = 1,
                WordsCount = 0,
                Words      = wordsInTopic
            };
            var requestModel = new WordRequestModel {
                Word = "dog"
            };

            this.mockedUnitOfWork.Setup(x => x.Topics.Get(It.IsAny <Expression <Func <Topic, bool> > >(), null, It.IsAny <string>()))
            .Returns(new List <Topic> {
                topic
            });
            this.mockedUnitOfWork.Setup(x => x.Words.Insert(It.IsAny <Word>()));
            this.mockedUnitOfWork.Setup(x => x.Commit());

            Assert.Throws <InvalidWordException>(() => this.service.AddWord(1, "userId", requestModel), message: "The word is already marked as inappropriate in this topic.");
            this.mockedUnitOfWork.Verify(x => x.Words.Insert(It.IsAny <Word>()), Times.Never);
            this.mockedUnitOfWork.Verify(x => x.Commit(), Times.Never);
        }
Beispiel #3
0
        public void AddWord_WhenTheWordStartsWithCorrectCharachterAndIsUnique_ShouldInsertTheWord()
        {
            var wordsInTopic = new List <Word>
            {
                new Word {
                    WordContent = "cat", TopicId = 1, IsDeleted = false
                }
            };
            var topic = new Topic
            {
                Id         = 1,
                WordsCount = 1,
                Words      = wordsInTopic
            };
            var requestModel = new WordRequestModel {
                Word = "turtle"
            };

            this.mockedUnitOfWork.Setup(x => x.Topics.Get(It.IsAny <Expression <Func <Topic, bool> > >(), null, It.IsAny <string>()))
            .Returns(new List <Topic> {
                topic
            });
            this.mockedUnitOfWork.Setup(x => x.Words.Insert(It.IsAny <Word>()));
            this.mockedUnitOfWork.Setup(x => x.Commit());

            var result = this.service.AddWord(1, "userId", requestModel);

            this.mockedUnitOfWork.Verify(x => x.Words.Insert(It.IsAny <Word>()), Times.Once);
            this.mockedUnitOfWork.Verify(x => x.Commit(), Times.Once);
            Assert.AreEqual(topic.WordsCount, 2);
        }
Beispiel #4
0
        private void SetupDefaultHeader(int wordCount, int minWordLength, int maxWordLength)
        {
            headers.Add("Authorization", "MatthewIsTheGreatest1234");
            headers.Add("Token", "Test"); // token not used in this version
            _client.AddDefaultHeaders(headers);
            WordRequestModel word = new WordRequestModel()
            {
                WordCount = wordCount, MaxWordLength = maxWordLength, MinWordLength = minWordLength
            };

            restRequest.AddJsonBody(word);
        }
Beispiel #5
0
        public async Task <bool> UpdateEntityByIdAsync(WordRequestModel modelRequest, int id)
        {
            var entity = _mapper.Map <WordRequestModel, Words>(modelRequest);

            entity.Id = id;

            var updated = await _uow.WordsRepository.UpdateAsync(entity);

            var result = await _uow.SaveAsync();

            return(result);
        }
        public virtual async Task <ActionResult <WordsDTO> > Create([FromBody] WordRequestModel request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var dtos = await _wordsService.CreateEntityAsync(request);

            if (dtos == null)
            {
                return(StatusCode(500));
            }

            return(Ok());
        }
        public virtual async Task <ActionResult> Update([FromRoute] int id, [FromBody] WordRequestModel request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var result = await _wordsService.UpdateEntityByIdAsync(request, id);

            if (!result)
            {
                return(StatusCode(500));
            }

            return(NoContent());
        }
Beispiel #8
0
        public ListedWordResponseModel AddWord(int topicId, string userId, WordRequestModel model)
        {
            var topic = unitOfWork.Topics
                        .Get(filter: t => t.Id == topicId,
                             includeProperties: "Words")
                        .SingleOrDefault();

            var lastWord = topic.Words.OrderBy(w => w.DateCreated)
                           .LastOrDefault();

            if (lastWord != null)
            {
                var lastWordLastCharachter = lastWord.WordContent.Last().ToString();

                if (topic.Words.Where(w => !w.IsDeleted).Select(w => w.WordContent).Contains(model.Word))
                {
                    throw new InvalidWordException($"The word is already added in this topic.");
                }


                if (topic.Words.Where(w => w.IsDeleted).Select(w => w.WordContent).Contains(model.Word))
                {
                    throw new InvalidWordException($"The word is already marked as inappropriate in this topic.");
                }

                if (!model.Word.StartsWith(lastWordLastCharachter))
                {
                    throw new InvalidWordException($"The new word should start with {lastWordLastCharachter}.");
                }
            }

            var word = mapper.Map <Word>(model);

            word.AuthorId    = userId;
            word.DateCreated = DateTime.Now;
            word.TopicId     = topicId;

            unitOfWork.Words.Insert(word);
            topic.WordsCount++;

            unitOfWork.Commit();

            return(mapper.Map <ListedWordResponseModel>(word));
        }
        public IHttpActionResult AddWord(int topicId, WordRequestModel model)
        {
            if (this.unitOfWork.Topics.GetByID(topicId) == null)
            {
                return(NotFound());
            }

            if (model == null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            string authorId = User.Identity.GetUserId();

            try
            {
                return(Ok(this.topics.AddWord(topicId, authorId, model)));
            }
            catch (InvalidWordException ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Beispiel #10
0
        public async Task <WordsDTO> CreateEntityAsync(WordRequestModel modelRequest)
        {
            var entity = _mapper.Map <WordRequestModel, Words>(modelRequest);

            entity = await _uow.WordsRepository.CreateEntityAsync(entity);

            var result = await _uow.SaveAsync();

            if (!result)
            {
                return(null);
            }

            if (entity == null)
            {
                return(null);
            }

            var dto = _mapper.Map <Words, WordsDTO>(entity);

            return(dto);
        }
Beispiel #11
0
        public void AddWord_WhenTheWordsCountInTopicIsZero_ShouldInsertTheWord()
        {
            var topic = new Topic
            {
                Id         = 1,
                WordsCount = 0,
            };
            var requestModel = new WordRequestModel {
                Word = "dog"
            };

            this.mockedUnitOfWork.Setup(x => x.Topics.Get(It.IsAny <Expression <Func <Topic, bool> > >(), null, It.IsAny <string>()))
            .Returns(new List <Topic> {
                topic
            });
            this.mockedUnitOfWork.Setup(x => x.Words.Insert(It.IsAny <Word>()));
            this.mockedUnitOfWork.Setup(x => x.Commit());

            this.service.AddWord(1, "userId", requestModel);


            this.mockedUnitOfWork.Verify(x => x.Words.Insert(It.IsAny <Word>()), Times.Once);
            this.mockedUnitOfWork.Verify(x => x.Commit(), Times.Once);
        }