public void TestEditingExistingActivityRecord() { // Arrange bool result = false; IAnimalActivityManager manager = new AnimalActivityManager(_fakeActivityAccessor); AnimalActivity existingRecord = new AnimalActivity() { AnimalActivityId = 1, AnimalID = 5, AnimalActivityTypeID = "Play" }; AnimalActivity updatedRecord = new AnimalActivity() { AnimalActivityId = 1, AnimalID = 4, AnimalActivityTypeID = "Play" }; // Act manager.AddAnimalActivityRecord(existingRecord); result = manager.EditExistingAnimalActivityRecord( existingRecord, updatedRecord); // Assert Assert.IsTrue(result); }
public void TestEditingNonExistentActivityRecord() { // Arrange bool result = false; IAnimalActivityManager manager = new AnimalActivityManager(_fakeActivityAccessor); AnimalActivity nonExistentRecord = new AnimalActivity() { AnimalActivityId = 3, AnimalID = 6, AnimalActivityTypeID = "Feeding" }; AnimalActivity updatedRecord = new AnimalActivity() { AnimalActivityId = 1, AnimalID = 4, AnimalActivityTypeID = "Play" }; // Act result = manager.EditExistingAnimalActivityRecord( nonExistentRecord, updatedRecord); // Assert Assert.IsFalse(result); }
/// <summary> /// Creator: Ethan Murphy /// Created: 4/25/2020 /// Approver: Chuck Baxter 4/27/2020 /// /// Deletes the selected activity record /// </summary> /// <remarks> /// Updater: /// Updated: /// Update: /// </remarks> private void btnDelete_Click(object sender, RoutedEventArgs e) { AnimalActivity activity = (AnimalActivity)dgActivities.SelectedItem; if (activity == null) { return; } string message = "Are you sure you want to delete the " + activity.AnimalActivityTypeID + " record for " + activity.AnimalName + "? This can't be undone!"; if (MessageBox.Show(message, "Confirm", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { try { if (_activityManager.DeleteAnimalActivityRecord(activity)) { MessageBox.Show("Record deleted"); DisableEditMode(); canViewActivityRecord.Visibility = Visibility.Hidden; canView.Visibility = Visibility.Visible; RefreshActivitiesList(); } else { throw new ApplicationException("Record not found"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
public async Task <IActionResult> Edit(int id, [Bind("AnimalActivityId,Name,UserId")] AnimalActivity animalActivity) { if (id != animalActivity.AnimalActivityId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(animalActivity); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AnimalActivityExists(animalActivity.AnimalActivityId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["UserId"] = new SelectList(_context.Users, "Id", "Id", animalActivity.UserId); return(View(animalActivity)); }
/// <summary> /// Creator: Ethan Murphy /// Created: 4/25/2020 /// Approver: Chuck Baxter 4/27/2020 /// /// Deletes an existing animal activity record /// </summary> /// <remarks> /// Updater: /// Updated: /// Update: /// </remarks> /// <param name="animalActivity">Record to be deleted</param> /// <returns>Number of records deleted</returns> public int DeleteAnimalActivityRecord(AnimalActivity animalActivity) { int rows = 0; var conn = DBConnection.GetConnection(); var cmd = new SqlCommand("sp_delete_animal_activity_record", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@AnimalActivityID", animalActivity.AnimalActivityId); cmd.Parameters.AddWithValue("@AnimalID", animalActivity.AnimalID); cmd.Parameters.AddWithValue("@UserID", animalActivity.UserID); cmd.Parameters.AddWithValue("@AnimalActivityTypeID", animalActivity.AnimalActivityTypeID); cmd.Parameters.AddWithValue("@ActivityDateTime", animalActivity.ActivityDateTime); cmd.Parameters.AddWithValue("@Description", animalActivity.Description); try { conn.Open(); rows = cmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { conn.Close(); } return(rows); }
/// <summary> /// Creator: Ethan Murphy /// Created: 4/2/2020 /// Approver: Carl Davis, 4/3/2020 /// /// Inserts an animal activity into the existing list /// of fake animal activity records /// </summary> /// <remarks> /// Updater: /// Updated: /// Update: /// </remarks> /// <param name="animalActivity">Record to insert</param> /// <returns>The number of records updated</returns> public int InsertAnimalActivityRecord(AnimalActivity animalActivity) { int startingLength = _animalActivity.Count; _animalActivity.Add(animalActivity); return(_animalActivity.Count - startingLength); }
/// <summary> /// Creator: Ethan Murphy /// Created: 4/2/2020 /// Approver: Carl Davis 4/3/2020 /// /// Helper method to populate form controls /// when a record is opened /// </summary> /// <remarks> /// Updater: /// Updated: /// Update: /// </remarks> private void PopulateFields(AnimalActivity activity) { txtActivityId.Text = activity.AnimalActivityId.ToString(); txtAnimalName.Text = activity.AnimalName; cmbActivityType2.Text = activity.AnimalActivityTypeID; dateActivityDate.SelectedDate = activity.ActivityDateTime; cboApptTime.Text = activity.ActivityDateTime.ToShortTimeString(); txtDescription.Text = activity.Description; }
/// <summary> /// Creator: Ethan Murphy /// Created: 4/6/2020 /// Approver: Chuck Baxter 4/7/2020 /// /// Edits an existing activity record /// </summary> /// <remarks> /// Updater: /// Updated: /// Update: /// </remarks> /// <param name="oldAnimalActivity">Existing record</param> /// <param name="newAnimalActivity">Updated record</param> /// <returns>Result of edit</returns> public bool EditExistingAnimalActivityRecord(AnimalActivity oldAnimalActivity, AnimalActivity newAnimalActivity) { try { return(_activityAccessor.UpdateAnimalActivityRecord(oldAnimalActivity, newAnimalActivity) == 1); } catch (Exception ex) { throw new ApplicationException("Record not updated", ex); } }
/// <summary> /// Creator: Ethan Murphy /// Created: 4/2/2020 /// Approver: Carl Davis 4/3/2020 /// /// Adds an animal activity record to the DB /// </summary> /// <remarks> /// Updater: /// Updated: /// Update: /// </remarks> /// <param name="animalActivity">Activity record to be added</param> /// <returns>Result of insert</returns> public bool AddAnimalActivityRecord(AnimalActivity animalActivity) { try { return(_activityAccessor.InsertAnimalActivityRecord(animalActivity) == 1); } catch (Exception ex) { throw new ApplicationException("Failed to add record", ex); } }
/// <summary> /// Creator: Ethan Murphy /// Created: 4/25/2020 /// Approver: Chuck Baxter 4/27/2020 /// /// Deletes an existing animal activity record /// </summary> /// <remarks> /// Updater: /// Updated: /// Update: /// </remarks> /// <param name="animalActivity">Record to be deleted</param> /// <returns>Delete successful</returns> public bool DeleteAnimalActivityRecord(AnimalActivity animalActivity) { try { return(_activityAccessor.DeleteAnimalActivityRecord(animalActivity) == 1); } catch (Exception ex) { throw new ApplicationException("Failed to delete record", ex); } }
public async Task <IActionResult> Create([Bind("AnimalActivityId,Name")] AnimalActivity animalActivity) { var user = await GetCurrentUserAsync(); if (ModelState.IsValid) { animalActivity.User = user; animalActivity.UserId = user.Id; _context.Add(animalActivity); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(animalActivity)); }
public void TestAddingAnimalActivityRecord() { // Arrange IAnimalActivityManager manager = new AnimalActivityManager(_fakeActivityAccessor); bool result = false; AnimalActivity activity = new AnimalActivity() { AnimalActivityId = 1000, AnimalID = 1, UserID = 1, AnimalActivityTypeID = "Play" }; // Act result = manager.AddAnimalActivityRecord(activity); // Assert Assert.IsTrue(result); }
/// <summary> /// Creator: Ethan Murphy /// Created: 4/6/2020 /// Approver: Chuck Baxter, 4/7/2020 /// /// Updates an existing animal activity record /// </summary> /// <remarks> /// Updater: /// Updated: /// Update: /// </remarks> /// <param name="oldAnimalActivity">The existing record</param> /// <param name="newAnimalActivity">The updated record</param> /// <returns>Update successful</returns> public int UpdateAnimalActivityRecord(AnimalActivity oldAnimalActivity, AnimalActivity newAnimalActivity) { int recordsUpdated = 0; var foundRecord = _animalActivity.Find(a => a.AnimalActivityTypeID == oldAnimalActivity.AnimalActivityTypeID && a.AnimalID == oldAnimalActivity.AnimalID && a.UserID == oldAnimalActivity.UserID && a.AnimalActivityTypeID == oldAnimalActivity.AnimalActivityTypeID && a.ActivityDateTime == oldAnimalActivity.ActivityDateTime && a.Description == oldAnimalActivity.Description); if (foundRecord != null) { _animalActivity[_animalActivity.IndexOf(foundRecord)] = newAnimalActivity; recordsUpdated = ((!_animalActivity.Contains(foundRecord)) && _animalActivity.Contains(newAnimalActivity)) ? 1 : 0; } return(recordsUpdated); }
/// <summary> /// Creator: Ethan Murphy /// Created: 4/25/2020 /// Approver: Chuck Baxter 4/27/2020 /// /// Deletes an existing animal activity record /// </summary> /// <remarks> /// Updater: /// Updated: /// Update: /// </remarks> /// <param name="animalActivity">Record to be deleted</param> /// <returns>Number of records deleted</returns> public int DeleteAnimalActivityRecord(AnimalActivity animalActivity) { int rows = 0; int startingCount = _animalActivity.Count; var foundRecord = _animalActivity.Where(a => a.AnimalActivityId == animalActivity.AnimalActivityId && a.AnimalID == animalActivity.AnimalID && a.UserID == animalActivity.UserID && a.AnimalActivityTypeID == animalActivity.AnimalActivityTypeID && a.ActivityDateTime == animalActivity.ActivityDateTime && a.Description == animalActivity.Description).FirstOrDefault(); if (foundRecord != null) { _animalActivity.Remove(foundRecord); } rows = startingCount - _animalActivity.Count; return(rows); }
public void TestDeleteNonExistentAnimalActivityRecord() { // Arrange IAnimalActivityManager manager = new AnimalActivityManager(_fakeActivityAccessor); bool result = false; AnimalActivity animalActivity = new AnimalActivity() { AnimalActivityId = 1, AnimalID = 1, UserID = 10000, AnimalActivityTypeID = "Feeding", ActivityDateTime = DateTime.Now, Description = "test" }; // Act result = manager.DeleteAnimalActivityRecord(animalActivity); // Assert Assert.IsFalse(result); }
/// <summary> /// Creator: Ethan Murphy /// Created: 4/2/2020 /// Approver: Carl Davis 4/3/2020 /// /// Handles saving and edititing functions /// </summary> /// <remarks> /// Updater: Ethan Murphy /// Updated: 4/6/2020 /// Update: Added edit functionality /// Approver: Chuck Baxter 4/7/2020 /// </remarks> private void btnSaveEdit_Click(object sender, RoutedEventArgs e) { if (btnSaveEdit.Content.Equals("Edit")) { EnableEditMode(); return; } if (selectedAnimal == null) { MessageBox.Show("You must select an animal first!"); return; } if (cmbActivityType2.SelectedItem == null) { MessageBox.Show("You must select an activity type!"); return; } if (dateActivityDate.SelectedDate == null) { MessageBox.Show("You must select the activity date"); return; } if (cboApptTime.SelectedItem == null) { MessageBox.Show("Please select the activity time"); return; } DateTime activityDate; if (!DateTime.TryParse( dateActivityDate.SelectedDate.Value.ToShortDateString() + " " + cboApptTime.SelectedItem.ToString(), out activityDate)) { MessageBox.Show("Invalid date or time entered"); return; } AnimalActivity animalActivity = new AnimalActivity() { AnimalID = selectedAnimal.AnimalID, AnimalActivityTypeID = cmbActivityType2.SelectedItem.ToString(), ActivityDateTime = activityDate, UserID = _user.PUUserID, Description = txtDescription.Text }; if (addMode) { try { if (_activityManager.AddAnimalActivityRecord(animalActivity)) { MessageBox.Show("Record added!"); DisableAddMode(); canViewActivityRecord.Visibility = Visibility.Hidden; canView.Visibility = Visibility.Visible; RefreshActivitiesList(); } else { throw new ApplicationException(); } } catch (Exception ex) { string message = ex == null ? "Failed to add record" : ex.Message + " " + ex.InnerException.Message; MessageBox.Show(message); } } else { // Perform update try { if (_activityManager.EditExistingAnimalActivityRecord( (AnimalActivity)dgActivities.SelectedItem, animalActivity)) { MessageBox.Show("Record updated"); DisableEditMode(); canViewActivityRecord.Visibility = Visibility.Hidden; canView.Visibility = Visibility.Visible; RefreshActivitiesList(); } else { throw new ApplicationException("Record not found"); } } catch (Exception ex) { string message = ex == null ? "Failed to add record" : ex.Message + " " + ex.InnerException.Message; MessageBox.Show(message); } } }