Ejemplo n.º 1
0
 public async Task <IActionResult> AddCard(string word)
 {
     try
     {
         return(Ok(await _cardsService.AddCard(word)));
     }
     catch (Exception ex)
     {
         var errorMessage = "There was an error while trying to add card to MongoDB";
         return(BadRequest(errorMessage + "\n" + ex));
     }
 }
Ejemplo n.º 2
0
        public HttpResponse Add(AddCardInputModel model)
        {
            if (!IsUserSignedIn())
            {
                return(Redirect("/users/login"));
            }

            if (string.IsNullOrEmpty(model.Name) || model.Name.Length < 5 || model.Name.Length > 15)
            {
                return(Error("Name should be between 5 and 15 characters long."));
            }

            if (string.IsNullOrWhiteSpace(model.Image))
            {
                return(Error("The image is required!"));
            }

            if (string.IsNullOrWhiteSpace(model.Keyword))
            {
                return(Error("Keyword is required."));
            }

            if (model.Attack < 0)
            {
                return(Error("Attack should be non-negative integer."));
            }

            if (model.Health < 0)
            {
                return(Error("Health should be non-negative integer."));
            }

            if (string.IsNullOrWhiteSpace(model.Description) || model.Description.Length > 200)
            {
                return(Error("Description is required and its length should be at most 200 characters."));
            }

            var cardId = cardsService.AddCard(model);
            var userId = GetUserId();

            cardsService.AddCardToUserCollection(userId, cardId);

            var viewModel = new DoAddViewModel
            {
                Attack = model.Attack,
                Health = model.Health,
            };

            return(View(viewModel));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> AddCard(Contracts.Cards.AddCardRequest card, ApiVersion version)
        {
            try
            {
                var domainCard = new Core.Domain.AddCardRequest
                {
                    DefinitionId = card.DefinitionId,
                    Name         = card.Name,
                    Properties   = card.Properties.Select(p => new Core.Domain.Property
                    {
                        Name  = p.Name,
                        Value = p.Value
                    })
                };

                var addedCardId = await cardsService.AddCard(domainCard);

                return(CreatedAtAction(nameof(GetCard), new { version = version.ToString(), cardId = addedCardId }, null));
            }
            catch (ArgumentException)
            {
                return(BadRequest());
            }
        }