Ejemplo n.º 1
0
        public async Task <IActionResult> New(NewCalendarEntry model, CancellationToken cancellationToken)
        {
            if (model.Item.Start > model.Item.End)
            {
                ModelState.AddModelError("Item_End", "End date and time cannot be before start date and time.");
            }

            if ((model.Item.RecurrenceType == (int)RecurrenceTypes.Weekly ||
                 model.Item.RecurrenceType == (int)RecurrenceTypes.Monthly ||
                 model.Item.RecurrenceType == (int)RecurrenceTypes.Yearly) &&
                (model.Item.RecurrenceEnd.HasValue && (model.Item.RecurrenceEnd.Value <= model.Item.Start || model.Item.RecurrenceEnd.Value <= model.Item.End)))
            {
                ModelState.AddModelError("Item_End", "End date and time cannot be before start date and time.");
            }

            if (ModelState.IsValid)
            {
                var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId);

                model.Item.DepartmentId  = DepartmentId;
                model.Item.CreatorUserId = UserId;
                model.Item.Entities      = model.entities;

                await _calendarService.AddNewCalendarItemAsync(model.Item, department.TimeZone, cancellationToken);

                return(RedirectToAction("Index"));
            }

            model.Types = new List <CalendarItemType>();
            model.Types.Add(new CalendarItemType()
            {
                CalendarItemTypeId = 0, Name = "No Type"
            });
            model.Types.AddRange(await _calendarService.GetAllCalendarItemTypesForDepartmentAsync(DepartmentId));

            model.Item.Description = StringHelpers.StripHtmlTagsCharArray(model.Item.Description);

            return(View(model));
        }