Skip to content

Commit d85948f

Browse files
committed
Add user preferences logic to server
1 parent eb1aaa1 commit d85948f

File tree

11 files changed

+177
-11
lines changed

11 files changed

+177
-11
lines changed

server/user/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
dependencies {
2+
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
3+
}
4+
15
jib {
26
from {
37
image = "eclipse-temurin:21-jre-alpine"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.continiousdisappointment.user.controller;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RequestHeader;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
12+
import com.continiousdisappointment.user.dto.SaveUserPreferencesRequestDto;
13+
import com.continiousdisappointment.user.service.UserService;
14+
15+
import jakarta.websocket.server.PathParam;
16+
import lombok.RequiredArgsConstructor;
17+
import lombok.extern.log4j.Log4j2;
18+
19+
@Controller
20+
@RequestMapping("/preferences")
21+
@Log4j2
22+
@RequiredArgsConstructor
23+
public class UserPreferencesController {
24+
private final UserService userService;
25+
26+
@PostMapping
27+
public ResponseEntity<Object> saveUserPreferences(@RequestHeader("Authorization") String authorization,
28+
@RequestBody SaveUserPreferencesRequestDto request) {
29+
try {
30+
31+
userService.saveUserPreferences(authorization, request.dietaryPreferences());
32+
33+
} catch (Exception e) {
34+
log.warn("Error while saving user preferences", e);
35+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
36+
}
37+
return null;
38+
}
39+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.continiousdisappointment.user.domain;
2+
3+
public enum DietaryPreference {
4+
VEGETARIAN,
5+
VEGAN,
6+
GLUTEN_FREE,
7+
DAIRY_FREE,
8+
NUT_FREE,
9+
SPICY_FOOD,
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.continiousdisappointment.user.domain;
2+
3+
import lombok.Data;
4+
import lombok.RequiredArgsConstructor;
5+
6+
@Data
7+
@RequiredArgsConstructor
8+
public class OAuthUser {
9+
private final String username;
10+
private final Integer id;
11+
}
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package com.continiousdisappointment.user.domain;
22

3-
public record User(Integer id, String username) {
4-
public User {
5-
if (id == null || id < 0) {
6-
throw new IllegalArgumentException("ID must be a non-negative integer.");
7-
}
8-
if (username == null || username.isBlank()) {
9-
throw new IllegalArgumentException("Username cannot be null or blank.");
10-
}
3+
import java.util.Set;
4+
5+
import org.springframework.lang.NonNull;
6+
7+
public class User extends OAuthUser {
8+
private final Set<DietaryPreference> dietaryPreferences;
9+
10+
public User(String username, Integer id, @NonNull Set<DietaryPreference> dietaryPreferences) {
11+
super(username, id);
12+
this.dietaryPreferences = dietaryPreferences;
13+
}
14+
15+
public Set<DietaryPreference> getDietaryPreferences() {
16+
return dietaryPreferences;
1117
}
1218
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.continiousdisappointment.user.dto;
2+
3+
import java.util.Set;
4+
5+
import com.continiousdisappointment.user.domain.DietaryPreference;
6+
7+
public record SaveUserPreferencesRequestDto(Set<DietaryPreference> dietaryPreferences) {
8+
9+
public SaveUserPreferencesRequestDto {
10+
if (dietaryPreferences == null || dietaryPreferences.isEmpty()) {
11+
throw new IllegalArgumentException("Dietary preferences cannot be null or empty");
12+
}
13+
}
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.continiousdisappointment.user.model;
2+
3+
import java.util.Set;
4+
import java.util.UUID;
5+
6+
import org.springframework.data.annotation.Id;
7+
import org.springframework.data.mongodb.core.mapping.Document;
8+
9+
import com.continiousdisappointment.user.domain.DietaryPreference;
10+
11+
import lombok.AllArgsConstructor;
12+
import lombok.Data;
13+
import lombok.NoArgsConstructor;
14+
15+
@Data
16+
@NoArgsConstructor
17+
@AllArgsConstructor
18+
@Document(collection = "user-preferences")
19+
public class UserPreferencesModel {
20+
@Id
21+
public UUID id;
22+
public int userId;
23+
public Set<DietaryPreference> dietaryPreferences;
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.continiousdisappointment.user.repository;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.UUID;
6+
7+
import org.springframework.data.mongodb.repository.MongoRepository;
8+
import org.springframework.lang.NonNull;
9+
10+
import com.continiousdisappointment.user.model.UserPreferencesModel;
11+
12+
public interface UserPreferencesRepository extends MongoRepository<UserPreferencesModel, UUID> {
13+
14+
List<UserPreferencesModel> findByUserId(int userId);
15+
16+
Optional<UserPreferencesModel> findById(@NonNull UUID id);
17+
}
Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,52 @@
11
package com.continiousdisappointment.user.service;
22

3+
import lombok.RequiredArgsConstructor;
34
import lombok.extern.log4j.Log4j2;
5+
6+
import java.util.Set;
7+
48
import org.springframework.http.*;
59
import org.springframework.stereotype.Service;
610
import org.springframework.web.client.RestTemplate;
711

12+
import com.continiousdisappointment.user.domain.DietaryPreference;
13+
import com.continiousdisappointment.user.domain.OAuthUser;
814
import com.continiousdisappointment.user.domain.User;
15+
import com.continiousdisappointment.user.model.UserPreferencesModel;
16+
import com.continiousdisappointment.user.repository.UserPreferencesRepository;
917

1018
@Log4j2
1119
@Service
20+
@RequiredArgsConstructor
1221
public class UserService {
1322

1423
private final RestTemplate restTemplate = new RestTemplate();
24+
private final UserPreferencesRepository userPreferencesRepository;
1525

1626
public User getUserInfo(String authorization) {
17-
String url = "https://gitlab.lrz.de/api/v4/user";
27+
OAuthUser oAuthUser = getOAuthUserInfo(authorization);
28+
var userPreferences = getUserPreferences(authorization);
29+
return new User(oAuthUser.getUsername(), oAuthUser.getId(), userPreferences);
30+
}
31+
32+
public void saveUserPreferences(String authorization, Set<DietaryPreference> dietaryPreferences) {
33+
OAuthUser oAuthUser = getOAuthUserInfo(authorization);
34+
var userPreferences = new UserPreferencesModel();
35+
userPreferences.setUserId(oAuthUser.getId());
36+
userPreferences.setDietaryPreferences(dietaryPreferences);
37+
userPreferencesRepository.save(userPreferences);
38+
}
39+
40+
private Set<DietaryPreference> getUserPreferences(String authorization) {
41+
OAuthUser oAuthUser = getOAuthUserInfo(authorization);
42+
return userPreferencesRepository.findByUserId(oAuthUser.getId()).stream()
43+
.findFirst()
44+
.map(UserPreferencesModel::getDietaryPreferences)
45+
.orElse(Set.of());
46+
}
47+
48+
private OAuthUser getOAuthUserInfo(String authorization) {
49+
String url = "https://gitlab.lrz.de/api{/v4/user";
1850
if (authorization == null) {
1951
log.warn("No access token found in security context");
2052
throw new IllegalStateException("No access token found in security context");
@@ -24,11 +56,11 @@ public User getUserInfo(String authorization) {
2456

2557
HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
2658

27-
ResponseEntity<User> response = restTemplate.exchange(
59+
ResponseEntity<OAuthUser> response = restTemplate.exchange(
2860
url,
2961
HttpMethod.GET,
3062
requestEntity,
31-
User.class);
63+
OAuthUser.class);
3264
return response.getBody();
3365
}
3466
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring:
2+
data:
3+
mongodb:
4+
uri: mongodb://admin:admin@127.0.0.1:27017/recipai?authSource=admin

0 commit comments

Comments
 (0)