コード例 #1
0
        public ActionResult EditEntry(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.", ""));
            }

            // Check if the calendar entry exists.
            if (!_scheduleRepository.DoesCalEntryExist(vm.CalendarEntryId))
            {
                return(SiteErrorHandler.GetBadRequestActionResult("<strong>Error:</strong> The calendar event could not be found.", ""));
            }

            var entry = _mapper.Map <CalEntry>(vm);

            // Save event.
            _scheduleRepository.EditCalendarEntry(entry);

            return(Json(new
            {
                message = "<strong>Success:</strong> Calendar event saved."
            }, JsonRequestBehavior.AllowGet));
        }
コード例 #2
0
        public ActionResult DeleteTheme(int id)
        {
            // Check if the theme exists.
            if (!_themeRepository.DoesThemeExist(id))
            {
                return(SiteErrorHandler.GetBadRequestActionResult("Could not find the theme.", ""));
            }

            // Check if the theme is a system theme.
            if (_themeRepository.IsThemeReadOnly(id))
            {
                return(SiteErrorHandler.GetBadRequestActionResult($"This is a system theme and cannot be deleted.", ""));
            }

            // Check if the theme is in use.
            if (_themeRepository.IsActiveTheme(id))
            {
                return(SiteErrorHandler.GetBadRequestActionResult("Cannot delete the theme as it is currently in use.", ""));
            }

            // Delete the theme.
            _themeRepository.DeleteTheme(id);

            return(Json(new { message = "<strong>Success</strong>: The theme has been deleted." }));
        }
コード例 #3
0
        public ActionResult SaveThemeVariables(ThemeEditVariablesViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(SiteErrorHandler.GetBadRequestActionResult(ModelState));
            }

            // Check if the theme exists.
            if (!_themeRepository.DoesThemeExist(vm.ThemeId))
            {
                return(SiteErrorHandler.GetBadRequestActionResult("Could not find the theme.", ""));
            }

            // Map the variables to a readable model.
            var themeVariables = _mapper.Map <List <ThemeVariableValueModel> >(vm.Variables);
            IEnumerable <int> themeVariableValueIds = themeVariables.Select(x => x.ThemeVariableValueId);

            // Check if the theme variable values exist.
            if (!_themeRepository.DoThemeVariableValuesExist(themeVariableValueIds))
            {
                return(SiteErrorHandler.GetBadRequestActionResult("Could not find the theme variable.", ""));
            }

            // Update the theme variable values.
            _themeRepository.UpdateThemeVariableValues(themeVariables);

            return(Json(new { message = "<strong>Success</strong>: The theme variables have been updated.", themeId = vm.ThemeId }));
        }
コード例 #4
0
        public ActionResult SaveThemeChanges(ThemeViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(SiteErrorHandler.GetBadRequestActionResult(ModelState));
            }

            // Check if the theme exists.
            if (!_themeRepository.DoesThemeExist(vm.ThemeId))
            {
                return(SiteErrorHandler.GetBadRequestActionResult("Could not find the theme.", ""));
            }

            // Check if the theme name is already in use.
            if (_themeRepository.IsThemeNameAlreadyInUse(vm.ThemeId, vm.Name))
            {
                return(SiteErrorHandler.GetBadRequestActionResult($"The theme name {vm.Name} is already in use.", nameof(vm.Name)));
            }

            // Check if the theme is a system theme and has been modified.
            if (_themeRepository.IsThemeReadOnly(vm.ThemeId) && _themeRepository.HasNameBeenChanged(vm.ThemeId, vm.Name))
            {
                return(SiteErrorHandler.GetBadRequestActionResult($"This is a system theme and cannot be modified.", ""));
            }

            var theme = _mapper.Map <ThemeModel>(vm);

            _themeRepository.SaveThemeChanges(theme);

            return(Json(new { message = "<strong>Success</strong>: The theme has been updated.", themeId = vm.ThemeId }));
        }
コード例 #5
0
        // Validate the theme is ok for exporting.
        public ActionResult ValidateExportTheme(int id)
        {
            // Check if the theme exists.
            if (!_themeRepository.DoesThemeExist(id))
            {
                return(SiteErrorHandler.GetBadRequestActionResult("Could not find the theme.", ""));
            }

            return(Json(new { themeId = id }, JsonRequestBehavior.AllowGet));
        }
コード例 #6
0
        // Create a .ics file for calendar events from a date range.
        public ActionResult ExportEventsToIcal(DateTime start, DateTime end)
        {
            // Check if dates are null before doing anything.
            if (start == null || end == null)
            {
                return(SiteErrorHandler.GetBadRequestActionResult("<strong>Error:</strong> No date range provided.", ""));
            }

            // Get user's calendar entries within the date range.
            List <CalEntry> userEntries = _scheduleRepository.GetAllUserEntries(User.Identity.GetUserId(), start, end);

            // Check if there are any diary entries to sync.
            if (userEntries == null)
            {
                return(SiteErrorHandler.GetBadRequestActionResult("<strong>Error:</strong> No calendar events to sync.", ""));
            }

            // Create iCal.
            var iCal = new Ical.Net.Calendar()
            {
                ProductId = "ASP.Net Diary Scheduler",
                Version   = "2.0"
            };

            // Create a new event for each calendar entry.
            foreach (CalEntry entry in userEntries)
            {
                // Create event.
                var evt = iCal.Create <Ical.Net.CalendarComponents.CalendarEvent>();

                // Prepare ical event.
                evt.Uid         = entry.CalendarEntryId.ToString();
                evt.Start       = new Ical.Net.DataTypes.CalDateTime(entry.DateFrom);
                evt.End         = new Ical.Net.DataTypes.CalDateTime(entry.DateTo);
                evt.Description = entry.Description;
                evt.Summary     = entry.Title;
                evt.IsAllDay    = entry.AllDay;
            }

            // Build the .ics file.
            Ical.Net.Serialization.SerializationContext ctx = new Ical.Net.Serialization.SerializationContext();
            var serialiser = new Ical.Net.Serialization.CalendarSerializer(ctx);

            string output      = serialiser.SerializeToString(iCal);
            string contentType = "text/calendar";
            var    bytes       = System.Text.Encoding.UTF8.GetBytes(output);

            // Create a name.
            Guid   fileId   = Guid.NewGuid();
            string fileName = fileId.ToString() + ".ics";

            return(File(bytes, contentType, fileName));
        }
コード例 #7
0
        public ActionResult ImportThemeFromFile(ImportThemeViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(SiteErrorHandler.GetBadRequestActionResult(ModelState));
            }

            // Check if the theme name is already in use.
            if (_themeRepository.IsThemeNameAlreadyInUse(0, vm.Name))
            {
                return(SiteErrorHandler.GetBadRequestActionResult($"The theme name {vm.Name} is already in use.", nameof(vm.Name)));
            }

            // Check if the file type is correct.
            if (vm.FileToUse.ContentType != "application/json")
            {
                return(SiteErrorHandler.GetBadRequestActionResult("File must be .json", ""));
            }

            // Check if the file has any content.
            if (vm.FileToUse.ContentLength == 0)
            {
                return(SiteErrorHandler.GetBadRequestActionResult("File is empty", ""));
            }

            List <ThemeVariableValueModel> variables;

            // Read the file and convert the json to a theme variable value collection.
            try
            {
                var    b       = new System.IO.BinaryReader(vm.FileToUse.InputStream);
                byte[] binData = b.ReadBytes(vm.FileToUse.ContentLength);
                string result  = System.Text.Encoding.UTF8.GetString(binData);
                variables = _themeService.ConvertJsonToThemeVariableValues(result);
            }
            catch (Exception ex)
            {
                return(SiteErrorHandler.GetBadRequestActionResult(ex.Message, ""));
            }

            // Check if there are any values inside the file.
            if (variables == null || !variables.Any())
            {
                return(SiteErrorHandler.GetBadRequestActionResult("Imported theme did not have any values", ""));
            }

            // Import the theme.
            int themeId = _themeRepository.ImportTheme(vm.Name, variables);

            return(Json(new { message = "<strong>Success</strong>: Theme has been created.", themeId }));
        }
コード例 #8
0
        public ActionResult DeleteEntry(Guid id)
        {
            // Check if the calendar entry exists.
            if (!_scheduleRepository.DoesCalEntryExist(id))
            {
                return(SiteErrorHandler.GetBadRequestActionResult("<strong>Error:</strong> The calendar event could not be found.", ""));
            }

            // Delete event.
            _scheduleRepository.DeleteCalendarEntry(id);

            return(Json(new
            {
                message = "<strong>Success:</strong> Calendar entry deleted."
            }, JsonRequestBehavior.AllowGet));
        }
コード例 #9
0
        // Create a .ics file for a calendar event.
        public ActionResult ExportEventToIcal(Guid id)
        {
            // Check if an id was sent.
            if (id == Guid.Empty)
            {
                return(SiteErrorHandler.GetBadRequestActionResult("<strong>Error:</strong> Invalid calendar event id.", ""));
            }

            var entry = _scheduleRepository.GetCalendarEntry(id);

            if (entry == null)
            {
                return(SiteErrorHandler.GetBadRequestActionResult("<strong>Error:</strong> The calendar event could not be found.", ""));
            }

            // Create iCal.
            var iCal = new Ical.Net.Calendar()
            {
                ProductId = "ASP.Net Diary Scheduler",
                Version   = "2.0"
            };

            // Create event.
            var evt = iCal.Create <Ical.Net.CalendarComponents.CalendarEvent>();

            // Prepare ical event.
            evt.Uid         = entry.CalendarEntryId.ToString();
            evt.Start       = new Ical.Net.DataTypes.CalDateTime(entry.DateFrom);
            evt.End         = new Ical.Net.DataTypes.CalDateTime(entry.DateTo);
            evt.Description = entry.Description;
            evt.Summary     = entry.Title;
            evt.IsAllDay    = entry.AllDay;

            // Build the .ics file.
            var ctx        = new Ical.Net.Serialization.SerializationContext();
            var serialiser = new Ical.Net.Serialization.CalendarSerializer(ctx);

            string output      = serialiser.SerializeToString(iCal);
            string contentType = "text/calendar";
            var    bytes       = System.Text.Encoding.UTF8.GetBytes(output);

            // Create a name.
            Guid   fileId   = Guid.NewGuid();
            string fileName = fileId.ToString() + ".ics";

            return(File(bytes, contentType, fileName));
        }
コード例 #10
0
        public ActionResult SaveNewTheme(CreateThemeViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(SiteErrorHandler.GetBadRequestActionResult(ModelState));
            }

            // Check if the theme name is already in use.
            if (_themeRepository.IsThemeNameAlreadyInUse(vm.ThemeId, vm.Name))
            {
                return(SiteErrorHandler.GetBadRequestActionResult($"The theme name {vm.Name} is already in use.", nameof(vm.Name)));
            }

            var theme = _mapper.Map <ThemeModel>(vm);
            int id    = _themeRepository.CreateTheme(theme, vm.SelectedThemeId);

            return(Json(new { message = "<strong>Success</strong>: The theme has been created.", themeId = id }));
        }
コード例 #11
0
        // GET: Edit event view.
        public ActionResult Edit(Guid id)
        {
            // Check if an id was sent.
            if (id == Guid.Empty)
            {
                return(SiteErrorHandler.GetBadRequestActionResult("<strong>Error:</strong> Invalid calendar event id.", ""));
            }

            var entry = _scheduleRepository.GetCalendarEntry(id);

            if (entry == null)
            {
                return(SiteErrorHandler.GetBadRequestActionResult("<strong>Error:</strong> The calendar event could not be found.", ""));
            }

            var vm = _mapper.Map <CalendarEventViewModel>(entry);

            return(View(vm));
        }
コード例 #12
0
        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));
        }