public static List <Error> Validate(TournamentListDTO tournament, List <Error> errors) { errors = AddressValidator.IsAdresseCorrect(tournament.Address, errors); if (tournament.BeginDate > tournament.EndDate) { errors.Add(new Error() { Code = "DateSequenceError", Description = "La date de debut ne peut pas être après la date de fin" }); } if (tournament.Name == null || tournament.Name.Length <= 0) { errors.Add(new Error() { Code = "NameTournamentRequired", Description = "Le nom du tournois ne peut pas être vide" }); } return(errors); }
public async Task <IActionResult> Post([FromBody] TournamentListDTO tournament) { List <Error> errors = new List <Error>(); Account currentUser = await GetCurrentUserAsync(); TournamentAdmin tournamentAdmin = new TournamentAdmin() { Account = currentUser }; Club club = null; if (tournament.ClubId > 0) { var rawClub = _context.Clubs .Where(c => c.Id == tournament.ClubId && c.Admins.Where(a => a.Account.Id == currentUser.Id).Count() > 0); if (rawClub.Count() <= 0) { errors.Add(new Error() { Code = "ClubUnknowOrUnAuthorize", Description = "Le club n'as pas été trouvé ou vous n'avez pas l'authorisation" }); } else { club = rawClub.First(); } } else { errors.Add(new Error() { Code = "ClubRequired", Description = "Le club ne peut pas être vide" }); } List <TournamentJoueur> participants = new List <TournamentJoueur>(); foreach (long participantId in tournament.ParticipantsId) { UserInfo user = _context.UserInfo.First(u => u.Id == participantId && u.CreatedBy.Id == currentUser.Id); if (user != null) { TournamentJoueur tournamentJoueur = new TournamentJoueur() { User = user }; participants.Add(tournamentJoueur); } else { errors.Add(new Error() { Code = "JoueurUnknowOrUnAuthorize", Description = "Un joueur est introuvable ou vous n'avez pas accès" }); } } errors = TournamentListDTOValidator.Validate(tournament, errors); if (errors.Count() <= 0) { Tournament newTournament = new Tournament() { Club = club, Admins = new List <TournamentAdmin>() { tournamentAdmin }, Address = tournament.Address, BeginDate = tournament.BeginDate, EndDate = tournament.EndDate, Etat = tournament.Etat, Name = tournament.Name, Participants = participants, }; _context.Tournaments.Add(newTournament); _context.SaveChanges(); tournament.Id = newTournament.Id; return(Created("tournament/" + tournament.Id, tournament)); } else { return(BadRequest(errors)); } }