private HolidayManager(DSModel db) : base(db) { this.ActiveModel = HolidayRepository.GetHolidays(db); }
public List <HolidayModel> GetHolidays() { return(HolidayRepository.GetHolidays(this.DbContext)); }
// POST api/Task public HttpResponseMessage PostTask(EventViewModel task) { //var httpContext = (HttpContextWrapper)Request.Properties["MS_HttpContext"]; //var models = httpContext.Request.Form["models"]; //var task = JsonConvert.DeserializeObject<EventViewModel>(models); if (ModelState.IsValid) { var eventTaskList = db.EventTaskLists.Include("Property").Include("Crew").FirstOrDefault(e => e.Id == task.OwnerID); if (eventTaskList != null) { task.Title = eventTaskList.Property.Name + ", " + eventTaskList.Crew.Name; } List <DateTime> holidays; using (var holidayRepo = new HolidayRepository()) { holidays = holidayRepo.GetHolidays().Select(h => h.HolidayDate.Date).ToList(); } if (holidays.Contains(task.StartDate.Date)) { var message = "Your schedule includes a holiday on: " + task.StartDate.Date; HttpError err = new HttpError(message); return(Request.CreateResponse(HttpStatusCode.Conflict, err)); } if (!string.IsNullOrEmpty(task.RecurrenceRule)) { var pattern = new RecurrencePattern(task.RecurrenceRule); var evaluator = new RecurrencePatternEvaluator(pattern); var items = evaluator.Evaluate(new CalDateTime(task.StartDate), task.Start, task.StartDate.AddDays(1), false); var allItems = evaluator.Evaluate(new CalDateTime(task.StartDate), task.Start, task.End.AddYears(5), true); //Adding 5 years since the end date is equal to the start date. The recurrence rule handles our occurences var holiday = allItems.FirstOrDefault(i => holidays.Contains(i.StartTime.Date)); if (holiday != null) { var message = "Your schedule includes a holiday on: " + holiday.StartTime.Date; HttpError err = new HttpError(message); return(Request.CreateResponse(HttpStatusCode.Conflict, err)); } if (!items.Any()) { var startEntity = new EventSchedule { Id = task.TaskID, Title = task.Title, StartTime = task.Start, StartTimezone = task.StartTimezone, EndTime = task.End, EndTimezone = task.EndTimezone, Description = task.Description, RecurrenceRule = string.Empty, RecurrenceException = string.Empty, RecurrenceID = null, IsAllDay = task.IsAllDay, EventTaskListId = task.OwnerID ?? 0 }; db.EventSchedules.Add(startEntity); } } var entity = new EventSchedule { Id = task.TaskID, Title = task.Title, StartTime = task.Start, StartTimezone = task.StartTimezone, EndTime = task.End, EndTimezone = task.EndTimezone, Description = task.Description, RecurrenceRule = task.RecurrenceRule, RecurrenceException = task.RecurrenceException, RecurrenceID = task.RecurrenceID, IsAllDay = task.IsAllDay, EventTaskListId = task.OwnerID ?? 0 }; db.EventSchedules.Add(entity); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, new[] { task }); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = task.TaskID })); return(response); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } }