Beispiel #1
0
        public IActionResult SeedUsers()
        {
            try
            {
                _userService.CleanUsersAndRelatedData();

                foreach (var user in UsersModel.Users)
                {
                    _userService.Save(user);
                }

                _userRoleService.Associate(userId: 1, new RoleModel()
                {
                    Id = 1, Description = "Admin"
                });

                return(StatusCode(StatusCodes.Status201Created));
            }
            catch (System.ApplicationException ex)
            {
                return(StatusCode(StatusCodes.Status422UnprocessableEntity, ex.Message));
            }
            catch (System.Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "A server error has occurred"));
            }
        }
        public ActionResult PostUserRoles(int userId, RoleModel role)
        {
            try
            {
                var user = _userService.Get(userId);

                if (user == null)
                {
                    return(NotFound("User not found"));
                }

                _userRoleService.Associate(userId, role);

                return(Created($"/api/v1/users/{userId}/roles", role));
            }
            catch (System.ApplicationException ex)
            {
                return(StatusCode(StatusCodes.Status422UnprocessableEntity, ex.Message));
            }
            catch (System.Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "A server error has occurred"));
            }
        }