Beispiel #1
0
 private bool ValidateCard(CardCreationInfo cardToValidate)
 {
     if (!FieldsAreFilled(cardToValidate.Question))
     {
         throw new AppException($"Не заполнены поля в \"Вопросе\" карты");
     }
     if (!FieldsAreFilled(cardToValidate.Answer))
     {
         throw new AppException($"Не заполнены поля в \"Вопросе\" карты");
     }
     if (!LengthIsCorrect(cardToValidate.Question.Text))
     {
         throw new AppException($"Некорректная длина в \"Вопросе\" карты в поле \"Текст\"" +
                                $", должна быть от {MinimumTextLength} до {MaximumTextLength}");
     }
     if (!LengthIsCorrect(cardToValidate.Question.Code))
     {
         throw new AppException($"Некорректная длина в \"Вопросе\" карты в поле \"Код\"" +
                                $", должна быть от {MinimumCodeLength} до {MaximumCodeLength}");
     }
     if (!LengthIsCorrect(cardToValidate.Answer.Text))
     {
         throw new AppException($"Некорректная длина в \"Ответе\" карты в поле \"Текст\"" +
                                $", должна быть от {MinimumTextLength} до {MaximumTextLength}");
     }
     if (!LengthIsCorrect(cardToValidate.Answer.Code))
     {
         throw new AppException($"Некорректная длина в \"Ответе\" карты в поле \"Код\"" +
                                $", должна быть от {MinimumCodeLength} до {MaximumCodeLength}");
     }
     return(true);
 }
Beispiel #2
0
        private bool ValidateCard(CardCreationInfo cardToValidate)
        {
            if (!FieldsAreFilled(cardToValidate.Question))
            {
                return(false);
            }
            if (!FieldsAreFilled(cardToValidate.Answer))
            {
                return(false);
            }
            if (!LengthIsCorrect(cardToValidate.Question.Text))
            {
                return(false);
            }
            if (!LengthIsCorrect(cardToValidate.Question.Code))
            {
                return(false);
            }
            if (!LengthIsCorrect(cardToValidate.Answer.Text))
            {
                return(false);
            }
            if (!LengthIsCorrect(cardToValidate.Answer.Code))
            {
                return(false);
            }

            return(true);
        }
Beispiel #3
0
        public CardItem CreateCard(CardCreationInfo cardToCreate, Guid userId)
        {
            ValidateCard(cardToCreate);

            var card = new CardItem
            {
                Question  = cardToCreate.Question,
                Answer    = cardToCreate.Answer,
                CreatedAt = DateTime.Now,
                UserId    = userId,
            };

            return(card);
        }
Beispiel #4
0
        private bool IsCardValid(CardItem cardToValidate)
        {
            if (cardToValidate.UserId == Guid.Empty)
            {
                return(false);
            }

            var card = new CardCreationInfo
            {
                Question = cardToValidate.Question,
                Answer   = cardToValidate.Answer
            };

            return(ValidateCard(card));
        }
Beispiel #5
0
        public async Task <ActionResult <CardItem> > CreateAsync([FromBody] CardCreationInfo cardCreationInfo,
                                                                 CancellationToken cancellationToken)
        {
            try
            {
                Guid.TryParse(HttpContext.User.Identity.Name, out var userId);

                var card = cardsService.CreateCard(cardCreationInfo, userId);
                await cardsService.AddCardAsync(card, cancellationToken);

                return(Ok(card));
            }
            catch (AppException ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
        }
Beispiel #6
0
        public CardItem CreateCard(CardCreationInfo cardToCreate, Guid uId)
        {
            if (!ValidateCard(cardToCreate))
            {
                return(null);
            }

            var card = new CardItem
            {
                Question  = cardToCreate.Question,
                Answer    = cardToCreate.Answer,
                CreatedAt = DateTime.Now,
                UserId    = uId
            };

            return(card);
        }