Exemple #1
0
        public TurnResultDto Reserve(DateTime date, int hourId, string name)
        {
            try
            {
                turnRepository.TransactionManager.BeginTransaction();
                Helper.ThrowIfIsNullOrEmpty(name, "Debe ingresar el nombre de quien reserva.");
                Helper.ThrowIf(date < Helper.GetDateTimeZone(), "No se puede solicitar un turno para una fecha pasada.");
                Hour hour = hourRepository.Get(hourId);
                Helper.ThrowIfNull(hour, "Horario inválido.");
                Field field = fieldRepository.FindAvailableReserve(date, hour);
                Helper.ThrowIfNull(field, "No hay canchas disponibles.");

                Turn turn = new Turn();
                turn.Date     = date;
                turn.FullName = name;
                turn.State    = EState.RESERVED;
                turn.Field    = field;
                turn.Hour     = hour;
                turnRepository.Save(turn);

                IList <Turn> requestedTurns = turnRepository.GetRequests(date, hour, field);
                foreach (Turn requestedTurn in requestedTurns)
                {
                    Field requestedField = fieldRepository.FindAvailable(date, hour);
                    if (requestedField != null)
                    {
                        requestedTurn.Field = requestedField;
                        turnRepository.SaveOrUpdate(requestedTurn);
                    }
                    else
                    {
                        //Notificar los turnos que quedan sin canchas
                    }
                }
                turnRepository.TransactionManager.CommitTransaction();

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