예제 #1
0
        /// <summary>
        /// Validate event details model.
        /// </summary>
        /// <param name="eventDetails">Event details which needs to be saved.</param>
        /// <param name="isUpdate">Set whether an event to be updated</param>
        /// <returns>Error message if any or null.</returns>
        public static List <string> ValidateEventModel(EventEntity eventDetails, bool isUpdate = false)
        {
            const short EventVenueMaxLetters       = 200;
            const short EventDescriptionMaxLetters = 1000;

            eventDetails = eventDetails ?? throw new ArgumentNullException(nameof(eventDetails), "Event details is null");

            List <string> validationMessages = new List <string>();

            // Description validation
            if (string.IsNullOrEmpty(eventDetails.Description))
            {
                validationMessages.Add("Event description is required");
            }
            else if (eventDetails.Description.Length > EventDescriptionMaxLetters)
            {
                validationMessages.Add($"Event description length cannot be more than {EventDescriptionMaxLetters} letters");
            }

            // Photo URL validation
            if (string.IsNullOrEmpty(eventDetails.Photo))
            {
                validationMessages.Add("Event photo URL is required");
            }

            bool result = Uri.TryCreate(eventDetails.Photo, UriKind.Absolute, out Uri uriResult) &&
                          (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);

            if (!result)
            {
                validationMessages.Add("Event photo URL must be in valid URL format");
            }

            // Start date validation
            if (eventDetails.StartDate == null)
            {
                validationMessages.Add("Event start date is required");
            }
            else if (!(isUpdate && eventDetails.StartDate < DateTime.UtcNow))
            {
                if (eventDetails.StartDate < DateTime.UtcNow.AddDays(1).Date)
                {
                    validationMessages.Add("Event start date must be future date.");
                }

                // Event time validation
                if (eventDetails.StartTime == null)
                {
                    validationMessages.Add("Event start time is required");
                }
                else if (eventDetails.EndTime == null)
                {
                    validationMessages.Add("Event end time is required");
                }
                else
                {
                    if (eventDetails.EndTime < eventDetails.StartTime)
                    {
                        validationMessages.Add("Event end time must be greater than start time");
                    }
                }
            }

            // Event type validation
            if (eventDetails.Type < (int)EventType.InPerson || eventDetails.Type > (int)EventType.LiveEvent)
            {
                validationMessages.Add("Invalid event type value. Event type should be in-between 1 to 3");
            }

            // Meeting link validation
            if (eventDetails.Type == (int)EventType.LiveEvent)
            {
                if (string.IsNullOrEmpty(eventDetails.MeetingLink))
                {
                    validationMessages.Add("Meeting link is required");
                }

                result = Uri.TryCreate(eventDetails.MeetingLink, UriKind.Absolute, out uriResult) &&
                         (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);

                if (!result)
                {
                    validationMessages.Add("Meeting URL must be in valid URL format");
                }
            }

            // Venue validation
            if (eventDetails.Type == (int)EventType.InPerson)
            {
                if (string.IsNullOrEmpty(eventDetails.Venue))
                {
                    validationMessages.Add("Meeting link is required");
                }

                if (eventDetails.Venue.Length > EventVenueMaxLetters)
                {
                    validationMessages.Add($"Event venue cannot be more than {EventVenueMaxLetters} letters");
                }
            }

            // Event category validation
            if (string.IsNullOrEmpty(eventDetails.CategoryId))
            {
                validationMessages.Add("Event category is required");
            }

            // Max participants validation
            if (eventDetails.MaximumNumberOfParticipants < 1)
            {
                validationMessages.Add($"Invalid {nameof(eventDetails.MaximumNumberOfParticipants)} value.");
            }

            // Audience validation
            if (eventDetails.Audience < (int)EventAudience.Public || eventDetails.Audience > (int)EventAudience.Private)
            {
                validationMessages.Add($"Invalid {nameof(eventDetails.Audience)} value. It should be either {(int)EventAudience.Public} or {(int)EventAudience.Private}");
            }

            return(validationMessages);
        }
        /// <summary>
        /// Validate event details model.
        /// </summary>
        /// <param name="eventDetails">Event details which needs to be saved.</param>
        /// <param name="localizer">The current culture's string localizer.</param>
        /// <param name="isUpdate">Set whether an event to be updated</param>
        /// <returns>Error message if any or null.</returns>
        public static List <string> ValidateEventModel(EventEntity eventDetails, IStringLocalizer <Strings> localizer, bool isUpdate = false)
        {
            const short EventVenueMaxLetters       = 200;
            const short EventDescriptionMaxLetters = 1000;

            eventDetails = eventDetails ?? throw new ArgumentNullException(nameof(eventDetails), "Event details is null");

            Uri           uriResult;
            List <string> validationMessages = new List <string>();

            if (string.IsNullOrEmpty(eventDetails.Name))
            {
                validationMessages.Add(localizer.GetString("ErrorEventNameRequired"));
            }

            // Description validation
            if (string.IsNullOrEmpty(eventDetails.Description))
            {
                validationMessages.Add(localizer.GetString("ErrorEventDescriptionRequired"));
            }
            else if (eventDetails.Description.Length > EventDescriptionMaxLetters)
            {
                validationMessages.Add(localizer.GetString("ErrorEventDescriptionLength", EventDescriptionMaxLetters));
            }

            // Either image URL or color is required.
            if (string.IsNullOrEmpty(eventDetails.Photo))
            {
                if (string.IsNullOrEmpty(eventDetails.SelectedColor))
                {
                    validationMessages.Add(localizer.GetString("ErrorEventPhotoOrColorRequired"));
                }
            }

            // If user selected image then validate URL.
            if (!string.IsNullOrEmpty(eventDetails.Photo))
            {
                bool result = Uri.TryCreate(eventDetails.Photo, UriKind.Absolute, out uriResult) &&
                              (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
                if (!result)
                {
                    validationMessages.Add(localizer.GetString("ErrorEventPhotoURLInvalid"));
                }
            }

            // Start date validation
            if (eventDetails.StartDate == null)
            {
                validationMessages.Add(localizer.GetString("ErrorEventStartDateRequired"));
            }
            else if (!(isUpdate && eventDetails.StartDate < DateTime.UtcNow))
            {
                if (eventDetails.StartDate < DateTime.UtcNow.AddDays(1).Date)
                {
                    validationMessages.Add(localizer.GetString("ErrorEventStartDateInvalid"));
                }

                // Event time validation
                if (eventDetails.StartTime == null)
                {
                    validationMessages.Add(localizer.GetString("ErrorEventStartTimeRequired"));
                }
                else if (eventDetails.EndTime == null)
                {
                    validationMessages.Add(localizer.GetString("ErrorEventEndTimeRequired"));
                }
                else
                {
                    if (eventDetails.EndTime < eventDetails.StartTime)
                    {
                        validationMessages.Add(localizer.GetString("ErrorEventEndTimeInvalid"));
                    }
                }
            }

            // Event type validation
            if (eventDetails.Type < (int)EventType.InPerson || eventDetails.Type > (int)EventType.LiveEvent)
            {
                validationMessages.Add(localizer.GetString("ErrorEventTypeInvalid"));
            }

            // Meeting link validation
            if (eventDetails.Type == (int)EventType.LiveEvent)
            {
                if (string.IsNullOrEmpty(eventDetails.MeetingLink))
                {
                    validationMessages.Add(localizer.GetString("ErrorMeetingLinkRequired"));
                }

                var result = Uri.TryCreate(eventDetails.MeetingLink, UriKind.Absolute, out uriResult) &&
                             (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);

                if (!result)
                {
                    validationMessages.Add(localizer.GetString("ErrorMeetingLinkInvalid"));
                }
            }

            // Venue validation
            if (eventDetails.Type == (int)EventType.InPerson)
            {
                if (string.IsNullOrEmpty(eventDetails.Venue))
                {
                    validationMessages.Add(localizer.GetString("ErrorEventVenueRequired"));
                }

                if (eventDetails.Venue.Length > EventVenueMaxLetters)
                {
                    validationMessages.Add(localizer.GetString("ErrorEventVenueInvalid", EventVenueMaxLetters));
                }
            }

            // Event category validation
            if (string.IsNullOrEmpty(eventDetails.CategoryId))
            {
                validationMessages.Add(localizer.GetString("ErrorEventCategoryRequired"));
            }

            // Max participants validation
            if (eventDetails.MaximumNumberOfParticipants < 1)
            {
                validationMessages.Add(localizer.GetString("ErrorEventMaxParticipantsInvalid"));
            }

            // Audience validation
            if (eventDetails.Audience < (int)EventAudience.Public || eventDetails.Audience > (int)EventAudience.Private)
            {
                validationMessages.Add(localizer.GetString("ErrorEventAudienceInvalid"));
            }

            return(validationMessages);
        }