예제 #1
0
파일: Create.cs 프로젝트: dusty3ntity/Log
            public async Task <Guid> Handle(Command request, CancellationToken cancellationToken)
            {
                var dictionary = await _context.Dictionaries.FindAsync(request.DictionaryId);

                if (dictionary == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, ErrorType.DictionaryNotFound);
                }

                if (dictionary.WordsCount + dictionary.PhrasesCount == 8000)
                {
                    throw new RestException(HttpStatusCode.BadRequest, ErrorType.ItemsLimitReached);
                }

                var originalLower    = request.Original.ToLower();
                var translationLower = request.Translation.ToLower();

                if (ItemChecker.AreEqual(originalLower, translationLower))
                {
                    throw new RestException(HttpStatusCode.BadRequest,
                                            ErrorType.ItemOriginalOrTranslationContainEachOther);
                }

                if (request.Definition != null && ItemChecker.DoesDefinitionContainItem(request.Definition,
                                                                                        originalLower,
                                                                                        translationLower))
                {
                    throw new RestException(HttpStatusCode.BadRequest,
                                            ErrorType.ItemDefinitionContainsOriginalOrTranslation);
                }

                var item = new Item
                {
                    Dictionary = dictionary,

                    Original         = request.Original,
                    Translation      = request.Translation,
                    Definition       = request.Definition,
                    DefinitionOrigin = request.DefinitionOrigin,
                    Type             = request.Type,
                    CreationDate     = DateTime.Now,

                    IsStarred = request.IsStarred,

                    GoesForNextDay = request.IsStarred,
                };

                if (item.Type == ItemType.Word)
                {
                    dictionary.WordsCount++;
                }
                else
                {
                    dictionary.PhrasesCount++;
                }

                _context.Items.Add(item);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(item.Id);
                }
                throw new RestException(HttpStatusCode.InternalServerError, ErrorType.SavingChangesError);
            }
예제 #2
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var dictionary = await _context.Dictionaries.FindAsync(request.DictionaryId);

                if (dictionary == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, ErrorType.DictionaryNotFound);
                }

                var item = await _context.Items.FindAsync(request.ItemId);

                if (item == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, ErrorType.ItemNotFound);
                }

                var originalLower    = request.Original.ToLower();
                var translationLower = request.Translation.ToLower();

                if (request.Definition != null && ItemChecker.DoesDefinitionContainItem(request.Definition,
                                                                                        originalLower,
                                                                                        translationLower))
                {
                    throw new RestException(HttpStatusCode.BadRequest,
                                            ErrorType.ItemDefinitionContainsOriginalOrTranslation);
                }

                if (!item.Original.ToLower().Equals(originalLower) ||
                    !item.Translation.ToLower().Equals(translationLower))
                {
                    if (ItemChecker.AreEqual(originalLower, translationLower))
                    {
                        throw new RestException(HttpStatusCode.BadRequest,
                                                ErrorType.ItemOriginalOrTranslationContainEachOther);
                    }

                    if (item.IsLearned)
                    {
                        if (item.Type == ItemType.Word)
                        {
                            dictionary.LearnedWordsCount--;
                        }
                        else
                        {
                            dictionary.LearnedPhrasesCount--;
                        }
                    }

                    item.IsLearned = false;
                    item.CorrectAnswersToCompletionCount = 0;
                    item.CreationDate = DateTime.Now;
                }

                item.Original         = request.Original;
                item.Translation      = request.Translation;
                item.Definition       = request.Definition;
                item.DefinitionOrigin = request.DefinitionOrigin;

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }
                throw new RestException(HttpStatusCode.InternalServerError, ErrorType.SavingChangesError);
            }