Beispiel #1
0
        public async Task <IActionResult> CreateGroup()
        {
            var userId             = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
            var groupForCrationDto = new GroupForCrationDto();

            groupForCrationDto.LeaderId = userId;
            var group = _mapper.Map <Group>(groupForCrationDto);

            var adminPermissionForCreation = new GroupPermissionsForCreation {
                AdminPanelManagement = true,
                NewsManagement       = true,
                UsersManagement      = true,
                FileManagement       = true,
                UserId = userId
            };

            var adminPermission = _mapper.Map <GroupPermissions>(adminPermissionForCreation);

            _repo.AddToRepo(group);
            _repo.AddToRepo(adminPermission);

            if (await _repo.SaveAll()) // +++refactoring
            {
                var groupUserForCreationDto = new GroupUserForCreationDto();
                groupUserForCreationDto.GroupId = group.Id;
                groupUserForCreationDto.UserId  = userId;
                var groupUser = _mapper.Map <GroupUser>(groupUserForCreationDto);

                _repo.AddToRepo(groupUser);
                await _repo.SaveAll();

                return(Ok());
            }
            throw new Exception("Nie udało się utworzyć grupy");
        }
Beispiel #2
0
        public async Task <IActionResult> AddUserToGroup([FromBody] int groupId, int userId)
        {
            var groupUserForCreationDto = new GroupUserForCreationDto();

            groupUserForCreationDto.GroupId = groupId;
            groupUserForCreationDto.UserId  = userId;
            var groupUser = _mapper.Map <GroupUser>(groupUserForCreationDto);


            var user = await _repo.GetUser(userId);

            if (user.Role.Equals("teacher"))
            {
                var permissions = new GroupPermissionsForCreation {
                    UserId = userId
                };
                var permissionsForCreation = _mapper.Map <GroupPermissions>(permissions);
                _repo.AddToRepo(permissionsForCreation);
            }

            _repo.AddToRepo(groupUser);
            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            throw new Exception("Nie udało się dodać użytkownika do grupy");
        }
Beispiel #3
0
        public async Task <IActionResult> UpdateUserPermissions(GroupPermissionsForCreation groupPermissions)
        {
            var groupPermissionsFromRepo = await _repo.GetPermissions(groupPermissions.UserId);

            _mapper.Map(groupPermissions, groupPermissionsFromRepo);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Aktualizacja uprawnień zakończona niepowodzeniem");
        }