// Add new calendar entry. public Guid CreateCalendarEntry(CalEntry entry) { var mappedEntry = _mapper.Map <CalendarEntry>(entry); _context.CalendarEntries.Attach(mappedEntry); _context.Entry(mappedEntry).State = EntityState.Added; SaveChanges(); return(mappedEntry.CalendarEntryId); }
public ActionResult CreateEntry(CalendarEventViewModel vm) { if (!ModelState.IsValid) { return(SiteErrorHandler.GetBadRequestActionResult("<strong>Error:</strong> Input is invalid.", "")); } // Date range check. if (vm.DateFrom > vm.DateTo) { return(SiteErrorHandler.GetBadRequestActionResult("<strong>Error:</strong> Start date cannot be after the end date.", "")); } var entry = new CalEntry() { Title = vm.Title.Trim(), Description = vm.Description == null ? null : vm.Description.Trim(), DateFrom = vm.DateFrom, DateTo = vm.DateTo, AllDay = vm.AllDay, UserId = User.Identity.GetUserId() }; // Save event. var id = _scheduleRepository.CreateCalendarEntry(entry); // Return calendar record for fullCalendar.js. return(Json(new { message = "<strong>Success:</strong> Calendar event created.", calEntry = new { id, title = entry.Title, start = entry.DateFrom, end = entry.DateTo, allDay = entry.AllDay } }, JsonRequestBehavior.AllowGet)); }
// Edit an existing calendar entry. public void EditCalendarEntry(CalEntry entry) { // Get the original entry. var originalEntry = _context.CalendarEntries.FirstOrDefault(x => x.CalendarEntryId == entry.CalendarEntryId); // Double check the entry exists. if (originalEntry == null) { throw new Exception("The calendar entry could not be found."); } // Update values. originalEntry.Title = entry.Title; originalEntry.Description = entry.Description; originalEntry.DateFrom = entry.DateFrom; originalEntry.DateTo = entry.DateTo; originalEntry.AllDay = entry.AllDay; // Save changes. _context.Entry(originalEntry).State = EntityState.Modified; SaveChanges(); }