Пример #1
0
        IActionResult IsDayInvalid(DayOfBusy dayOfBusy, out bool flag)
        {
            if (flag = dayOfBusy == null)
            {
                return(BadRequest("Day can not be null!"));
            }

            if (flag = dayOfBusy.Holder == null)
            {
                return(BadRequest("Holder can not be null!"));
            }

            if (flag = IsRoomNotExists(dayOfBusy.RoomId))
            {
                return(NotFound("Room not found!"));
            }

            if (flag = dayOfBusy.TimeOfBusy == dayOfBusy.TimeOfFree)
            {
                return(BadRequest("Invalid time interval!"));
            }

            if (flag = dayOfBusy.TimeOfBusy < new TimeSpan(8, 0, 0) || dayOfBusy.TimeOfBusy >= new TimeSpan(20, 0, 0) ||
                       dayOfBusy.TimeOfFree <= new TimeSpan(8, 0, 0) || dayOfBusy.TimeOfFree > new TimeSpan(20, 0, 0))
            {
                return(BadRequest("Room can be occupied only during working hours!"));
            }

            return(Ok(dayOfBusy));
        }
Пример #2
0
 bool IsTimeIntervalInvalid(List <DayOfBusy> chunks, DayOfBusy day)
 {
     return(chunks.Any(x => x.RoomId == day.RoomId &&
                       day.TimeOfBusy >= x.TimeOfBusy &&
                       (day.TimeOfBusy <= x.TimeOfFree &&
                        day.TimeOfFree >= x.TimeOfFree || day.TimeOfFree <= day.TimeOfFree) ||
                       day.TimeOfBusy <= x.TimeOfBusy &&
                       (day.TimeOfFree >= x.TimeOfBusy &&
                        day.TimeOfFree <= x.TimeOfFree || day.TimeOfFree >= x.TimeOfFree)));
 }
Пример #3
0
        public IActionResult Put([FromBody] DayOfBusy dayOfBusy)
        {
            if (db.DaysOfBusy.FirstOrDefault(x => x == dayOfBusy) == null)
            {
                return(BadRequest());
            }

            db.DaysOfBusy.Update(dayOfBusy);
            db.SaveChanges();

            return(Ok(dayOfBusy));
        }
Пример #4
0
        public IActionResult Delete(DateTime date)
        {
            DayOfBusy dayOfBusy = db.DaysOfBusy.FirstOrDefault(x => x.Date == date);

            if (dayOfBusy == null)
            {
                return(BadRequest());
            }

            db.DaysOfBusy.Remove(dayOfBusy);
            db.SaveChanges();

            return(Ok(dayOfBusy));
        }
Пример #5
0
        public IActionResult Update(DateTime date, [FromBody] DayOfBusy oldDay, [FromBody] DayOfBusy newDay)
        {
            if (oldDay == null || newDay == null)
            {
                return(BadRequest("Day can not be null!"));
            }

            if (IsRoomNotExists(newDay.RoomId))
            {
                return(NotFound("Room not found!"));
            }

            ScheDay scheDay = GetScheDay(date);

            if (scheDay == null)
            {
                return(NotFound("There are no occupied rooms for this date!"));
            }

            oldDay = scheDay.Chunks
                     .FirstOrDefault(x => x.RoomId == oldDay.RoomId &&
                                     x.TimeOfBusy == oldDay.TimeOfBusy &&
                                     x.TimeOfFree == oldDay.TimeOfFree &&
                                     x.Holder == oldDay.Holder);

            if (oldDay == null)
            {
                return(BadRequest("There is no such schedule!"));
            }

            newDay.ScheDayId = oldDay.ScheDayId;
            oldDay           = newDay;

            db.DaysOfBusy.Update(oldDay);
            db.SaveChanges();

            return(Ok(newDay));
        }
Пример #6
0
        public IActionResult AddDay([FromBody] DayOfBusy dayOfBusy, DateTime?date = null)
        {
            bool          invalid;
            IActionResult request = IsDayInvalid(dayOfBusy, out invalid);

            if (invalid)
            {
                return(request);
            }

            if (date == null)
            {
                date = DateTime.Today;
            }

            ScheDay scheDay = GetScheDay(date.Value);

            if (scheDay == null)
            {
                db.ScheDays.Add(new ScheDay
                {
                    Date = date.Value
                });
                db.SaveChanges();
                scheDay = GetScheDay(date.Value);
            } //если указанной даты нет, добавляем её в БД

            if (IsTimeIntervalInvalid(scheDay.Chunks, dayOfBusy))
            {
                return(BadRequest("Room already exists!"));
            }

            dayOfBusy.ScheDayId = scheDay.Id;
            db.DaysOfBusy.Add(dayOfBusy);
            db.SaveChanges();

            return(Ok(dayOfBusy));
        }
Пример #7
0
        public IActionResult Delete(DateTime date, int idRoom, TimeSpan tBusy)
        {
            ScheDay scheDay = GetScheDay(date);

            if (scheDay == null)
            {
                return(NotFound("Day is not busy!"));
            }

            if (IsRoomNotExists(idRoom))
            {
                return(NotFound("Room not found!"));
            }

            DayOfBusy day = scheDay.Chunks
                            .FirstOrDefault(x => x.RoomId == idRoom &&
                                            x.TimeOfBusy == tBusy);

            if (day == null)
            {
                return(NotFound("Room is already free!"));
            }

            db.DaysOfBusy.Remove(day);
            db.SaveChanges();

            scheDay = GetScheDay(date);

            if (scheDay.Chunks.Count == 0)
            {
                db.ScheDays.Remove(scheDay);
                db.SaveChanges();
            }

            return(Ok(date));
        }