Esempio n. 1
0
        public async Task <IActionResult> SignupGroup(GroupSignupViewModel vm)
        {
            var user = await userManager.GetUserAsync(User);

            CalendarRepository repo = new CalendarRepository(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"));
            }

            // Verify that all of the inputs are valid
            // Note that I check if the date is equal to DateTime.MinValue, not null, as DateTime cannot be null in C#
            if (String.IsNullOrEmpty(vm.Group.Name) || String.IsNullOrEmpty(vm.Group.LeaderName) || String.IsNullOrEmpty(vm.Group.Email) ||
                String.IsNullOrEmpty(vm.Group.Phone) || vm.Date == DateTime.MinValue)
            {
                return(Utilities.ErrorJson("Name, Leader Name, Email, Phone, and Date are all required."));
            }

            // Check that there are not 0 (or fewer, in the case of fake inputs) volunteers in the group
            if (vm.Group.Count <= 0)
            {
                return(Utilities.ErrorJson("Groups must not have no volunteers"));
            }

            // Verify that the specified date is a saturday
            if (vm.Date.DayOfWeek != DayOfWeek.Saturday)
            {
                return(Utilities.ErrorJson("Date provided should be for a Saturday"));
            }

            // Insert the group into the database
            try
            {
                repo.CreateGroup(vm.Date, vm.Group);
            }
            catch (Exception e)
            {
                return(Utilities.ErrorJson(e.Message));
            }

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