Пример #1
0
        public ICollection <string> ValidateCard(CardAddFormModel model)
        {
            var errors = new List <string>();

            if (model.Name.Length < 5 || model.Name.Length > 15)
            {
                errors.Add($"Username '{model.Name}' is not valid. It must be between 5 and 15 characters long.");
            }

            //if (!Uri.IsWellFormedUriString(model.ImageUrl, UriKind.Absolute))
            //{
            //    errors.Add($"Image {model.ImageUrl} is not a valid URL.");
            //}

            if (model.Attack < 0)
            {
                errors.Add("Attack cannot be negative.");
            }

            if (model.Health < 0)
            {
                errors.Add("Health cannot be negative.");
            }

            if (model.Description.Length > 200 || model.Description == null) // not sure if it can be null
            {
                errors.Add("Description is not valid. It must be between 1 and 200 characters long");
            }

            return(errors);
        }
Пример #2
0
        public HttpResponse Add(CardAddFormModel model)
        {
            var modelErrors = validator.ValidateCard(model);

            if (modelErrors.Any())
            {
                return(Error(modelErrors));
            }

            var card = new Card()
            {
                Name        = model.Name,
                ImageUrl    = model.Image,
                Keyword     = model.Keyword,
                Attack      = model.Attack,
                Health      = model.Health,
                Description = model.Description
            };

            this.data.Cards.Add(card);

            this.data.SaveChanges();

            var usercards = new UserCard
            {
                CardId = card.Id,
                UserId = this.User.Id
            };


            this.data.UsersCards.Add(usercards);

            this.data.SaveChanges();

            return(Redirect("/Cards/All"));
        }