Ejemplo n.º 1
0
        public async Task <IActionResult> AssignBusDriver(BusModel model)
        {
            VolunteerRepository repo    = new VolunteerRepository(configModel.ConnectionString);
            BusRepository       busRepo = new BusRepository(configModel.ConnectionString);
            BusModel            bus;
            VolunteerModel      profile;
            var user = await userManager.GetUserAsync(User);

            /// Ensure that ONLY staff accounts have access to this API endpoint
            if (user == null || !await userManager.IsInRoleAsync(user, UserHelpers.UserRoles.Staff.ToString()))
            {
                return(Utilities.ErrorJson("Not authorized"));
            }

            // Validate the inputs
            bus = busRepo.GetBus(model.Id);

            if (bus == null)
            {
                return(Utilities.ErrorJson("Invalid bus id"));
            }

            profile = repo.GetVolunteer(model.DriverId);

            if (profile == null)
            {
                return(Utilities.ErrorJson("Invalid driver id"));
            }

            if (profile.Role != UserHelpers.UserRoles.BusDriver.ToString())
            {
                return(Utilities.ErrorJson("That user is not a bus driver"));
            }

            // Update in the database
            try
            {
                busRepo.AssignDriver(model.Id, model.DriverId);
            }
            catch (Exception e)
            {
                return(Utilities.ErrorJson(e.Message));
            }

            return(Utilities.NoErrorJson());
        }