public IActionResult GetDay(DateTime date, int?roomId = null) { ScheDay scheDay = GetScheDay(date); if (scheDay == null) { return(NotFound("Date is free!")); } if (roomId != null && IsRoomNotExists(roomId.Value)) { return(BadRequest("Room not found!")); } List <DayOfBusy> days = roomId == null ? scheDay.Chunks : scheDay.Chunks.FindAll(x => x.RoomId == roomId); //находим все записи по номеру переговорной if (date == DateTime.Today) { foreach (DayOfBusy day in days) { day.CurrentDay = true; day.CurrentWeek = IsCurrentWeek(date); } } return(Ok(days)); }
public IActionResult GetScheduler(DateTime?startDay = null, DateTime?endDay = null) { if (startDay == null && endDay == null) { DateTime today = DateTime.Today; startDay = today.AddDays(-14); endDay = today.AddDays(14); } if (startDay == null && endDay != null) { startDay = endDay; //если 1 из параметров не задан, приравнивает его к заданному } if (startDay != null && endDay == null) { endDay = startDay; } if (startDay > endDay) { return(BadRequest("Invalid date range!")); } startDay = StartOfWeek(startDay.Value, DayOfWeek.Monday); //первый день интервала endDay = StartOfWeek(endDay.Value, DayOfWeek.Monday).AddDays(7); //последний день интервала + 1 List <ShortInfoDay> weeks = new List <ShortInfoDay>(); while (startDay < endDay) { ScheDay scheDay = GetScheDay(startDay.Value); ShortInfoDay day = new ShortInfoDay { Date = startDay.Value }; day.CurrentWeek = IsCurrentWeek(startDay.Value); if (scheDay != null && scheDay.Chunks.Count > 0) { day.CountRes = scheDay.Chunks.Count; weeks.Add(day); } else { weeks.Add(day); } startDay = startDay.Value.AddDays(1); } return(Ok(weeks)); }
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)); }
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)); }
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)); }