Skip to content

Commit

Permalink
Add new endpoint for info api /blob.
Browse files Browse the repository at this point in the history
Also removed the old /groups one.
  • Loading branch information
Portals committed Mar 5, 2024
1 parent a134e07 commit 2d912ad
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import it.chalmers.gamma.adapter.primary.api.utils.NotFoundResponse;
import it.chalmers.gamma.app.group.GroupFacade;
import it.chalmers.gamma.app.settings.SettingsFacade;
import it.chalmers.gamma.app.supergroup.SuperGroupFacade;
import it.chalmers.gamma.app.user.UserFacade;
import java.util.List;
import java.util.UUID;
Expand All @@ -21,22 +23,26 @@ public class InfoV1ApiController {

public static final String URI = "/api/info/v1";

private final GroupFacade groupFacade;
private final SuperGroupFacade superGroupFacade;
private final UserFacade userFacade;
private final SettingsFacade settingsFacade;

public InfoV1ApiController(GroupFacade groupFacade, UserFacade userFacade) {
this.groupFacade = groupFacade;
public InfoV1ApiController(
SuperGroupFacade superGroupFacade, UserFacade userFacade, SettingsFacade settingsFacade) {
this.superGroupFacade = superGroupFacade;
this.userFacade = userFacade;
this.settingsFacade = settingsFacade;
}

@GetMapping("/users/{id}")
public UserFacade.UserWithGroupsDTO getUser(@PathVariable("id") UUID id) {
return this.userFacade.get(id).orElseThrow(UserNotFoundResponse::new);
}

@GetMapping("/groups")
public GroupsResponse getGroups() {
return new GroupsResponse(this.groupFacade.getAllForInfoApi());
@GetMapping("/blob")
public List<SuperGroupFacade.SuperGroupTypeDTO> getGroups() {
return this.superGroupFacade.getAllTypesWithSuperGroups(
this.settingsFacade.getInfoApiSuperGroupTypes());
}

public record GroupsResponse(List<GroupFacade.GroupWithMembersDTO> groups) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package it.chalmers.gamma.adapter.secondary.jpa.group;

import it.chalmers.gamma.adapter.secondary.jpa.supergroup.SuperGroupJpaRepository;
import it.chalmers.gamma.adapter.secondary.jpa.user.UserEntityConverter;
import it.chalmers.gamma.adapter.secondary.jpa.user.UserJpaRepository;
import it.chalmers.gamma.adapter.secondary.jpa.util.PersistenceErrorHelper;
import it.chalmers.gamma.adapter.secondary.jpa.util.PersistenceErrorState;
import it.chalmers.gamma.app.group.domain.Group;
import it.chalmers.gamma.app.group.domain.GroupId;
import it.chalmers.gamma.app.group.domain.GroupRepository;
import it.chalmers.gamma.app.group.domain.UnofficialPostName;
import it.chalmers.gamma.app.group.domain.*;
import it.chalmers.gamma.app.image.domain.ImageUri;
import it.chalmers.gamma.app.post.domain.PostId;
import it.chalmers.gamma.app.supergroup.domain.SuperGroupId;
Expand Down Expand Up @@ -43,6 +41,7 @@ public class GroupRepositoryAdapter implements GroupRepository {
private final SuperGroupJpaRepository superGroupJpaRepository;
private final PostJpaRepository postJpaRepository;
private final UserJpaRepository userJpaRepository;
private final UserEntityConverter userEntityConverter;

public GroupRepositoryAdapter(
GroupJpaRepository groupJpaRepository,
Expand All @@ -51,14 +50,16 @@ public GroupRepositoryAdapter(
PostEntityConverter postEntityConverter,
SuperGroupJpaRepository superGroupJpaRepository,
PostJpaRepository postJpaRepository,
UserJpaRepository userJpaRepository) {
UserJpaRepository userJpaRepository,
UserEntityConverter userEntityConverter) {
this.groupJpaRepository = groupJpaRepository;
this.groupEntityConverter = groupEntityConverter;
this.membershipJpaRepository = membershipJpaRepository;
this.postEntityConverter = postEntityConverter;
this.superGroupJpaRepository = superGroupJpaRepository;
this.postJpaRepository = postJpaRepository;
this.userJpaRepository = userJpaRepository;
this.userEntityConverter = userEntityConverter;
}

@Override
Expand Down Expand Up @@ -133,6 +134,18 @@ public Optional<Group> get(GroupId groupId) {
.map(this.groupEntityConverter::toDomain);
}

@Override
public List<GroupMember> getAllMembersBySuperGroup(SuperGroupId superGroupId) {
return this.membershipJpaRepository.findAllBySuperGroup(superGroupId.value()).stream()
.map(
membershipEntity ->
new GroupMember(
this.postEntityConverter.toDomain(membershipEntity.getId().getPost()),
new UnofficialPostName(membershipEntity.getUnofficialPostName()),
this.userEntityConverter.toDomain(membershipEntity.getId().getUser())))
.toList();
}

private GroupEntity toEntity(Group group) {
GroupEntity entity =
this.groupJpaRepository.findById(group.id().value()).orElse(new GroupEntity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
import java.util.List;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface MembershipJpaRepository extends JpaRepository<MembershipEntity, MembershipPK> {
List<MembershipEntity> findAllById_Post_Id(UUID postId);

List<MembershipEntity> findAllById_User_Id(UUID userId);

@Query(
"SELECT DISTINCT g.members "
+ "FROM GroupEntity g "
+ "WHERE g.superGroup.id = :superGroupId")
List<MembershipEntity> findAllBySuperGroup(@Param("superGroupId") UUID superGroupId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import it.chalmers.gamma.app.authentication.UserAccessGuard;
import it.chalmers.gamma.app.common.Email;
import it.chalmers.gamma.app.image.domain.ImageUri;
import it.chalmers.gamma.app.settings.domain.Settings;
import it.chalmers.gamma.app.settings.domain.SettingsRepository;
import it.chalmers.gamma.app.user.domain.*;
import org.springframework.lang.Nullable;
Expand All @@ -22,7 +21,6 @@ public UserEntityConverter(
}

@Nullable public GammaUser toDomain(UserEntity userEntity) {
Settings settings = this.settingsRepository.getSettings();
UserId userId = new UserId(userEntity.id);

if (!userAccessGuard.haveAccessToUser(userId, userEntity.locked)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ void save(Group group)

Optional<Group> get(GroupId groupId);

List<GroupMember> getAllMembersBySuperGroup(SuperGroupId superGroupId);

class GroupNotFoundException extends Exception {}

class GroupNameAlreadyExistsException extends Exception {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package it.chalmers.gamma.app.settings;

import static it.chalmers.gamma.app.authentication.AccessGuard.isAdmin;
import static it.chalmers.gamma.app.authentication.AccessGuard.isApi;

import it.chalmers.gamma.app.Facade;
import it.chalmers.gamma.app.apikey.domain.ApiKeyType;
import it.chalmers.gamma.app.authentication.AccessGuard;
import it.chalmers.gamma.app.settings.domain.SettingsRepository;
import it.chalmers.gamma.app.supergroup.domain.SuperGroupType;
Expand Down Expand Up @@ -31,7 +33,7 @@ public void setInfoSuperGroupTypes(List<String> superGroupTypes) {
}

public List<String> getInfoApiSuperGroupTypes() {
this.accessGuard.require(isAdmin());
this.accessGuard.requireEither(isAdmin(), isApi(ApiKeyType.INFO));

return this.settingsRepository.getSettings().infoSuperGroupTypes().stream()
.map(SuperGroupType::value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
package it.chalmers.gamma.app.supergroup;

import static it.chalmers.gamma.app.authentication.AccessGuard.isAdmin;
import static it.chalmers.gamma.app.authentication.AccessGuard.isSignedIn;
import static it.chalmers.gamma.app.authentication.AccessGuard.*;

import it.chalmers.gamma.app.Facade;
import it.chalmers.gamma.app.apikey.domain.ApiKeyType;
import it.chalmers.gamma.app.authentication.AccessGuard;
import it.chalmers.gamma.app.common.PrettyName;
import it.chalmers.gamma.app.common.Text;
import it.chalmers.gamma.app.group.GroupFacade;
import it.chalmers.gamma.app.group.domain.GroupRepository;
import it.chalmers.gamma.app.supergroup.domain.*;
import it.chalmers.gamma.app.user.domain.Name;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import org.springframework.stereotype.Service;

@Service
public class SuperGroupFacade extends Facade {

private final SuperGroupRepository superGroupRepository;
private final SuperGroupTypeRepository superGroupTypeRepository;
private final GroupRepository groupRepository;

public SuperGroupFacade(
AccessGuard accessGuard,
SuperGroupRepository superGroupRepository,
SuperGroupTypeRepository superGroupTypeRepository) {
SuperGroupTypeRepository superGroupTypeRepository,
GroupRepository groupRepository) {
super(accessGuard);
this.superGroupRepository = superGroupRepository;
this.superGroupTypeRepository = superGroupTypeRepository;
this.groupRepository = groupRepository;
}

public void addType(String type)
Expand All @@ -50,6 +53,33 @@ public List<String> getAllTypes() {
return this.superGroupTypeRepository.getAll().stream().map(SuperGroupType::value).toList();
}

public List<SuperGroupTypeDTO> getAllTypesWithSuperGroups(List<String> superGroupTypes) {
accessGuard.requireEither(isAdmin(), isApi(ApiKeyType.INFO), isApi(ApiKeyType.GOLDAPPS));

List<SuperGroupTypeDTO> output = new ArrayList<>();

for (SuperGroupType type : superGroupTypes.stream().map(SuperGroupType::new).toList()) {
List<SuperGroupWithMembersDTO> superGroupsOutput = new ArrayList<>();
for (SuperGroup superGroup : this.superGroupRepository.getAllByType(type)) {
List<GroupFacade.GroupMemberDTO> members =
this.groupRepository.getAllMembersBySuperGroup(superGroup.id()).stream()
.map(GroupFacade.GroupMemberDTO::new)
.toList();

superGroupsOutput.add(new SuperGroupWithMembersDTO(new SuperGroupDTO(superGroup), members));
}

output.add(new SuperGroupTypeDTO(type.value(), superGroupsOutput));
}

return output;
}

public record SuperGroupWithMembersDTO(
SuperGroupDTO superGroup, List<GroupFacade.GroupMemberDTO> members) {}

public record SuperGroupTypeDTO(String type, List<SuperGroupWithMembersDTO> superGroups) {}

public UUID createSuperGroup(NewSuperGroup newSuperGroup)
throws SuperGroupRepository.SuperGroupAlreadyExistsException {
accessGuard.require(isAdmin());
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ server:
path: "/error"
http2:
enabled: true
forward-headers-strategy: native
forward-headers-strategy: "native"
logging:
level:
root: "${ROOT_DEBUG_LEVEL:INFO}"
Expand Down

0 comments on commit 2d912ad

Please sign in to comment.