Exemplo n.º 1
0
        public async Task <IActionResult> BusCreation(BusModel bus)
        {
            var user = await userManager.GetUserAsync(User);

            BusRepository busRepo = new BusRepository(configModel.ConnectionString);

            // 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"));
            }

            if (string.IsNullOrEmpty(bus.Name))
            {
                return(Utilities.ErrorJson("Bus name cannot be empty"));
            }
            if (bus.Name.Length > 300)
            {
                return(Utilities.ErrorJson("Bus name is too long (limit 300 characters)"));
            }

            try
            {
                busRepo.CreateBus(bus.Name, bus.Route);
            }
            catch (Exception e)
            {
                return(Utilities.ErrorJson(e.Message));
            }

            return(new JsonResult(new
            {
                Error = ""
            }));
        }