Exemplo n.º 1
0
        public Pronostic Create(Pronostic pronostic)
        {
            if (context.Pronostic.Any(x => x.IdMatch == pronostic.IdMatch && x.IdUser == pronostic.IdUser))
            {
                throw new AppException("Pronostic already created on match id " + pronostic.IdMatch + " for user id " + pronostic.IdUser);
            }

            context.Pronostic.Add(pronostic);
            context.SaveChanges();

            return(pronostic);
        }
Exemplo n.º 2
0
        public Match Create(Match match)
        {
            if (context.Match.Any(x => x.Team1 == match.Team1 || x.Team1 == match.Team2))
            {
                if (context.Match.Any(x => x.Date == match.Date && x.Team2 == match.Team1 || x.Team2 == match.Team2))
                {
                    throw new AppException("A match between " + match.Team1 + " and " + match.Team2 + " at " + match.Date + " already exist");
                }
            }

            context.Match.Add(match);
            context.SaveChanges(); // needed to have the ID of current match object on the following code


            // Create pronostic for each user when a match is created
            foreach (User user in context.User)
            {
                context.Pronostic.Add(new Pronostic
                {
                    IdMatch = match.Id,
                    IdUser  = user.Id
                });
            }
            context.SaveChanges();

            return(match);
        }
Exemplo n.º 3
0
        public User Create(User user, string password)
        {
            // validation
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new AppException("Password is required");
            }

            if (context.User.Any(x => x.Username == user.Username))
            {
                throw new AppException("Username " + user.Username + " is already taken");
            }

            if (context.User.Any(x => x.Email == user.Email))
            {
                throw new AppException("Email adress " + user.Email + " is already used");
            }

            user.Password = CryptoHelper.Encrypt(password);

            context.User.Add(user);
            context.SaveChanges();  // needed to have the ID of current User object on the following code

            // Add pronostic for each match already created
            foreach (Match match in context.Match)
            {
                context.Pronostic.Add(new Pronostic
                {
                    IdUser  = user.Id,
                    IdMatch = match.Id
                });
            }
            context.SaveChanges();

            return(user);
        }