Esempio n. 1
0
        public ICollection <string> ValidatePlayer(CreatePlayerFormModel model)
        {
            var errors = new List <string>();

            if (model.FullName.Length < PlayerFullNameMin || model.FullName.Length > PlayerFullNameMax)
            {
                errors.Add($"Name {model.FullName} must be between {PlayerFullNameMin} and {PlayerFullNameMax} characters.");
            }

            if (model.Position.Length < PlayerPositionMin || model.Position.Length > PlayerPositionMax)
            {
                errors.Add($"Position {model.Position} must be between {PlayerPositionMin} and {PlayerPositionMax} characters.");
            }

            if (model.Speed < PlayerSpeedMin || model.Speed > PlayerSpeedMax)
            {
                errors.Add($"Player speed value must be between {PlayerSpeedMin} and {PlayerSpeedMax}.");
            }

            if (model.Endurance < PlayerEnduranceMin || model.Endurance > PlayerEnduranceMax)
            {
                errors.Add($"Player endurance value must be between {PlayerEnduranceMin} and {PlayerEnduranceMax}.");
            }

            if (model.Description.Length > PlayerDescriptionMax)
            {
                errors.Add($"Player description cannot exceed {PlayerDescriptionMax} characters.");
            }

            return(errors);
        }
Esempio n. 2
0
        public HttpResponse Add(CreatePlayerFormModel model)
        {
            var modelErrors = this.validator.ValidatePlayer(model);

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

            if (!this.User.IsAuthenticated)
            {
                var errors = new List <string>()
                {
                    "Users must login to add a player."
                };

                return(Error(modelErrors));
            }

            var user = this.data
                       .Users
                       .Where(u => u.Id == this.User.Id)
                       .FirstOrDefault();

            var player = new Player
            {
                FullName    = model.FullName,
                ImageUrl    = model.ImageURL,
                Position    = model.Position,
                Speed       = model.Speed,
                Endurance   = model.Endurance,
                Description = model.Description
            };

            this.data.Players.Add(player);

            this.data.SaveChanges();

            var addedPlayer = this.data
                              .Players
                              .Where(p => p.FullName == player.FullName && p.ImageUrl == player.ImageUrl)
                              .FirstOrDefault();

            var userPlayer = new UserPlayer
            {
                UserId   = user.Id,
                PlayerId = addedPlayer.Id,
            };

            this.data.UserPlayers.Add(userPlayer);

            this.data.SaveChanges();

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