private void AddNewClient(object sender, EventArgs e)
 {
     using (var ctx = new PetRentalContext()) {
         if (ClientName.Text.Length > 15 || ClientSurname.Text.Length > 20)
         {
             MessageBox.Show("Name or Surname is too long !");
         }
         else
         {
             var date      = ClientDateOfBirth.SelectedDate;
             var newClient = new Client()
             {
                 Name             = ClientName.Text.Trim(),
                 Surname          = ClientSurname.Text.Trim(),
                 DateOfBirth      = DateTime.Parse(date.Value.ToString()),
                 RegistrationDate = DateTime.Now
             };
             ctx.Clients.Add(newClient);
             int result = ctx.SaveChanges();
             if (result == 1)
             {
                 MessageBox.Show("Client added !");
             }
         }
     }
 }
        private void ChangeValue(object sender, RoutedEventArgs e)
        {
            string inputClientId = ClientIdInput.Text.Trim();
            int    id;
            var    value         = ValueToChange.SelectedValue as ListBoxItem;
            string selectedValue = value.Content as string;

            if (int.TryParse(inputClientId, out id))
            {
                using (var ctx = new PetRentalContext()) {
                    var client = ctx.Clients
                                 .Where(a => a.Id == id)
                                 .ToList();
                    if (client.Count == 0)
                    {
                        MessageBox.Show("Client not found !");
                    }
                    else
                    {
                        string inputData = InputNewClientValue.Text.Trim();
                        if (selectedValue == "Name")
                        {
                            client[0].Name = inputData;
                            int result = ctx.SaveChanges();
                            if (result == 1)
                            {
                                MessageBox.Show("Data changed !");
                            }
                        }
                        if (selectedValue == "Surname")
                        {
                            client[0].Surname = inputData;
                            int result = ctx.SaveChanges();
                            if (result == 1)
                            {
                                MessageBox.Show("Data changed !");
                            }
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Incorrect id !");
            }
        }
Beispiel #3
0
        private void ReturnRentedAccessory(object sender, EventArgs e)
        {
            string input = ReturnInput.Text;
            int    x;

            if (int.TryParse(input, out x))
            {
                using (var ctx = new PetRentalContext()) {
                    var rentals = ctx.Rentals
                                  .Where(b => b.ReturnDate == null)
                                  .Where(c => c.AccessoryId == x).ToArray();

                    var accessories = ctx.Accessories
                                      .Where(d => d.Id == x)
                                      .ToList();

                    if (accessories.Count == 0)
                    {
                        MessageBox.Show("Item does not exist in the database!");
                    }
                    else if (rentals.Length == 0)
                    {
                        MessageBox.Show("Selected item is not rented !");
                    }
                    else
                    {
                        rentals[0].ReturnDate = DateTime.Now;
                        int result = ctx.SaveChanges();
                        if (result == 1)
                        {
                            MessageBox.Show("Item returned !");
                        }


                        decimal amountToPay = accessories[0].OneDayRentalPrice;

                        int rentalDays = ((DateTime)rentals[0].ReturnDate.Value).Subtract(rentals[0].RentalDate).Days;
                        amountToPay     += (accessories[0].OneDayRentalPrice * rentalDays);
                        AmountToPay.Text = amountToPay.ToString();
                    }
                }
            }
            else
            {
                MessageBox.Show("Incorrect id");
            }
        }
        private void RemoveSelClient(object sender, EventArgs e)
        {
            var clientId = ClientToRemId.Text;
            int a;


            if (int.TryParse(clientId, out a))
            {
                using (var ctx = new PetRentalContext()) {
                    var clientToRemove = ctx.Clients.Where(x => x.Id == a).ToList();

                    if (clientToRemove.Count == 1)
                    {
                        var clientRentals = ctx.Rentals.Where(x => x.ClientId == a).ToList();
                        if (clientRentals.Count == 0)
                        {
                            ctx.Clients.Remove(clientToRemove[0]);
                            int result = ctx.SaveChanges();
                            if (result == 1)
                            {
                                MessageBox.Show("Deleted !");
                            }
                        }
                        else
                        {
                            MessageBox.Show("This client has already rented something !");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Client not found !");
                    }
                }
            }
            else
            {
                MessageBox.Show("Incorrect client Id");
            }
        }
        private void AddNewRental(object sender, EventArgs e)
        {
            string inputAccessory = AccessoryId.Text;
            string inputClient    = ClientId.Text;
            int    x;
            int    y;

            if (int.TryParse(inputAccessory, out x) && int.TryParse(inputClient, out y))
            {
                using (var ctx = new PetRentalContext()) {
                    var rentals = ctx.Rentals
                                  .Include(a => a.Accessory)
                                  .ToList();
                    var acc = ctx.Accessories
                              .Where(p => p.Id == x)
                              .ToList();
                    var NotReturnedRentals = rentals
                                             .Where(b => b.ReturnDate is null)
                                             .Where(c => c.AccessoryId == x)
                                             .ToList();
                    var clients = ctx.Clients
                                  .Where(z => z.Id == y)
                                  .ToList();


                    if (NotReturnedRentals.Count == 0)
                    {
                        if (clients.Count == 1)
                        {
                            if (acc.Count == 0)
                            {
                                MessageBox.Show("This item does not exist in the database !");
                            }
                            else
                            {
                                Client    client    = clients[0];
                                Accessory accessory = acc[0];

                                var newRental = new Rental()
                                {
                                    Client = client, Accessory = accessory, RentalDate = DateTime.Now
                                };
                                ctx.Rentals.Add(newRental);
                                int result = ctx.SaveChanges();
                                if (result == 1)
                                {
                                    MessageBox.Show("Rental added !");
                                }
                            }
                        }
                        else
                        {
                            MessageBox.Show("Client not found !");
                        }
                    }
                    else
                    {
                        MessageBox.Show("This accessory is rented now !");
                    }
                }
            }
            else
            {
                MessageBox.Show("Incorrect id !");
            }
        }