Skip to content

Commit

Permalink
create settings endpoint #248
Browse files Browse the repository at this point in the history
  • Loading branch information
iam-flo committed Mar 7, 2025
1 parent 6d3abfa commit e794be4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


@RestController
@RequestMapping("/user")
Expand All @@ -27,6 +30,9 @@ public class UserController {
@Autowired
private Keycloak keycloak;

@Autowired
private UserRepository userRepository;

@Value("${keycloak.realm}")
private String realm;

Expand Down Expand Up @@ -56,4 +62,26 @@ public ResponseEntity<Void> deleteUser(JwtAuthenticationToken auth) {
}
return ResponseEntity.ok().build();
}

@GetMapping("/settings")
public ResponseEntity<UserSettingsDTO> getUserSettings() {
var user = userRepository.getCurrentUser();
if (user.isEmpty()) {
return ResponseEntity.notFound().build();
}

UserSettingsDTO userSettings = userService.getUserSettings(user.get());
return ResponseEntity.ok(userSettings);
}

@PostMapping("/settings")
public ResponseEntity<UserSettingsDTO> updateUserSettings(@RequestBody UserSettingsDTO userSettings) {
var user = userRepository.getCurrentUser();
if (user.isEmpty()) {
return ResponseEntity.notFound().build();
}

UserSettingsDTO updatedUserSettings = userService.updateUserSettings(user.get(), userSettings);
return ResponseEntity.ok(updatedUserSettings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,16 @@ public Optional<UserProfileDTO> getUserProfile(String login) {
new UserProfileDTO(user, firstContribution, contributedRepositories, reviewActivity, openPullRequests)
);
}

public UserSettingsDTO getUserSettings(User user) {
logger.info("Getting user settings with userId: " + user);
return new UserSettingsDTO(user.isNotificationsEnabled());
}

public UserSettingsDTO updateUserSettings(User user, UserSettingsDTO userSettings) {
logger.info("Updating user settings with userId: " + user);
user.setNotificationsEnabled(userSettings.receiveNotifications());
userRepository.save(user);
return new UserSettingsDTO(user.isNotificationsEnabled());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package de.tum.in.www1.hephaestus.gitprovider.user;

public record UserSettingsDTO(boolean receiveNotifications) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<changeSet author="flo (generated)" id="1740410752168-1">
<addColumn tableName="user">
<column name="notifications_enabled" type="boolean" defaultValue="false">
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>
</databaseChangeLog>
1 change: 1 addition & 0 deletions server/application-server/src/main/resources/db/master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
<include file="./changelog/1737749442154_changelog.xml" relativeToChangelogFile="true"/>
<include file="./changelog/1739871861123_changelog.xml" relativeToChangelogFile="true"/>
<include file="./changelog/1740998214558_changelog.xml" relativeToChangelogFile="true"/>
<include file="./changelog/1740410752168_changelog.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

0 comments on commit e794be4

Please sign in to comment.