public CommonResult AddUsers(UserGroupRequest request) { using (var context = ContextFactory.GetDbContext(_dbConfiguration)) { var group = context.Groups.FirstOrDefault(x => x.Id == request.GroupId); if (group == null) { return CommonResult.Failure("Podana grupa nie istnieje."); } foreach (var userId in request.Users) { var userGroup = context.UserGroups.FirstOrDefault(x => x.Group.Id == request.GroupId && x.User.Id == userId); if (userGroup != null) { return CommonResult.Failure("Podany użytkownik został już dodany."); } context.UserGroups.Add(new UserGroup { Group = new Group { Id = request.GroupId }, User = new User { Id = userId } }); } context.SaveChanges(); return CommonResult.Success(); } }
public CommonResult AddUsers(UserGroupRequest request) { return _groupsRepository.AddUsers(request); }
public CommonResult TryAddUserToPrivateGroup(AddUserToGroupRequest request) { var getUserProfileDataResult = _userRepository.GetUserProfile(request.UserName); var getGroup = GetData(new GetGroupRequest { GroupId = request.GroupId }); if (getUserProfileDataResult.IsSuccess && getGroup.IsSuccess) { if (getGroup.Item.IsPrivate) { var isPasswordCorrect = getGroup.Item.Password == request.Password.ToBase64(); if (!isPasswordCorrect) { return CommonResult.Failure("Błędne hasło."); } } var userGroupRequest = new UserGroupRequest { GroupId = request.GroupId, Users = new List<int> { getUserProfileDataResult.Item.Id } }; var addUserResult = _groupsRepository.AddUsers(userGroupRequest); if (addUserResult.IsSuccess) { return CommonResult.Success(); } else { return CommonResult.Failure(addUserResult.ErrorMessage); } } else { return CommonResult.Failure("Błąd pobierania danych."); } }
public CommonResult RemoveUsers(UserGroupRequest request) { return _groupsRepository.RemoveUsers(request); }
public CommonResult RemoveUsers(UserGroupRequest request) { using (var context = ContextFactory.GetDbContext(_dbConfiguration)) { var group = context.Groups.FirstOrDefault(x => x.Id == request.GroupId); if (group == null) { return CommonResult.Failure("Podana grupa nie istnieje."); } foreach (var userId in request.Users) { var userGroup = context.UserGroups.FirstOrDefault(x => x.Group.Id == request.GroupId && x.User.Id == userId); if (userGroup == null) { return CommonResult.Failure("Podany użytkownik nie jest członkiem tej grupy."); } context.Remove(userGroup); } context.SaveChanges(); return CommonResult.Success(); } }
public IActionResult DeleteUser(DeleteUserFromGroupViewModel viewModel) { var request = new UserGroupRequest { GroupId = viewModel.GroupId, Users = new int[] { viewModel.UserId } }; var result = _groupsService.RemoveUsers(request); return new JsonResult(result); }