public async Task <ActionResult> PostAppointmentToCase(int caseId, [FromBody] Appointment appointment)
        {
            try
            {
                string userId = _auth.GetUserIdFromToken(HttpContext);
                User   user   = await _user.GetUserByIdAsync(userId);

                Case aCase = await _case.GetCaseByIdAsync(caseId);

                if (user == null)
                {
                    return(Forbid());
                }

                if (aCase == null)
                {
                    return(NotFound());
                }

                if (aCase.Clients.Contains(user) || user.Role.Id == Role.Consultant.Id)
                {
                    await _appointment.AddAppointmentToCaseAsync(aCase, appointment);

                    return(NoContent());
                }
                else
                {
                    return(Forbid());
                }
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e));
            }
        }
        // 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);
            }
        }
Ejemplo n.º 3
0
        static async Task RunCaseWithAppointmentsTest(IUserRepository _user, ICaseRepository _case,
                                                      ICaseStatusRepository _caseStatus, IAppointmentRepository _appointment, IRoleRepository _role)
        {
            Console.WriteLine("TEST BEGIN");
            Console.WriteLine("Beginning appointment test...");

            Status unassigned = await _caseStatus.GetCaseStatusByTextAsync("Unassigned");

            Role roleClient = await _role.GetRoleByText("Client");

            Role roleConsultant = await _role.GetRoleByText("Consultant");

            // SETUP
            User consultant = new User
            {
                UserId = "testid_consultant",
                Role   = roleConsultant
            };

            consultant = await _user.AddUserAsync(consultant);

            User client = new User
            {
                UserId = "testid_client",
                Role   = roleClient
            };

            client = await _user.AddUserAsync(client);

            Case aCase = new Case
            {
                Title            = "Test Case",
                ActiveConsultant = consultant,
                Clients          = new List <User> {
                    client
                },
                Status = unassigned
            };

            aCase = await _case.AddCaseAsync(aCase);

            // CREATE appointments for case
            Console.WriteLine("Creating appointments...");
            await _appointment.AddAppointmentToCaseAsync(aCase, new Appointment
            {
                CaseId = aCase.Id,
                AppointmentDateTime = DateTime.Now,
                Title = "Test Appointment 1"
            });

            await _appointment.AddAppointmentToCaseAsync(aCase, new Appointment
            {
                CaseId = aCase.Id,
                AppointmentDateTime = DateTime.Today,
                Title = "Test Appointment 2"
            });

            // RETRIEVE case via client
            Console.WriteLine("Retrieving data...");
            client = await _user.GetUserByRowIdAsync(client.Id);

            foreach (Case c in client.Cases)
            {
                foreach (Appointment a in c.Appointments)
                {
                    Console.WriteLine("Appointment for " + c.Title + ": " + a.AppointmentDateTime);
                }
            }

            // UPDATE first appointment to be for tomorrow
            Console.WriteLine("Updating an appointment...");
            client.Cases[0].Appointments[0].AppointmentDateTime = client.Cases[0].Appointments[0].AppointmentDateTime.AddDays(1);
            await _appointment.UpdateAppointmentAsync(client.Cases[0].Appointments[0]);

            // DELETE appointment
            Console.WriteLine("Deleting an appointment...");
            await _appointment.DeleteAppointmentFromCaseAsync(client.Cases[0], client.Cases[0].Appointments[1]);

            // RETRIEVE case via client a second time to see update
            Console.WriteLine("Retrieving data again...");
            client = await _user.GetUserByIdAsync(client.UserId);

            foreach (Case c in client.Cases)
            {
                foreach (Appointment a in c.Appointments)
                {
                    Console.WriteLine("Appointment for " + c.Title + ": " + a.AppointmentDateTime);
                }
            }

            // CLEANUP
            Console.WriteLine("Deleting entries...");
            await _user.DeleteUserAsync(client);

            await _case.DeleteCaseAsync(aCase);

            await _user.DeleteUserAsync(consultant);

            client = await _user.GetUserByIdAsync(client.UserId);

            aCase = await _case.GetCaseByIdAsync(aCase.Id);

            consultant = await _user.GetUserByIdAsync(consultant.UserId);

            Console.WriteLine("Deletion: " + ((client == null && aCase == null && consultant == null) ? "Success" : "Failure"));
            Console.WriteLine("TEST END.");
            Console.WriteLine();
            Console.WriteLine();
        }