Skip to content

Commit

Permalink
Remove global settings, and add api type specific settings to info an…
Browse files Browse the repository at this point in the history
…d account_scaffold (previously goldapps)
  • Loading branch information
Portals committed Mar 23, 2024
1 parent 2d912ad commit 443afcb
Show file tree
Hide file tree
Showing 45 changed files with 712 additions and 619 deletions.
3 changes: 0 additions & 3 deletions app/src/main/java/it/chalmers/gamma/BootstrapRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class BootstrapRunner {
public CommandLineRunner runBootStrap(
ApiKeyBootstrap apiKeyBootstrap,
EnsureAnAdminUserBootstrap ensureAnAdminUserBootstrap,
EnsureSettingsBootstrap ensureSettingsBootstrap,
GroupBootstrap groupBootstrap,
MiscBootstrap miscBootstrap,
PostBootstrap postBootstrap,
Expand All @@ -26,8 +25,6 @@ public CommandLineRunner runBootStrap(

miscBootstrap.runImageBootstrap();

ensureSettingsBootstrap.ensureAppSettings();

ensureAnAdminUserBootstrap.ensureAnAdminUser();

userBootstrap.createUsers();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package it.chalmers.gamma.adapter.primary.api.accountscaffold;

import it.chalmers.gamma.app.accountscaffold.AccountScaffoldFacade;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Used by, for example, <a href="https://github.com/cthit/goldapps">cthit/goldapps</a> to sync
* Google accounts for the student division.
*/
@RestController
@RequestMapping(AccountScaffoldV1ApiController.URI)
public class AccountScaffoldV1ApiController {

public static final String URI = "/api/account-scaffold/v1";

private final AccountScaffoldFacade accountScaffoldFacade;

public AccountScaffoldV1ApiController(AccountScaffoldFacade accountScaffoldFacade) {
this.accountScaffoldFacade = accountScaffoldFacade;
}

@GetMapping("/supergroups")
public List<AccountScaffoldFacade.AccountScaffoldSuperGroupDTO> getSuperGroups() {
return this.accountScaffoldFacade.getActiveSuperGroups();
}

@GetMapping("/users")
public List<AccountScaffoldFacade.AccountScaffoldUserDTO> getUsers() {
return this.accountScaffoldFacade.getActiveUsers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/** If you need changes, then create a new version of the API. */
@RestController
@RequestMapping(ClientApiV1Controller.URI)
public class ClientApiV1Controller {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package it.chalmers.gamma.adapter.primary.api.info;

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;
Expand All @@ -14,8 +12,7 @@

/**
* Going to be used by chalmers.it to display groups and their members. A separate API since user
* info are going to be returned, and that should be used with caution. If you need changes, then
* create a new version of the API.
* info are going to be returned, and that should be used with caution.
*/
@RestController
@RequestMapping(InfoV1ApiController.URI)
Expand All @@ -25,13 +22,10 @@ public class InfoV1ApiController {

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

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

@GetMapping("/users/{id}")
Expand All @@ -41,11 +35,8 @@ public UserFacade.UserWithGroupsDTO getUser(@PathVariable("id") UUID id) {

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

public record GroupsResponse(List<GroupFacade.GroupWithMembersDTO> groups) {}

private static class UserNotFoundResponse extends NotFoundResponse {}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package it.chalmers.gamma.adapter.primary.web;

import it.chalmers.gamma.app.apikey.ApiKeyFacade;
import it.chalmers.gamma.app.apikey.ApiKeySettingsFacade;
import it.chalmers.gamma.app.supergroup.SuperGroupFacade;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand All @@ -13,9 +16,16 @@
public class ApiKeyController {

private final ApiKeyFacade apiKeyFacade;
private final ApiKeySettingsFacade apiKeySettingsFacade;
private final SuperGroupFacade superGroupFacade;

public ApiKeyController(ApiKeyFacade apiKeyFacade) {
public ApiKeyController(
ApiKeyFacade apiKeyFacade,
ApiKeySettingsFacade apiKeySettingsFacade,
SuperGroupFacade superGroupFacade) {
this.apiKeyFacade = apiKeyFacade;
this.apiKeySettingsFacade = apiKeySettingsFacade;
this.superGroupFacade = superGroupFacade;
}

@GetMapping("/api-keys")
Expand All @@ -36,24 +46,58 @@ public ModelAndView getApiKeys(
return mv;
}

private void loadApiKeySettingsInfo(ModelAndView mv, UUID apiKeyId) {
var settings = this.apiKeySettingsFacade.getInfoSettings(apiKeyId);

mv.addObject("settings_title", "Info settings");
mv.addObject(
"settings_description",
"Set the super group types from which the information will be query from");
mv.addObject(
"settings_form", new ApiKeySettingsForm(settings.version(), settings.superGroupTypes()));
}

private void loadApiKeySettingsAccountScaffold(ModelAndView mv, UUID apiKeyId) {
var settings = this.apiKeySettingsFacade.getAccountScaffoldSettings(apiKeyId);

mv.addObject("settings_title", "Account scaffold settings");
mv.addObject(
"settings_description",
"Set the super group types from which the information will query from");
mv.addObject(
"settings_form", new ApiKeySettingsForm(settings.version(), settings.superGroupTypes()));
}

public record ApiKeySettingsForm(int version, List<String> superGroupTypes) {}

@GetMapping("/api-keys/{id}")
public ModelAndView getApiKey(
@RequestHeader(value = "HX-Request", required = false) boolean htmxRequest,
@PathVariable("id") UUID id) {
Optional<ApiKeyFacade.ApiKeyDTO> apiKey = this.apiKeyFacade.getById(id);
Optional<ApiKeyFacade.ApiKeyDTO> maybeApiKey = this.apiKeyFacade.getById(id);

if (apiKey.isEmpty()) {
if (maybeApiKey.isEmpty()) {
throw new RuntimeException();
}

ApiKeyFacade.ApiKeyDTO apiKey = maybeApiKey.get();

ModelAndView mv = new ModelAndView();
if (htmxRequest) {
mv.setViewName("pages/api-key-details");
} else {
mv.setViewName("index");
mv.addObject("page", "pages/api-key-details");
}
mv.addObject("apiKey", apiKey.get());

mv.addObject("apiKey", apiKey);
mv.addObject("apiKeyId", apiKey.id());

if (apiKey.keyType().equals("ACCOUNT_SCAFFOLD")) {
loadApiKeySettingsAccountScaffold(mv, apiKey.id());
} else if (apiKey.keyType().equals("INFO")) {
loadApiKeySettingsInfo(mv, apiKey.id());
}

return mv;
}
Expand Down Expand Up @@ -111,4 +155,73 @@ public ModelAndView deleteApiKey(

return new ModelAndView("redirect:/api-keys");
}

public static final class ApiKeySettings {
public List<String> superGroupTypesForInfo = new ArrayList<>();
public int version;

public List<String> getSuperGroupTypesForInfo() {
return superGroupTypesForInfo;
}

public void setSuperGroupTypesForInfo(List<String> superGroupTypesForInfo) {
this.superGroupTypesForInfo = superGroupTypesForInfo;
}

public int getVersion() {
return version;
}

public void setVersion(int version) {
this.version = version;
}
}

@GetMapping("/api-keys/new-super-group")
public ModelAndView getNewSuperGroupType(
@RequestHeader(value = "HX-Request", required = true) boolean htmxRequest) {
ModelAndView mv = new ModelAndView();

mv.setViewName("partial/new-super-group-type-to-api-settings");
mv.addObject("superGroupTypes", this.superGroupFacade.getAllTypes());

return mv;
}

@PutMapping("/api-keys/{apiKeyId}/settings")
public ModelAndView updateSettings(
@RequestHeader(value = "HX-Request", required = false) boolean htmxRequest,
@PathVariable("apiKeyId") UUID apiKeyId,
ApiKeySettings form) {

Optional<ApiKeyFacade.ApiKeyDTO> apiKeyMaybe = this.apiKeyFacade.getById(apiKeyId);

if (apiKeyMaybe.isEmpty()) {
throw new RuntimeException();
}

ApiKeyFacade.ApiKeyDTO apiKey = apiKeyMaybe.get();

ModelAndView mv = new ModelAndView("partial/api-key-super-group-types");

if (apiKey.keyType().equals("ACCOUNT_SCAFFOLD")) {
this.apiKeySettingsFacade.setAccountScaffoldSettings(
apiKeyId,
new ApiKeySettingsFacade.ApiKeySettingsAccountScaffoldDTO(
form.version, form.superGroupTypesForInfo));

loadApiKeySettingsAccountScaffold(mv, apiKey.id());
} else if (apiKey.keyType().equals("INFO")) {
this.apiKeySettingsFacade.setInfoSettings(
apiKeyId,
new ApiKeySettingsFacade.ApiKeySettingsInfoDTO(
form.version, form.superGroupTypesForInfo));

loadApiKeySettingsInfo(mv, apiKey.id());
}

mv.addObject("apiKeyId", apiKeyId);

return mv;
}
}

This file was deleted.

Loading

0 comments on commit 443afcb

Please sign in to comment.