示例#1
0
        public HttpResponse DoAdd()
        {
            var db = new ApplicationDbContext();

            if (Request.FormData["name"].Length < 5)
            {
                return Error("Name should be at least 5 characters long.");
            }

            db.Cards.Add(new Card
            {
                Attack = int.Parse(Request.FormData["attack"]),
                Health = int.Parse(Request.FormData["health"]),
                Description = Request.FormData["description"],
                Name = Request.FormData["name"],
                ImageUrl = Request.FormData["image"],
                Keyword = Request.FormData["keyword"],
            });

            db.SaveChanges();

            var viewModel = new DoAddViewModel
            {
                Attack = int.Parse(Request.FormData["attack"]),
                Health = int.Parse(Request.FormData["health"]),
            };

            return View(viewModel);
        }
示例#2
0
        public HttpResponse DoAdd(AddCardInputModel model)
        {
            if (!IsUserSignedIn())
            {
                return(Redirect("/users/login"));
            }

            if (model.Name.Length < 5)
            {
                return(Error("Name should be at least 5 characters long."));
            }

            db.Cards.Add(new Card
            {
                Attack      = model.Attack,
                Health      = model.Health,
                Description = model.Description,
                Name        = model.Name,
                ImageUrl    = model.Image,
                Keyword     = model.Keyword,
            });

            db.SaveChanges();

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

            return(View(viewModel));
        }
示例#3
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));
        }