Example #1
0
        private async void Button_Create_Click(object sender, EventArgs e)
        {
            int eventDuration = Int32.Parse(ComboBox_EventDuration.SelectedItem.ToString());

            //Check if event dates are correct
            int eventErrorCode = eventServices.isEventDatesCorrect(DateTime_EventDate.Value,
                                                                   eventDuration, DateTime_Day1.Value, DateTime_Day2.Value, DateTime_Day3.Value,
                                                                   DateTime_Day4.Value);

            //Check if event details strings are correct
            bool isEventNameCorrect   = eventServices.isStringCorrect(TextBox_EventName.Text);
            bool isVenueNameCorrect   = eventServices.isStringCorrect(TextBox_VenueName.Text);
            bool isVenueAdressCorrect = eventServices.isStringCorrect(TextBox_VenueAdress.Text);

            string emailTemplateString = "";

            if (ComboBox_EmailTemplate.SelectedIndex >= 0)
            {
                emailTemplateString = mailTemplates[ComboBox_EmailTemplate.SelectedIndex].templateName;
            }

            //Check if email template is correct
            int emailTemplateErrorCode = eventServices.isEmailTemplateCorrect(CheckBox_UseDefaultEmail.Checked, TextBox_Subject.Text,
                                                                              TextBox_Body.Text, emailTemplateString);

            //check if all From and To times are correct
            int isSomeTimeFromToNotCorrect = eventServices.isSomeTimeFromToNotCorrect(
                Int32.Parse(ComboBox_Day1FromHour.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day1FromMinute.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day1ToHour.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day1ToMinute.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day2FromHour.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day2FromMinute.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day2ToHour.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day2ToMinute.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day3FromHour.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day3FromMinute.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day3ToHour.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day3ToMinute.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day4FromHour.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day4FromMinute.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day4ToHour.SelectedItem.ToString()),
                Int32.Parse(ComboBox_Day4ToMinute.SelectedItem.ToString()),
                eventDuration
                );


            if (eventErrorCode > 0 || emailTemplateErrorCode > 0 || isSomeTimeFromToNotCorrect > 0 ||
                !isEventNameCorrect || !isVenueNameCorrect ||
                !isVenueAdressCorrect)
            {
                if (!isEventNameCorrect)
                {
                    showWarning("Event name is too short (minimum 3 letters)", "Warning");
                }
                else if (!isVenueNameCorrect)
                {
                    showWarning("Venue name is too short (minimum 3 letters)", "Warning");
                }
                else if (!isVenueAdressCorrect)
                {
                    showWarning("Venue address is too short (minimum 1 letters or number)", "Warning");
                }
                else if (eventErrorCode > 0)
                {
                    if (eventErrorCode == 5)
                    {
                        showWarning("Event duration not choosen", "Warning");
                    }
                    else if (eventErrorCode == 1)
                    {
                        showWarning("Event day " + eventErrorCode + " date is not equal to " +
                                    "event begining day",
                                    "Warning");
                    }
                    else
                    {
                        showWarning("Event day " + eventErrorCode + " date is earlier than " +
                                    "event day" + (eventErrorCode - 1) + " date",
                                    "Warning");
                    }
                }
                else if (emailTemplateErrorCode > 0)
                {
                    if (emailTemplateErrorCode == 1)
                    {
                        showWarning("Email template is not choosen", "Warning");
                    }
                    else if (emailTemplateErrorCode == 2)
                    {
                        showWarning("Email subject is too short " +
                                    "(must be at least 5 characters length)",
                                    "Warning");
                    }
                    else if (emailTemplateErrorCode == 3)
                    {
                        showWarning("Email body is too short " +
                                    "(must be at least 5 characters length)",
                                    "Warning");
                    }
                }
                else if (isSomeTimeFromToNotCorrect > 0)
                {
                    showWarning("Day " + isSomeTimeFromToNotCorrect + " time from is " +
                                "bigger or equal to time to",
                                "Warning");
                }
            }
            else
            {
                string eventStatus = eventServices.getEventStatus(DateTime_EventDate.Value,
                                                                  eventDuration);
                DateTime day1         = SetDateTimeHoursAndMinutes(DateTime_Day1.Value);
                DateTime day2         = SetDateTimeHoursAndMinutes(DateTime_Day2.Value);
                DateTime day3         = SetDateTimeHoursAndMinutes(DateTime_Day3.Value);
                DateTime day4         = SetDateTimeHoursAndMinutes(DateTime_Day4.Value);
                string   day1TimeFrom = eventServices.formHoursAndMinutesString(
                    ComboBox_Day1FromHour.SelectedItem.ToString(),
                    ComboBox_Day1FromMinute.SelectedItem.ToString());
                string day1TimeTo = eventServices.formHoursAndMinutesString(
                    ComboBox_Day1ToHour.SelectedItem.ToString(),
                    ComboBox_Day1ToMinute.SelectedItem.ToString());

                string day2TimeFrom = eventServices.formHoursAndMinutesString(
                    ComboBox_Day2FromHour.SelectedItem.ToString(),
                    ComboBox_Day2FromMinute.SelectedItem.ToString());
                string day2TimeTo = eventServices.formHoursAndMinutesString(
                    ComboBox_Day2ToHour.SelectedItem.ToString(),
                    ComboBox_Day2ToMinute.SelectedItem.ToString());

                string day3TimeFrom = eventServices.formHoursAndMinutesString(
                    ComboBox_Day3FromHour.SelectedItem.ToString(),
                    ComboBox_Day3FromMinute.SelectedItem.ToString());
                string day3TimeTo = eventServices.formHoursAndMinutesString(
                    ComboBox_Day3ToHour.SelectedItem.ToString(),
                    ComboBox_Day3ToMinute.SelectedItem.ToString());

                string day4TimeFrom = eventServices.formHoursAndMinutesString(
                    ComboBox_Day4FromHour.SelectedItem.ToString(),
                    ComboBox_Day4FromMinute.SelectedItem.ToString());
                string day4TimeTo = eventServices.formHoursAndMinutesString(
                    ComboBox_Day4ToHour.SelectedItem.ToString(),
                    ComboBox_Day4ToMinute.SelectedItem.ToString());


                string emailTemplate = "";
                string emailBody     = TextBox_Body.Text;
                string emailSubject  = TextBox_Subject.Text;
                if (CheckBox_UseDefaultEmail.Checked)
                {
                    emailTemplate = mailTemplates[ComboBox_EmailTemplate.SelectedIndex].templateName;
                    emailBody     = "";
                    emailSubject  = "";
                }

                //Adding event to database
                Event eventEntity       = null;
                bool  creationSuccesful = true;
                try
                {
                    await lastEntityIdentificationNumberService.IncreaseLastIdetificationNumber("Event");

                    eventEntity = await eventServices.addEvent(TextBox_EventName.Text, DateTime_EventDate.Value,
                                                               eventDuration, day1, day2, day3, day4, day1TimeFrom, day1TimeTo,
                                                               day2TimeFrom, day2TimeTo, day3TimeFrom, day3TimeTo, day4TimeFrom,
                                                               day4TimeTo, TextBox_WebPage.Text, TextBox_VenueName.Text, TextBox_VenueAdress.Text,
                                                               eventStatus, TextBox_Comments.Text, CheckBox_UseDefaultEmail.Checked,
                                                               emailTemplate, emailBody, emailSubject);
                }
                catch (Exception)
                {
                    creationSuccesful = false;
                }

                if (eventEntity != null && creationSuccesful)
                {
                    // checking which images to add
                    bool image1Exists = eventImagePath.ContainsKey(1);
                    if (image1Exists)
                    {
                        await AddEventImageToDatabase("1", eventEntity);
                    }
                    bool image2Exists = eventImagePath.ContainsKey(2);
                    if (image2Exists)
                    {
                        await AddEventImageToDatabase("2", eventEntity);
                    }


                    mainWindow.Enabled = true;
                    int selectedEventIndex = mainWindow.ComboBox_Events.SelectedIndex;
                    mainWindow.ComboBox_Events.Items.Clear();

                    mainWindow.events.Add(eventEntity);
                    List <Event> currentEventList = new List <Event>();
                    currentEventList.Add(eventEntity);
                    List <Event> filteredEventList = eventServices.filterEvents(currentEventList, mainWindow.lastEventFilteringYear, mainWindow.lastEventFilteringMonth,
                                                                                mainWindow.lastEventFilteringDay, mainWindow.lastEventFilteringEventName);

                    if (filteredEventList.Count > 0)
                    {
                        if (eventEntity.eventStatus.Equals(nameof(EventStatus.Ongoing)) || eventEntity.eventStatus.Equals(nameof(EventStatus.Upcoming)))
                        {
                            mainWindow.filteredEvents = mainWindow.filteredEvents.OrderByDescending(ev => ev.date_From).ToList();
                            mainWindow.emptyTable(mainWindow.Table_EventsData);
                            mainWindow.UpdateEventsTable(mainWindow.filteredEvents);
                        }
                        else
                        {
                            mainWindow.addEventTableRow();
                            mainWindow.addEventToEventTableRow(eventEntity, mainWindow.Table_EventsData.RowCount - 1);
                        }
                        foreach (Event ev in mainWindow.events)
                        {
                            mainWindow.ComboBox_Events.Items.Add(ev.eventName);
                        }
                        if (selectedEventIndex >= 0)
                        {
                            mainWindow.ComboBox_Events.SelectedIndex = mainWindow.ComboBox_Events.Items.IndexOf(mainWindow.selectedEvent.eventName);
                        }
                    }
                    mainWindow.Label_FilteredEventsAmount.Text = mainWindow.filteredEvents.Count.ToString();
                    mainWindow.Label_EventsNumberAmount.Text   = mainWindow.events.Count.ToString();
                    this.Dispose();
                }
                else
                {
                    showWarning("Event creation was unsuccesfull, because of internet connection" +
                                "or database write request number exceeded", "Warning");
                }
            }
        }