public async Task <EventAndAppointment> UpdateEventAndAppointment(EventAndAppointment eventAndAppoint)
        {
            try
            {
                db.EventAndAppointments.AddOrUpdate(eventAndAppoint);
                await db.SaveChangesAsync();

                return(eventAndAppoint);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public async Task DeleteEventAndAppointmentById(int id)
        {
            try
            {
                EventAndAppointment evnt = await GetEventAndAppointmentById(id);

                db.EventAndAppointments.Attach(evnt);
                db.EventAndAppointments.Remove(evnt);
                await db.SaveChangesAsync();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public async Task AddData()
        {
            // create contacts
            List <Contact> contacts     = new List <Contact>();
            ContactModel   contactModel = new ContactModel(db);

            Contact ct1 = new Contact();

            ct1.FirstName = "Kamal";
            ct1.LastName  = "Perera";
            ct1.Address   = "Ragama";
            ct1.Email     = "*****@*****.**";
            ct1.Other     = "";
            ct1.Phone1    = "0777227373";
            ct1.Phone2    = "";
            contacts.Add(ct1);

            Contact ct2 = new Contact();

            ct2.FirstName = "Namal";
            ct2.LastName  = "Jayasinghe";
            ct2.Address   = "Gampaha";
            ct2.Email     = "*****@*****.**";
            ct2.Other     = "";
            ct2.Phone1    = "0717227355";
            ct2.Phone2    = "";
            contacts.Add(ct2);

            Contact ct3 = new Contact();

            ct3.FirstName = "Sumith";
            ct3.LastName  = "Ranaweera";
            ct3.Address   = "Maharagama";
            ct3.Email     = "*****@*****.**";
            ct3.Other     = "";
            ct3.Phone1    = "0727265344";
            ct3.Phone2    = "";
            contacts.Add(ct3);

            await contactModel.SaveContacts(contacts);

            //create categories
            CategoryModel   categoryModel = new CategoryModel(db);
            List <Category> categories    = new List <Category>();
            Category        C1            = new Category();

            C1.Name = "Salary";
            C1.Type = "Income";
            C1.IsSpecialOccassion = false;
            C1.CreatedAt          = new DateTime(2021, 3, 20, 14, 0, 0);
            categories.Add(C1);

            Category C2 = new Category();

            C2.Name = "Telephone Bill";
            C2.Type = "Expense";
            C2.IsSpecialOccassion = false;
            C2.CreatedAt          = new DateTime(2021, 3, 20, 14, 0, 0);
            categories.Add(C2);

            Category C3 = new Category();

            C3.Name = "Party";
            C3.Type = "Expense";
            C3.IsSpecialOccassion = true;
            C3.CreatedAt          = new DateTime(2021, 3, 20, 14, 0, 0);
            categories.Add(C3);

            Category C4 = new Category();

            C4.Name = "Tussion Fee";
            C4.Type = "Expense";
            C4.IsSpecialOccassion = false;
            C4.CreatedAt          = new DateTime(2021, 3, 20, 14, 0, 0);
            categories.Add(C4);

            Category C5 = new Category();

            C5.Name = "Light Bill";
            C5.Type = "Expense";
            C5.IsSpecialOccassion = false;
            C5.CreatedAt          = new DateTime(2021, 3, 20, 14, 0, 0);
            categories.Add(C5);

            await categoryModel.SaveCategories(categories);


            //create recursive items
            RecursiveItemModel   recursiveItemModel = new RecursiveItemModel(db);
            List <RecursiveItem> recursiveItems     = new List <RecursiveItem>();
            RecursiveItem        ri1 = new RecursiveItem();

            ri1.Type   = "Transaction";
            ri1.Every  = 1;
            ri1.Option = "Month";
            ri1.Day    = 20;
            recursiveItems.Add(ri1);
            await recursiveItemModel.SaveRecursiveItems(recursiveItems);


            //create transactions
            TransactionModel   transactionModel = new TransactionModel(db);
            List <Transaction> transactions     = new List <Transaction>();
            Transaction        tj = new Transaction();

            tj.Type            = "Income";
            tj.PaymentType     = "Cash";
            tj.Date            = new DateTime(2021, 1, 20, 14, 0, 0);
            tj.Amount          = 100000.00;
            tj.Description     = "March salary";
            tj.IsRepeat        = true;
            tj.CategoryId      = C1.Id;
            tj.ContactId       = null;
            tj.RecursiveItemId = ri1.Id;
            transactions.Add(tj);

            Transaction tj2 = new Transaction();

            tj2.Type            = "Expense";
            tj2.PaymentType     = "Cash";
            tj2.Date            = new DateTime(2021, 1, 21, 08, 0, 0);
            tj2.Amount          = 4200;
            tj2.IsRepeat        = true;
            tj2.CategoryId      = C2.Id;
            tj2.ContactId       = ct1.Id;
            tj2.RecursiveItemId = ri1.Id;
            transactions.Add(tj2);

            Transaction t21 = new Transaction();

            t21.Type            = "Income";
            t21.PaymentType     = "Cash";
            t21.Date            = new DateTime(2021, 2, 22, 14, 0, 0);
            t21.Amount          = 100000.00;
            t21.Description     = "March salary";
            t21.IsRepeat        = true;
            t21.CategoryId      = C1.Id;
            t21.ContactId       = null;
            t21.RecursiveItemId = ri1.Id;
            transactions.Add(t21);

            Transaction t22 = new Transaction();

            t22.Type            = "Expense";
            t22.PaymentType     = "Cash";
            t22.Date            = new DateTime(2021, 2, 23, 08, 0, 0);
            t22.Amount          = 4200;
            t22.IsRepeat        = true;
            t22.CategoryId      = C2.Id;
            t22.ContactId       = ct1.Id;
            t22.RecursiveItemId = ri1.Id;
            transactions.Add(t22);

            Transaction t23 = new Transaction();

            t23.Type        = "Expense";
            t23.PaymentType = "Cash";
            t23.Date        = new DateTime(2021, 2, 24, 12, 0, 0);
            t23.Amount      = 5000.00;
            t23.IsRepeat    = false;
            t23.CategoryId  = C3.Id;
            t23.ContactId   = null;
            transactions.Add(t23);

            Transaction t24 = new Transaction();

            t24.Type            = "Expense";
            t24.PaymentType     = "Cash";
            t24.Date            = new DateTime(2021, 2, 24, 08, 30, 0);
            t24.Amount          = 10000.00;
            t24.IsRepeat        = true;
            t24.CategoryId      = C4.Id;
            t24.ContactId       = ct3.Id;
            t24.RecursiveItemId = ri1.Id;
            transactions.Add(t24);

            Transaction t25 = new Transaction();

            t25.Type        = "Expense";
            t25.PaymentType = "Cash";
            t25.Date        = new DateTime(2021, 2, 24, 09, 30, 0);
            t25.Amount      = 5500.00;
            t25.IsRepeat    = false;
            t25.CategoryId  = C3.Id;
            t25.ContactId   = null;
            transactions.Add(t25);

            Transaction t26 = new Transaction();

            t26.Type            = "Expense";
            t26.PaymentType     = "Cash";
            t26.Date            = new DateTime(2021, 2, 27, 11, 14, 0);
            t26.Amount          = 2000.00;
            t26.IsRepeat        = true;
            t26.CategoryId      = C5.Id;
            t26.ContactId       = null;
            t26.RecursiveItemId = ri1.Id;
            transactions.Add(t26);


            Transaction t1 = new Transaction();

            t1.Type            = "Income";
            t1.PaymentType     = "Cash";
            t1.Date            = new DateTime(2021, 3, 22, 14, 0, 0);
            t1.Amount          = 100000.00;
            t1.Description     = "March salary";
            t1.IsRepeat        = true;
            t1.CategoryId      = C1.Id;
            t1.ContactId       = null;
            t1.RecursiveItemId = ri1.Id;
            transactions.Add(t1);

            Transaction t2 = new Transaction();

            t2.Type            = "Expense";
            t2.PaymentType     = "Cash";
            t2.Date            = new DateTime(2021, 3, 23, 08, 0, 0);
            t2.Amount          = 4200;
            t2.IsRepeat        = true;
            t2.CategoryId      = C2.Id;
            t2.ContactId       = ct1.Id;
            t2.RecursiveItemId = ri1.Id;
            transactions.Add(t2);

            Transaction t3 = new Transaction();

            t3.Type        = "Expense";
            t3.PaymentType = "Cash";
            t3.Date        = new DateTime(2021, 3, 24, 12, 0, 0);
            t3.Amount      = 5000.00;
            t3.IsRepeat    = false;
            t3.CategoryId  = C3.Id;
            t3.ContactId   = null;
            transactions.Add(t3);

            Transaction t4 = new Transaction();

            t4.Type            = "Expense";
            t4.PaymentType     = "Cash";
            t4.Date            = new DateTime(2021, 3, 24, 08, 30, 0);
            t4.Amount          = 10000.00;
            t4.IsRepeat        = true;
            t4.CategoryId      = C4.Id;
            t4.ContactId       = ct3.Id;
            t4.RecursiveItemId = ri1.Id;
            transactions.Add(t4);

            Transaction t5 = new Transaction();

            t5.Type        = "Expense";
            t5.PaymentType = "Cash";
            t5.Date        = new DateTime(2021, 3, 24, 09, 30, 0);
            t5.Amount      = 5500.00;
            t5.IsRepeat    = false;
            t5.CategoryId  = C3.Id;
            t5.ContactId   = null;
            transactions.Add(t5);

            Transaction t6 = new Transaction();

            t6.Type            = "Expense";
            t6.PaymentType     = "Cash";
            t6.Date            = new DateTime(2021, 3, 26, 11, 14, 0);
            t6.Amount          = 2000.00;
            t6.IsRepeat        = true;
            t6.CategoryId      = C5.Id;
            t6.ContactId       = null;
            t6.RecursiveItemId = ri1.Id;
            transactions.Add(t6);

            Transaction t7 = new Transaction();

            t7.Type            = "Expense";
            t7.PaymentType     = "Cash";
            t7.Date            = new DateTime(2021, 4, 3, 13, 26, 0);
            t7.Amount          = 10000.00;
            t7.IsRepeat        = true;
            t7.CategoryId      = C4.Id;
            t7.ContactId       = ct3.Id;
            t7.RecursiveItemId = ri1.Id;
            transactions.Add(t7);

            Transaction t8 = new Transaction();

            t8.Type        = "Expense";
            t8.PaymentType = "Cash";
            t8.Date        = new DateTime(2021, 4, 4, 10, 45, 0);
            t8.Amount      = 6000.00;
            t8.IsRepeat    = false;
            t8.CategoryId  = C3.Id;
            t8.ContactId   = null;
            transactions.Add(t8);

            Transaction t9 = new Transaction();

            t9.Type            = "Income";
            t9.PaymentType     = "Cash";
            t9.Date            = new DateTime(2021, 4, 5, 10, 15, 0);
            t9.Amount          = 100000.00;
            t9.Description     = "April Salary";
            t9.IsRepeat        = true;
            t9.CategoryId      = C1.Id;
            t9.ContactId       = null;
            t9.RecursiveItemId = ri1.Id;
            transactions.Add(t9);

            Transaction t12 = new Transaction();

            t12.Type            = "Expense";
            t12.PaymentType     = "Cash";
            t12.Date            = new DateTime(2021, 4, 7, 11, 14, 0);
            t12.Amount          = 2000.00;
            t12.IsRepeat        = true;
            t12.CategoryId      = C5.Id;
            t12.ContactId       = null;
            t12.RecursiveItemId = ri1.Id;
            transactions.Add(t12);

            Transaction t10 = new Transaction();

            t10.Type            = "Expense";
            t10.PaymentType     = "Cash";
            t10.Date            = DateTime.Now;
            t10.Amount          = 4100;
            t10.IsRepeat        = true;
            t10.CategoryId      = C2.Id;
            t10.ContactId       = null;
            t10.RecursiveItemId = ri1.Id;
            transactions.Add(t10);

            Transaction t11 = new Transaction();

            t11.Type            = "Expense";
            t11.PaymentType     = "Cash";
            t11.Date            = DateTime.Now;
            t11.Amount          = 10000.00;
            t11.IsRepeat        = true;
            t11.CategoryId      = C4.Id;
            t11.ContactId       = ct2.Id;
            t11.RecursiveItemId = ri1.Id;
            transactions.Add(t11);

            Transaction t13 = new Transaction();

            t13.Type            = "Income";
            t13.PaymentType     = "Cash";
            t13.Date            = DateTime.Now;
            t13.Amount          = 100000.00;
            t13.Description     = "May Salary";
            t13.IsRepeat        = true;
            t13.CategoryId      = C1.Id;
            t13.ContactId       = null;
            t13.RecursiveItemId = ri1.Id;
            transactions.Add(t13);


            await transactionModel.SaveTransactions(transactions);


            //add events and appointments
            EventAndAppointmentModel   eventAndAppointmentModel = new EventAndAppointmentModel(db);
            List <EventAndAppointment> events = new List <EventAndAppointment>();
            EventAndAppointment        e1     = new EventAndAppointment();

            e1.Name        = "Family doctor appointment";
            e1.Description = "Daughter has to take medicines";
            e1.Date        = DateTime.Now.AddMinutes(15);
            e1.IsRepeat    = false;
            events.Add(e1);

            EventAndAppointment e2 = new EventAndAppointment();

            e2.Name        = "Client meeting";
            e2.Description = "Wireless project";
            e2.Date        = DateTime.Now.AddHours(2);
            e2.IsRepeat    = false;
            events.Add(e2);

            EventAndAppointment e3 = new EventAndAppointment();

            e3.Name        = "Dinner out";
            e3.Description = "Great wall restaurant";
            e3.Date        = DateTime.Now.AddDays(1);
            e3.IsRepeat    = false;
            events.Add(e3);

            EventAndAppointment e4 = new EventAndAppointment();

            e4.Name        = "Birthday party";
            e4.Description = "Kamal's birthday party";
            e4.Date        = DateTime.Now.AddDays(2);
            e4.IsRepeat    = false;
            events.Add(e4);

            EventAndAppointment e5 = new EventAndAppointment();

            e5.Name        = "Client meeting";
            e5.Description = "Tenaga project meeting";
            e5.Date        = DateTime.Now.AddHours(4);
            e5.IsRepeat    = false;
            events.Add(e5);

            await eventAndAppointmentModel.SaveEventAndAppointments(events);
        }
        private async void BtnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                bool nameValid = false;
                if (string.IsNullOrEmpty(txtName.Text.ToString()))
                {
                    errorProvider1.SetError(txtName, "Name required");
                }
                else
                {
                    nameValid = true;
                    errorProvider1.SetError(txtName, "");
                }

                if (!nameValid)
                {
                    return;
                }

                EventAndAppointment curentEvent = new EventAndAppointment();

                curentEvent.Name        = txtName.Text;
                curentEvent.Description = txtDescription.Text;
                curentEvent.Date        = dtpEvent.Value;

                RecursiveItem recItem = null;
                if (repeatToggle.Checked)
                {
                    recItem = new RecursiveItem();
                    curentEvent.IsRepeat = true;
                    recItem.Type         = "Event";
                    recItem.Every        = int.Parse(cbRepeatValues.SelectedItem.ToString());
                    recItem.Option       = cbRepeatValuesType.SelectedItem.ToString();
                    if (recItem.Option.Equals("Month"))
                    {
                        recItem.Day = dtpRepeat.Value.Day;
                    }
                }
                else
                {
                    curentEvent.IsRepeat = false;
                }

                EventAndAppointmentModel eventAndAppointmentModel = new EventAndAppointmentModel(db);
                if (editingEventAndAppointment == null)
                {
                    if (repeatToggle.Checked)
                    {
                        RecursiveItemModel recursiveItemModel = new RecursiveItemModel(db);
                        await recursiveItemModel.SaveRecursiveItem(recItem);

                        curentEvent.RecursiveItemId = recItem.Id;
                    }
                    await eventAndAppointmentModel.SaveEventAndAppointment(curentEvent);

                    MessageBox.Show("Successfully created.");
                }
                else
                {
                    curentEvent.Id = editingEventAndAppointment.Id;
                    await eventAndAppointmentModel.UpdateEventAndAppointment(curentEvent);

                    MessageBox.Show("Successfully updated.");
                }

                eventAndAppointment.HandleClose();
                this.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("Failed to add event");
            }
        }