コード例 #1
0
        public IActionResult Add(RegisterKittenBindingModel model)
        {
            using (this.Context)
            {
                var breeds = this.Context.Breeds.ToList();

                bool validCatModel = ValidateCatModel(model, breeds);
                if (this.User.IsAuthenticated && validCatModel)
                {
                    Breed breed = this.Context.Breeds.FirstOrDefault(x => x.Name == model.Breed);

                    Kitten kitten = new Kitten()
                    {
                        Name    = model.Name,
                        Age     = int.Parse(model.Age),
                        BreedId = breed.Id,
                        Breed   = breed
                    };

                    breed.Kittens.Add(kitten);

                    this.Context.Kittens.Add(kitten);
                    this.Context.SaveChanges();
                    return(this.Add());
                }
                else if (!validCatModel)
                {
                    return(View());
                }
            }
            this.Model.Data[ErrorKey] = ShowMessageValue;
            this.Model.Data[ErrorKey] = UnauthorizedErrorMessage;

            return(RedirectToAction(LoginRoute));
        }
コード例 #2
0
        private bool ValidateCatModel(RegisterKittenBindingModel model, List <Breed> breeds)
        {
            bool validName = this.IsValidModel(model);

            if (!validName)
            {
                this.Model.Data[ErrorKey]        = ShowMessageValue;
                this.Model.Data[ErrorMessageKey] = InvalidInputMessage;

                return(false);
            }

            bool validInteger = int.TryParse(model.Age, out int age);

            if (!breeds.Any(x => x.Name == model.Breed))
            {
                this.Model.Data[ErrorKey]        = ShowMessageValue;
                this.Model.Data[ErrorMessageKey] = InvalidBreedMessage;

                return(false);
            }
            else if (!validInteger || age < 1)
            {
                this.Model.Data[ErrorKey]        = ShowMessageValue;
                this.Model.Data[ErrorMessageKey] = InvalidAgeMessage;

                return(false);
            }

            return(true);
        }
コード例 #3
0
        public IActionResult Add(RegisterKittenBindingModel model)
        {
            if (!this.IsValidModel(model))
            {
                this.Model.Data[ErrorKey] = InvalidKittenRegisterInput;
            }

            using (this.Context)
            {
                bool isValidAge   = int.TryParse(model.Age, out int age) && (age >= ValidationConstants.KittenConstraints.MinAge && age <= ValidationConstants.KittenConstraints.MaxAge);
                bool isValidBreed = ValidationConstants.BreedConstraints.AllowedBreeds.Contains(model.Breed);

                if (!isValidAge)
                {
                    this.Model.Data[ErrorKey] = InvalidKittenAge;
                }
                else if (!isValidBreed)
                {
                    this.Model.Data[ErrorKey] = InvalidKittenBreed;
                }
                else
                {
                    Breed breed = this.Context.Breeds.FirstOrDefault(x => x.Name == model.Breed);

                    Kitten kitten = new Kitten()
                    {
                        Name    = model.Name,
                        Age     = age,
                        BreedId = breed.Id,
                        Breed   = breed
                    };

                    this.Context.Kittens.Add(kitten);
                    this.Context.SaveChanges();

                    return(RedirectToHome);
                }
            }

            return(this.View());
        }