Esempio n. 1
0
        public static List <ScheduleMeetings> GetAvailablePeriod(List <ScheduleMeetings> scheduledMeetings)
        {
            scheduledMeetings = scheduledMeetings.OrderBy(x => x.MeetingStart).ToList();
            var meetingAvailable   = new List <ScheduleMeetings>();
            var currentTime        = DateTime.Now.ToTimeSpan();
            var lastHour           = new TimeSpan(23, 59, 59);
            var previousMeetingEnd = TimeSpan.Zero;

            for (int i = 1; i <= scheduledMeetings.Count; i++)
            {
                var item = scheduledMeetings[i - 1];

                if (previousMeetingEnd == TimeSpan.Zero && currentTime < item.MeetingStart)
                {
                    meetingAvailable.Add(ScheduleMeetings.Build(currentTime, item.MeetingStart.AddMinutes(-1)));
                }

                if (previousMeetingEnd > TimeSpan.Zero && MinimumTime(item.MeetingStart, previousMeetingEnd))
                {
                    meetingAvailable.Add(ScheduleMeetings.Build(previousMeetingEnd.AddMinutes(1), item.MeetingStart.AddMinutes(-1)));
                }

                if (i == scheduledMeetings.Count && MinimumTime(lastHour, item.MeetingEnd))
                {
                    meetingAvailable.Add(ScheduleMeetings.Build(item.MeetingEnd.AddMinutes(1), lastHour));
                }

                previousMeetingEnd = item.MeetingEnd;
            }

            return(meetingAvailable);
        }
        public ResponseModel Schedule(ScheduleMeetingsModel model)
        {
            try
            {
                #region Validations

                var response = ResponseModel.Empty();

                #region Validate model

                if (model.Date.Date < DateTime.Today)
                {
                    response.AddError($"Deve-se marcar a reunião apartir da data de {DateTime.Today.ToString("dd/MM/yyyy")}");
                }

                if (model.Date.Date == DateTime.Today && model.Start < DateTime.Now.ToTimeSpan())
                {
                    response.AddError("Hora início da reunião deve ser maior que a hora atual");
                }

                if (model.End <= model.Start)
                {
                    response.AddError("Hora final da reunião deve ser maior que a hora de início");
                }

                if (response.IsNotValid())
                {
                    return(response);
                }

                #endregion

                if (!_roomsRepository.Exists(model.RoomId))
                {
                    response.AddError("Sala não encontrada");
                }

                if (!_usersRepository.Exists(model.UserId))
                {
                    response.AddError("Usuário não encontrado");
                }

                var scheduledMeetings = _scheduleMeetingsRepository.GetByDate(model.Date, model.RoomId);

                if (!Meeting.Available(scheduledMeetings, model.Start, model.End))
                {
                    response.AddError($"Não foi possível agendar a reunião no dia {model.Date.ToString("dd/MM/yyyy")} nesse período ({model.Start} - {model.End})");
                }

                if (response.IsNotValid())
                {
                    return(response);
                }
                #endregion

                var meeting = ScheduleMeetings.New();

                meeting.UserId       = model.UserId;
                meeting.RoomId       = model.RoomId;
                meeting.Title        = model.Title;
                meeting.MeetingDate  = model.Date;
                meeting.MeetingStart = model.Start;
                meeting.MeetingEnd   = model.End;

                _scheduleMeetingsRepository.Add(meeting);
                _unitOfWork.Commit();

                return(ResponseModel.BuildSuccess());
            }
            catch (Exception ex)
            {
                return(ResponseModel.BuildError($"Erro interno: {ex.Message}"));
            }
        }