Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
BE-#99:Update user settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Flugschnitzel committed Apr 24, 2024
1 parent d7e3017 commit ec5d508
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.dhbw.get2gether.backend.user.adapter.in;

import com.dhbw.get2gether.backend.user.application.UserService;
import com.dhbw.get2gether.backend.user.model.UpdateUserCommand;
import com.dhbw.get2gether.backend.user.model.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticatedPrincipal;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {
Expand All @@ -35,4 +35,9 @@ public User user(@AuthenticationPrincipal AuthenticatedPrincipal principal) {
public User user(@PathVariable String id) {
return userService.getUserById(id);
}

@PutMapping("user/self")
public User updateUser(@AuthenticationPrincipal AuthenticatedPrincipal principal, @RequestBody UpdateUserCommand updateUserCommand){
return userService.updateUser(principal, updateUserCommand);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.dhbw.get2gether.backend.user.application;

import com.dhbw.get2gether.backend.user.model.CreateUserCommand;
import com.dhbw.get2gether.backend.user.model.UpdateUserCommand;
import com.dhbw.get2gether.backend.user.model.SyncUserCommand;
import com.dhbw.get2gether.backend.user.model.User;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -34,17 +34,17 @@ private void processOAuthUser(OAuth2User oAuth2User) {
user -> updateUser(oAuth2User, createUpdateCommand(user, oAuth2User)), () -> createNewUser(oAuth2User));
}

private UpdateUserCommand createUpdateCommand(User user, OAuth2User oAuth2User) {
return UpdateUserCommand.builder()
private SyncUserCommand createUpdateCommand(User user, OAuth2User oAuth2User) {
return SyncUserCommand.builder()
.firstName(oAuth2User.getAttribute("given_name"))
.lastName(oAuth2User.getAttribute("family_name"))
.email(oAuth2User.getAttribute("email"))
.profilePictureUrl(oAuth2User.getAttribute("picture"))
.build();
}

private void updateUser(OAuth2User principal, UpdateUserCommand updateUserCommand) {
userService.updateUser(principal, updateUserCommand);
private void updateUser(OAuth2User principal, SyncUserCommand syncUserCommand) {
userService.syncUser(principal, syncUserCommand);
}

private void createNewUser(OAuth2User oAuth2User) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
import com.dhbw.get2gether.backend.exceptions.EntityNotFoundException;
import com.dhbw.get2gether.backend.user.adapter.out.UserRepository;
import com.dhbw.get2gether.backend.user.application.mapper.UserMapper;
import com.dhbw.get2gether.backend.user.model.CreateUserCommand;
import com.dhbw.get2gether.backend.user.model.Guest;
import com.dhbw.get2gether.backend.user.model.UpdateUserCommand;
import com.dhbw.get2gether.backend.user.model.User;
import com.dhbw.get2gether.backend.user.model.*;

import java.time.LocalDateTime;
import java.util.Optional;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.AuthenticatedPrincipal;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -57,13 +56,22 @@ protected User createUser(CreateUserCommand command) {
User user = userMapper.mapToUser(command).toBuilder()
.id(UUID.randomUUID().toString())
.creationDate(LocalDateTime.now())
.settings(Settings.builder()
.colorMode(ColorMode.LIGHT)
.build())
.build();
return userRepository.insert(user);
}

protected User updateUser(OAuth2User principal, UpdateUserCommand updateUserCommand) {
protected User syncUser(OAuth2User principal, SyncUserCommand syncUserCommand) {
User oldUser = getUserByPrincipal(principal);
User newUser = userMapper.mapSyncCommandToUser(oldUser, syncUserCommand);
return userRepository.save(newUser);
}
@PreAuthorize("hasRole('USER')")
public User updateUser(AuthenticatedPrincipal principal, UpdateUserCommand updateUserCommand) {
User oldUser = getUserByPrincipal(principal);
User newUser = userMapper.updateUser(oldUser, updateUserCommand);
User newUser = userMapper.mapSettingsCommandToUser(oldUser, updateUserCommand);
return userRepository.save(newUser);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dhbw.get2gether.backend.user.model.CreateUserCommand;
import com.dhbw.get2gether.backend.user.model.UpdateUserCommand;
import com.dhbw.get2gether.backend.user.model.SyncUserCommand;
import com.dhbw.get2gether.backend.user.model.User;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
Expand All @@ -13,5 +14,8 @@ public interface UserMapper {
@Mapping(target = "id", ignore = true)
User mapToUser(CreateUserCommand command);

User updateUser(@MappingTarget User user, UpdateUserCommand command);

User mapSyncCommandToUser(@MappingTarget User user, SyncUserCommand command);

User mapSettingsCommandToUser(@MappingTarget User user, UpdateUserCommand command);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.dhbw.get2gether.backend.user.model;

public enum ColorMode {
LIGHT,
DARK,
MODERN


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Guest extends User {
private final boolean isGuest = true;

public Guest(String id, LocalDateTime creationDate) {
super(id, creationDate, "Guest", "", "", getProfilePictureUrl(id));
super(id, creationDate, "Guest", "", "", getProfilePictureUrl(id), new Settings(ColorMode.LIGHT));
}

private static String getProfilePictureUrl(String hash) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.dhbw.get2gether.backend.user.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Settings {
private ColorMode colorMode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.dhbw.get2gether.backend.user.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Builder
@AllArgsConstructor
@Getter
public class SyncUserCommand {
private String firstName;
private String lastName;
private String email;
private String profilePictureUrl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class UpdateUserCommand {
private String firstName;
private String lastName;
private String email;
private String profilePictureUrl;
private Settings settings;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;

@Builder(toBuilder = true)
@AllArgsConstructor
@Getter
@Setter
public class User {
@Id
private final String id;
Expand All @@ -18,4 +20,5 @@ public class User {
private String lastName;
private String email;
private String profilePictureUrl;
private Settings settings;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<button (click)="getOwn()">GetMyEvents</button>
<button (click)="generateInvitationLink()">GenerateInvitationLink</button>
<button (click)="updateEvent()">UpdateEvent</button>
<button (click)="updateUserColor('LIGHT')">Lightmode</button>
<button (click)="updateUserColor('DARK')">Darkmode</button>
<button (click)="updateUserColor('MODERN')">Modernmode</button>
<!-- TODO: remove above -->

<!-- TODO: add new component here for event banner -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@ export class DashboardContentComponent {
console.log(result);
})
}
updateUserColor( colormode: string){
this.http.put("http://localhost:8080/user/self", {
"settings": {
"colorMode" : colormode
}
},{withCredentials: true}).subscribe(result =>{
console.log(result);
})
}
}

0 comments on commit ec5d508

Please sign in to comment.