internal static void UpdateShot(string shotName, Animal animal) { Shot thisShot = null; Animal animalWithShot = null; try { thisShot = db.Shots.Where(c => c.Name == shotName).Single(); } catch (InvalidOperationException) { UserInterface.DisplayMessage("No record of this particular shot can be found."); UserInterface.DisplayMessage("No updates have been made."); return; } try { animalWithShot = db.Animals.Where(a => a.AnimalId == animal.AnimalId).Single(); } catch (InvalidOperationException) { UserInterface.DisplayMessage("No record of this animal can be found."); UserInterface.DisplayMessage("No updates have been made."); return; } AnimalShot shotToAdd = new AnimalShot { AnimalId = animalWithShot.AnimalId, ShotId = thisShot.ShotId, DateReceived = DateTime.Today }; db.AnimalShots.InsertOnSubmit(shotToAdd); db.SubmitChanges(); }
internal static void UpdateAdoption(bool isAdopted, Adoption adoption) { //Search DB for this particular adoption Adoption adoptionFromDb = null; try { adoptionFromDb = db.Adoptions.Where(c => c.AnimalId == adoption.AnimalId).Single(); } catch (InvalidOperationException) { UserInterface.DisplayMessage("No adoption matches the ID passed in."); UserInterface.DisplayMessage("No updates have been made."); return; } Animal animalFromDb = db.Animals.Where(c => c.AnimalId == adoption.AnimalId).Single(); if (isAdopted) { ApproveAdoption(adoptionFromDb, animalFromDb); } else { RemoveAdoption(adoptionFromDb); } db.SubmitChanges(); }
internal static void UpdateAnimal(int animalID, Dictionary <int, string> updates) { Animal animalFromDb = null; var allCategories = db.Categories; try { animalFromDb = db.Animals.Where(c => c.AnimalId == animalID).FirstOrDefault(); } catch (InvalidOperationException) { UserInterface.DisplayMessage("No animals have an AnimalId that matches the ID passed in."); UserInterface.DisplayMessage("No updates have been made."); return; } foreach (var item in updates) { switch (item.Key) { case 1: //Category Category updatedCategory = allCategories.Where(a => a.Name == item.Value).FirstOrDefault(); animalFromDb.Category = updatedCategory; break; case 2: //Name animalFromDb.Name = item.Value; break; case 3: //Age int ageInInt = Int32.Parse(item.Value); animalFromDb.Age = ageInInt; break; case 4: //Demeanor animalFromDb.Demeanor = item.Value; break; case 5: //Kid Friendly bool kidFriendly = UserInterface.YesNoToBool(item.Value); animalFromDb.KidFriendly = kidFriendly; break; case 6: //Pet Friendly bool petFriendly = UserInterface.YesNoToBool(item.Value); animalFromDb.PetFriendly = petFriendly; break; case 7: //Weight int weightInInt = Int32.Parse(item.Value); animalFromDb.Weight = weightInInt; break; default: UserInterface.DisplayMessage("Improper parameters passed in."); UserInterface.DisplayMessage("Update was unsuccessful. Please try your updates again"); break; } db.SubmitChanges(); } }
//// TODO Items: //// // TODO: Allow any of the Create(insert)Read(select)UpdateDelete operations to occur here internal static void RunEmployeeQueries(Employee employee, string crudOperation) { //Switch case for all CRUD operations switch (crudOperation) { case "Create": AddEmployee(employee); break; case "Read": GetEmployeeByID(employee); break; case "Update": UpdateEmployee(employee); break; case "Delete": RemoveEmployee(employee); break; default: UserInterface.DisplayMessage("Not a valid selection. Please try again"); break; } }
// TODO: Animal Multi-Trait Search internal static IQueryable <Animal> SearchForAnimalsByMultipleTraits(Dictionary <int, string> searchCriteria) { IQueryable <Animal> allAnimals = db.Animals; foreach (var item in searchCriteria) { switch (item.Key) { case 1: //Category allAnimals = allAnimals.Where(c => c.Category.Name == item.Value); break; case 2: //Name allAnimals = allAnimals.Where(c => c.Name == item.Value); break; case 3: //Age int ageInInt = Int32.Parse(item.Value); allAnimals = allAnimals = allAnimals.Where(c => c.Age == ageInInt); break; case 4: //Demeanor allAnimals = allAnimals.Where(c => c.Demeanor == item.Value); break; case 5: //Kid Friendly bool kidFriendly = UserInterface.YesNoToBool(item.Value); allAnimals = allAnimals.Where(c => c.KidFriendly == kidFriendly); break; case 6: //Pet Friendly bool petFriendly = UserInterface.YesNoToBool(item.Value); allAnimals = allAnimals.Where(c => c.PetFriendly == petFriendly); break; case 7: //Weight int weightInInt = Int32.Parse(item.Value); allAnimals = allAnimals.Where(c => c.Weight == weightInInt); break; case 8: //ID int idInInt = Int32.Parse(item.Value); allAnimals = allAnimals.Where(c => c.AnimalId == idInInt); break; default: UserInterface.DisplayMessage("Improper search parameters passed in."); UserInterface.DisplayMessage("Search was unsuccessful. Please try your search again"); break; } } return(allAnimals); }
internal static void UpdateClient(Client clientWithUpdates) { // find corresponding Client from Db Client clientFromDb = null; try { clientFromDb = db.Clients.Where(c => c.ClientId == clientWithUpdates.ClientId).Single(); } catch (InvalidOperationException) { UserInterface.DisplayMessage("No clients have a ClientId that matches the Client passed in."); UserInterface.DisplayMessage("No update have been made."); return; } // update clientFromDb information with the values on clientWithUpdates (aside from address) clientFromDb.FirstName = clientWithUpdates.FirstName; clientFromDb.LastName = clientWithUpdates.LastName; clientFromDb.UserName = clientWithUpdates.UserName; clientFromDb.Password = clientWithUpdates.Password; clientFromDb.Email = clientWithUpdates.Email; // get address object from clientWithUpdates Address clientAddress = clientWithUpdates.Address; // look for existing Address in Db (null will be returned if the address isn't already in the Db Address updatedAddress = db.Addresses.Where(a => a.AddressLine1 == clientAddress.AddressLine1 && a.USStateId == clientAddress.USStateId && a.Zipcode == clientAddress.Zipcode).FirstOrDefault(); // if the address isn't found in the Db, create and insert it if (updatedAddress == null) { Address newAddress = new Address(); newAddress.AddressLine1 = clientAddress.AddressLine1; newAddress.City = null; newAddress.USStateId = clientAddress.USStateId; newAddress.Zipcode = clientAddress.Zipcode; db.Addresses.InsertOnSubmit(newAddress); db.SubmitChanges(); updatedAddress = newAddress; } // attach AddressId to clientFromDb.AddressId clientFromDb.AddressId = updatedAddress.AddressId; // submit changes db.SubmitChanges(); }
// TODO: Shots Stuff internal static IQueryable <AnimalShot> GetShots(Animal animal) { Animal animalFromDb = null; try { animalFromDb = db.Animals.Where(c => c.AnimalId == animal.AnimalId).Single(); } catch (InvalidOperationException) { UserInterface.DisplayMessage("No animals have an AnimalId that matches the ID passed in."); UserInterface.DisplayMessage("No updates have been made."); return(null); } var listOfShots = db.AnimalShots.Where(s => s.AnimalId == animalFromDb.AnimalId); return(listOfShots); }
// TODO: Adoption CRUD Operations internal static void Adopt(Animal animal, Client client) { Animal animalFromDb = null; Client clientFromDb = null; try { animalFromDb = db.Animals.Where(c => c.AnimalId == animal.AnimalId).Single(); } catch (InvalidOperationException) { UserInterface.DisplayMessage("No animals have an AnimalId that matches the ID passed in."); UserInterface.DisplayMessage("No updates have been made."); return; } try { clientFromDb = db.Clients.Where(c => c.ClientId == client.ClientId).Single(); } catch (InvalidOperationException) { UserInterface.DisplayMessage("No clients have a client ID that matches the ID passed in."); UserInterface.DisplayMessage("No updates have been made."); return; } Adoption animalAdoption = new Adoption { AnimalId = animalFromDb.AnimalId, ClientId = clientFromDb.ClientId, ApprovalStatus = "Pending", AdoptionFee = 100, PaymentCollected = true }; db.Adoptions.InsertOnSubmit(animalAdoption); db.SubmitChanges(); }
//Update employee internal static void UpdateEmployee(Employee employee) { Employee employeeFromDb = null; try { employeeFromDb = db.Employees.Where(c => c.EmployeeId == employee.EmployeeId).FirstOrDefault(); } catch (InvalidOperationException) { UserInterface.DisplayMessage("No employees have an EmployeeId that matches the Employee passed in."); UserInterface.DisplayMessage("No update have been made."); return; } employeeFromDb.FirstName = employee.FirstName; employeeFromDb.LastName = employee.LastName; employeeFromDb.UserName = employee.UserName; employeeFromDb.Password = employee.Password; employeeFromDb.EmployeeNumber = employee.EmployeeNumber; employeeFromDb.Email = employee.Email; db.SubmitChanges(); }