public async Task <bool> DeleteUserAsync(User user, bool save = true) { try { // Remove all caseclient entries foreach (Case c in user.Cases) { // Select the caseclient with the user's id and the case id pertaining to this case Caseclient cc = await _context.Caseclient.FirstOrDefaultAsync(cc => cc.Clientid == user.Id && cc.Caseid == c.Id); if (cc != null) { _context.Caseclient.Remove(cc); } } // Finally, remove the user _context.Users.Remove(await _context.Users.FirstOrDefaultAsync(c => c.Rowid == user.Id)); if (save) { await _context.SaveChangesAsync(); } return(true); } catch (Exception e) { return(false); } }
// NOTE: AddCaseAsync isnt able to delay saving due to needing to query the db in order to get the id, // which is accomplished by saving public async Task <bool> AddCaseAsync(Case targetCase, bool save = true) { try { // Add the case Cases dbCase = _context.Cases.Add(CaseMapper.Map(targetCase)).Entity; await _context.SaveChangesAsync(); // Next, add the caseclient entry for every client foreach (User user in targetCase.Clients) { Caseclient dbCaseclient = _context.Caseclient.Add(new Caseclient { Caseid = dbCase.Caseid, Clientid = user.Id }).Entity; user.Cases.Add(targetCase); await _user.UpdateUserAsync(user, false); dbCase.Caseclient.Add(dbCaseclient); } // Next, add all of the case's appointments, if any foreach (Appointment a in targetCase.Appointments) { await _appointment.AddAppointmentToCaseAsync(targetCase, a, false); } // Finally, add all of the case's notes, if any foreach (Note cn in targetCase.Notes) { await _note.AddNoteToCaseAsync(targetCase, cn, false); } if (save) { await _context.SaveChangesAsync(); } return(true); } catch (Exception e) { return(false); } }
public async Task <bool> DeleteCaseAsync(Case targetCase, bool save = true) { try { // Delete all entries in appointment List <Appointment> appointmentCopy = targetCase.Appointments.ToList(); foreach (Appointment appointment in appointmentCopy) { await _appointment.DeleteAppointmentFromCaseAsync(targetCase, appointment, false); } // Next, delete all entries in casenote List <Note> notesCopy = targetCase.Notes.ToList(); foreach (Note note in notesCopy) { await _note.DeleteNoteFromCaseAsync(targetCase, note, false); } // Next, delete all entries in caseclient foreach (User client in targetCase.Clients) { Caseclient cc = await _context.Caseclient.FirstOrDefaultAsync(cc => cc.Caseid == targetCase.Id); if (cc != null) { _context.Caseclient.Remove(cc); } } // Finally, remove the case _context.Cases.Remove(await _context.Cases.FirstOrDefaultAsync(c => c.Caseid == targetCase.Id)); if (save) { await _context.SaveChangesAsync(); } return(true); } catch (Exception e) { return(false); } }