Esempio n. 1
0
        public TurnResultDto Request(DateTime date, int hourId, int teamId, int perfilId)
        {
            try
            {
                turnRepository.TransactionManager.BeginTransaction();
                DateTime now = Helper.GetDateTimeZone();
                Helper.ThrowIf(date < now, "No se puede solicitar un turno para una fecha pasada.");
                Hour hour = hourRepository.Get(hourId);
                Helper.ThrowIfNull(hour, "Horario inválido.");
                Team team = teamRepository.Get(teamId);
                Helper.ThrowIfNull(team, "Grupo no encontrado.");
                Player requestPlayer = team.Players.Where(p => p.Perfil.Id == perfilId).FirstOrDefault();
                Helper.ThrowIfNull(requestPlayer, "No eres miembro de este grupo.");
                int amoutPlayers = (int)EPlayersOnTeam.MIN_NUMBER;
                Helper.ThrowIf(team.Players.Count < amoutPlayers, string.Format("El grupo {0} no tiene la cantidad de participantes suficientes para invitar, debe tener al menos {1} participantes.", team.Name, amoutPlayers));
                Field field = fieldRepository.FindAvailable(date, hour);
                Helper.ThrowIfNull(field, "No hay canchas disponibles.");

                Turn turn = new Turn();
                turn.Date     = date;
                turn.FullName = team.Name;
                turn.State    = EState.REQUESTED;
                turn.Field    = field;
                turn.Hour     = hour;
                turn.Team     = team;
                turnRepository.Save(turn);
                turnRepository.SaveOrUpdate(turn);
                Camp camp = hour.Camp;
                requestPlayer.ConfirmDate = now;
                playerRepository.SaveOrUpdate(requestPlayer);

                IList <Player> players = team.Players.Where(p => p.Perfil.Id != perfilId).ToList();
                //Notificar a los jugadores
                foreach (Player player in players)
                {
                    if (player.ConfirmDate.HasValue)
                    {
                        player.ConfirmDate = null;
                        playerRepository.SaveOrUpdate(player);
                    }

                    Perfil perfil = player.Perfil;
                    string nofity = string.Format("Hola {0}! El grupo {1} quiere jugar el día {2} a Hs {3} en la cancha {4}, {5} {6}. ¿Te sumas?", perfil.Name, team.Name, turn.Date.ToString("dd/MM/yy"), hour.Time.ToString("HH:mm"), camp.Name, camp.Street, camp.Number);
                }

                turnRepository.TransactionManager.CommitTransaction();

                DateTime dateTime = new DateTime(date.Year, date.Month, date.Day, hour.Time.Hours, hour.Time.Minutes, hour.Time.Seconds);
                return(new TurnResultDto(turn.Id, dateTime, turn.FullName, turn.Field.Name));
            }
            catch
            {
                turnRepository.TransactionManager.RollbackTransaction();
                throw;
            }
        }