public GroupUser AddGroupUser(GroupUser groupUser, long groupId, OldUser curator) { using (var groupUserRepository = new BaseRepository<GroupUser>()) { var group = groupUserRepository.Context.Groups.FirstOrDefault(x => x.Id == groupId); if (group == null) { throw new PersonsGroupDoesNotExistException(); } if (!String.Equals(curator.Role.Name, Constraints.KCuratorRoleName) || curator.Id != group.Curator) { throw new UserRightsException(); } if (String.IsNullOrWhiteSpace(groupUser.Email) || String.IsNullOrWhiteSpace(groupUser.FirstName) || String.IsNullOrWhiteSpace(groupUser.LastName) || String.IsNullOrWhiteSpace(groupUser.MiddleName)) { throw new RequireFieldException(); } //TODO: Refactor, optimization var code = CodeGenerator.Generate(); while (!CheckCodeUnique(code)) { code = CodeGenerator.Generate(); } groupUser.Code = code; groupUser.Group = group; if (!groupUserRepository.Create(groupUser).Status) { throw new CreateException(); } return groupUser; } }
public Object DeleteGroupUser(long id, long groupUserId) { var bodyResponse = new WebJsonResponse(); try { var curator = _authorizationRepository.GetUserByCredentials(WebRequestHeaders.GetAuthorizationToken(this)); if (curator == null) { return new HttpResponseMessage(HttpStatusCode.Unauthorized); } var groupUser = new GroupUser(); groupUser.Id = groupUserId; _groupRepository.DeleteGroupUser(groupUser, id, curator); bodyResponse.Data = new object(); bodyResponse.Status = true; } catch (UnauthorizedException) { return new HttpResponseMessage(HttpStatusCode.Unauthorized); } catch (Exception exception) { bodyResponse.Error = exception.Message; } return bodyResponse; }
public void UpdateGroupUser(GroupUser groupUser, long groupId, OldUser curator) { using (var groupUserRepository = new BaseRepository<GroupUser>()) { var group = groupUserRepository.Context.Groups.FirstOrDefault(x => x.Id == groupId); if (group == null) { throw new PersonsGroupDoesNotExistException(); } if (!String.Equals(curator.Role.Name, Constraints.KCuratorRoleName) || curator.Id != group.Curator) { throw new UserRightsException(); } var updatedGroupUser = groupUserRepository.GetAllItems.FirstOrDefault(x => x.Id == groupUser.Id); if (updatedGroupUser == null) { throw new UserDoesNotExistException(); } if (updatedGroupUser.Group.Id != groupId) { throw new GroupUserException(); } if (String.IsNullOrWhiteSpace(groupUser.Email) || String.IsNullOrWhiteSpace(groupUser.FirstName) || String.IsNullOrWhiteSpace(groupUser.LastName) || String.IsNullOrWhiteSpace(groupUser.MiddleName)) { throw new RequireFieldException(); } //update all fields except Id, code, group updatedGroupUser.Email = groupUser.Email; updatedGroupUser.BirthDate = groupUser.BirthDate; updatedGroupUser.Gender = groupUser.Gender; updatedGroupUser.FirstName = groupUser.FirstName; updatedGroupUser.LastName = groupUser.LastName; updatedGroupUser.MiddleName = groupUser.MiddleName; updatedGroupUser.Note = groupUser.Note; if (!groupUserRepository.Update(updatedGroupUser).Status) { throw new UpdateException(); } } }
public void DeleteGroupUser(GroupUser groupUser, long groupId, OldUser curator) { using (var groupUserRepository = new BaseRepository<GroupUser>()) { var group = groupUserRepository.Context.Groups.FirstOrDefault(x => x.Id == groupId); if (group == null) { throw new PersonsGroupDoesNotExistException(); } if (!String.Equals(curator.Role.Name, Constraints.KCuratorRoleName) || curator.Id != group.Curator) { throw new UserRightsException(); } var deletedGroupUser = groupUserRepository.GetAllItems.FirstOrDefault(x => x.Id == groupUser.Id); if (deletedGroupUser == null) { throw new UserDoesNotExistException(); } if (deletedGroupUser.Group.Id != groupId) { throw new GroupUserException(); } if (!groupUserRepository.Delete(deletedGroupUser).Status) { throw new DeleteException(); } } }