Skip to content

Commit 2fbfb76

Browse files
Feature / Matching Service Implementation V2 (#68)
2 parents ab72bc2 + b4f6cd7 commit 2fbfb76

File tree

15 files changed

+1297
-75
lines changed

15 files changed

+1297
-75
lines changed

server/matching/src/main/java/meet_at_mensa/matching/MatchingApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.scheduling.annotation.EnableScheduling;
56
import org.springframework.web.bind.annotation.GetMapping;
67
import org.springframework.web.bind.annotation.RestController;
78
import java.util.concurrent.TimeUnit;
89

910
@SpringBootApplication
11+
@EnableScheduling
1012
// MatchingApplication is the main class for the matching application
1113
public class MatchingApplication {
1214

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,209 @@
11
package meet_at_mensa.matching.client;
22

3+
import org.openapitools.client.Configuration;
4+
import org.openapitools.client.api.GenAiApi;
5+
import org.springframework.stereotype.Service;
6+
7+
import meet_at_mensa.matching.exception.RestException;
8+
import meet_at_mensa.matching.repository.GroupRepository;
9+
import meet_at_mensa.matching.service.ConversationStarterService;
10+
11+
import org.openapitools.client.ApiClient;
12+
13+
import org.openapitools.model.User;
14+
import org.openapitools.model.UserCollection;
15+
import org.openapitools.model.ConversationStarter;
16+
import org.openapitools.model.ConversationStarterCollection;
17+
18+
import org.openapitools.client.auth.*;
19+
20+
/**
21+
* GenAiClient uses the generated java client to handle REST requests to the GenAi-Microservice
22+
*
23+
* WARNING: This class is currently non-functional due to complications with Auth0
24+
*
25+
* TODO: @AK - Implement Authentication
26+
*
27+
*/
28+
@Service
329
public class GenAiClient {
30+
31+
private ApiClient defaultClient;
32+
33+
public GenAiClient() {
34+
35+
// get default client
36+
this.defaultClient = Configuration.getDefaultApiClient();
37+
38+
// set path
39+
this.defaultClient.setBasePath("http://meetatmensa-genai:80");
40+
41+
}
42+
43+
44+
/**
45+
* Uses a Generated client to a REST request to genai-service for a ConverationStarterCollection object
46+
*
47+
* WARNING: This currently fails due to missing authentication!
48+
*
49+
* TODO: @AK Figure out auth0
50+
*
51+
* @param users server-style UserCollection object of the user being fetched
52+
* @return server-style ConversationStarterCollection object (org.openapitools.model.ConversationStarterCollection)
53+
*/
54+
public ConversationStarterCollection getPrompts(UserCollection users) {
55+
56+
// create instance of the API
57+
GenAiApi apiInstance = new GenAiApi(this.defaultClient);
58+
59+
try {
60+
61+
// initiate object
62+
org.openapitools.client.model.ConversationStarterCollection clientCollection;
63+
64+
// try to fetch prompts from ConversationStarter
65+
clientCollection = apiInstance.getApiV2GenaiConversationStarter(
66+
67+
// convert from server-type to client-type
68+
convertServerUserCollectionToClientUserCollection(users)
69+
70+
);
71+
72+
// convert from client-type to server-type
73+
return convertClientPromptsToServerPrompts(clientCollection);
74+
75+
} catch (Exception e) {
76+
77+
throw new RestException(e.toString());
78+
79+
}
80+
}
81+
82+
83+
84+
/**
85+
* Converts a Server-Style User object into a Client-Style User object.
86+
*
87+
* Generated clients and servers are generated separatly, and each have their own version of model objects
88+
* A user from org.openapitools.model.User and a user from org.openapitools.client.model.User have the same
89+
* fields, but are distinct in a formal sense.
90+
*
91+
* These conversion functions convert between client-type objects returned by generated client functions
92+
* and server-type objects used by the rest of the application
93+
*
94+
* @param serverUser server-style user object (org.openapitools.model.User)
95+
* @return clientUser client-style user object (org.openapitools.client.model.User)
96+
*/
97+
private org.openapitools.client.model.User convertServerUserToClientUser(User serverUser) {
98+
99+
// create new object of client type
100+
org.openapitools.client.model.User clientUser = new org.openapitools.client.model.User();
101+
102+
// copy fields
103+
clientUser.setUserID(serverUser.getUserID());
104+
clientUser.setEmail(serverUser.getEmail());
105+
clientUser.setFirstname(serverUser.getFirstname());
106+
clientUser.setLastname(serverUser.getLastname());
107+
clientUser.setBirthday(serverUser.getBirthday());
108+
clientUser.setGender(serverUser.getGender());
109+
clientUser.setDegree(serverUser.getDegree());
110+
clientUser.setDegreeStart(serverUser.getDegreeStart());
111+
clientUser.setInterests(serverUser.getInterests());
112+
clientUser.setBio(serverUser.getBio());
113+
114+
// return object
115+
return clientUser;
116+
117+
}
118+
119+
120+
/**
121+
* Converts a Server-Style UserCollection object into a Client-Style UserCollection object.
122+
*
123+
* Generated clients and servers are generated separatly, and each have their own version of model objects
124+
* A user from org.openapitools.model.User and a user from org.openapitools.client.model.User have the same
125+
* fields, but are distinct in a formal sense.
126+
*
127+
* These conversion functions convert between client-type objects returned by generated client functions
128+
* and server-type objects used by the rest of the application
129+
*
130+
* @param serverUsers server-style userCollection object (org.openapitools.model.UserCollection)
131+
* @return clientUsers client-style userCollection object (org.openapitools.client.model.UserCollection)
132+
*/
133+
private org.openapitools.client.model.UserCollection convertServerUserCollectionToClientUserCollection(UserCollection serverUsers) {
134+
135+
// create new client-type UserCollection
136+
org.openapitools.client.model.UserCollection clientUsers = new org.openapitools.client.model.UserCollection();
137+
138+
// convert to client-user and add to collection
139+
for (User serverUser : serverUsers.getUsers()) {
140+
clientUsers.addUsersItem(
141+
convertServerUserToClientUser(serverUser)
142+
);
143+
}
144+
145+
// return object
146+
return clientUsers;
147+
148+
}
149+
4150

151+
/**
152+
* Converts a Client-Style ConversationStarter object into a Server-Style ConversationStarter object.
153+
*
154+
* Generated clients and servers are generated separatly, and each have their own version of model objects
155+
* A user from org.openapitools.model.User and a user from org.openapitools.client.model.User have the same
156+
* fields, but are distinct in a formal sense.
157+
*
158+
* These conversion functions convert between client-type objects returned by generated client functions
159+
* and server-type objects used by the rest of the application
160+
*
161+
* @param clientPromt Client-Style ConversationStarter object (org.openapitools.client.model.ConversationStarter)
162+
* @return serverPrompt Server-Style ConversationStarter object (org.openapitools.model.ConversationStarter)
163+
*/
164+
private ConversationStarter convertClientPromptToServerPrompt(org.openapitools.client.model.ConversationStarter clientPrompt) {
165+
166+
// create new object of server type
167+
ConversationStarter serverPrompt = new ConversationStarter();
168+
169+
// copy fields
170+
serverPrompt.setPrompt(clientPrompt.getPrompt());
171+
172+
// return server object
173+
return serverPrompt;
174+
175+
}
176+
177+
178+
/**
179+
* Converts a Client-Style ConversationStarterCollection object into a Server-Style ConversationStarterCollection object.
180+
*
181+
* Generated clients and servers are generated separatly, and each have their own version of model objects
182+
* A user from org.openapitools.model.User and a user from org.openapitools.client.model.User have the same
183+
* fields, but are distinct in a formal sense.
184+
*
185+
* These conversion functions convert between client-type objects returned by generated client functions
186+
* and server-type objects used by the rest of the application
187+
*
188+
* @param clientPromts Client-Style ConversationStarterCollection object (org.openapitools.client.model.ConversationStarterCollection)
189+
* @return serverPrompts Server-Style ConversationStarterCollection object (org.openapitools.model.ConversationStarterCollection)
190+
*/
191+
private ConversationStarterCollection convertClientPromptsToServerPrompts(org.openapitools.client.model.ConversationStarterCollection clientPrompts) {
192+
193+
// create new object of server type
194+
ConversationStarterCollection serverPrompts = new ConversationStarterCollection();
195+
196+
// convert to server-type and add to collection
197+
for (org.openapitools.client.model.ConversationStarter clientPrompt : clientPrompts.getConversationsStarters()) {
198+
199+
serverPrompts.addConversationsStartersItem(
200+
convertClientPromptToServerPrompt(clientPrompt)
201+
);
202+
}
203+
204+
// return server object
205+
return serverPrompts;
206+
207+
}
208+
5209
}

server/matching/src/main/java/meet_at_mensa/matching/client/UserClient.java

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,55 @@
77
import org.openapitools.client.Configuration;
88
import org.openapitools.client.auth.*;
99
import org.openapitools.client.api.UserApi;
10-
import org.openapitools.client.model.User;
1110

11+
import org.openapitools.model.User;
1212
import org.openapitools.model.UserCollection;
1313
import org.springframework.stereotype.Service;
1414

1515
import meet_at_mensa.matching.exception.RestException;
1616

17+
/**
18+
* User Client uses the generated java client to handle REST requests to the User-Microservice
19+
*
20+
* WARNING: This class is currently non-functional due to complications with Auth0
21+
*
22+
* TODO: @AK - Implement Authentication
23+
*
24+
*/
1725
@Service
1826
public class UserClient {
1927

28+
// API client object
2029
private ApiClient defaultClient;
2130

31+
// Constructor
2232
public UserClient() {
2333

2434
// get default client
2535
this.defaultClient = Configuration.getDefaultApiClient();
2636

2737
// set path
28-
this.defaultClient.setBasePath("meetatmensa-user");
38+
this.defaultClient.setBasePath("http://meetatmensa-user:80");
2939
}
3040

31-
private org.openapitools.model.User convertUser(User clientUser) {
3241

33-
return new org.openapitools.model.User(
42+
43+
/**
44+
* Converts a Client-Style User object into a Server-Style User object.
45+
*
46+
* Generated clients and servers are generated separatly, and each have their own version of model objects
47+
* A user from org.openapitools.model.User and a user from org.openapitools.client.model.User have the same
48+
* fields, but are distinct in a formal sense.
49+
*
50+
* These conversion functions convert between client-type objects returned by generated client functions
51+
* and server-type objects used by the rest of the application
52+
*
53+
* @param clientUser client-style user object (org.openapitools.client.model.User)
54+
* @return serverUser server-style user object (org.openapitools.model.User)
55+
*/
56+
private User convertClientUserToServerUser(org.openapitools.client.model.User clientUser) {
57+
58+
return new User(
3459
clientUser.getUserID(),
3560
clientUser.getEmail(),
3661
clientUser.getFirstname(),
@@ -46,25 +71,47 @@ private org.openapitools.model.User convertUser(User clientUser) {
4671
}
4772

4873

49-
50-
public org.openapitools.model.User getUser(UUID userID) {
74+
/**
75+
* Uses the Generated client to send a REST request to user-service for a User object
76+
*
77+
* WARNING: This currently fails due to missing authentication
78+
*
79+
* TODO: @AK Figure out auth0
80+
*
81+
* @param userID userID of the user being fetched
82+
* @return serverUser server-style user object (org.openapitools.model.User)
83+
*/
84+
public User getUser(UUID userID) {
5185

5286
// create instance of the API
5387
UserApi apiInstance = new UserApi(this.defaultClient);
5488

5589
try {
5690

91+
org.openapitools.client.model.User userClient;
92+
5793
// request user Object from user-service
58-
User userClient = apiInstance.getApiV2UserUserID(userID);
94+
userClient = apiInstance.getApiV2UserUserID(userID);
5995

6096
// convert to server-type object and return
61-
return convertUser(userClient);
97+
return convertClientUserToServerUser(userClient);
6298

6399
} catch (Exception e) {
64-
throw new RestException();
100+
throw new RestException(e.toString());
65101
}
66102
}
67103

104+
105+
/**
106+
* Uses the Generated client to send multiple REST requests to user-service for multiple User objects
107+
*
108+
* WARNING: This currently fails due to missing authentication
109+
*
110+
* TODO: @AK Figure out auth0
111+
*
112+
* @param userIDs userID of the user being fetched
113+
* @return serverUserCollection server-style user object (org.openapitools.model.UserCollection)
114+
*/
68115
public UserCollection getUsers(List<UUID> userIDs) {
69116

70117
// create empty UserCollection

server/matching/src/main/java/meet_at_mensa/matching/controller/MatchingController.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.springframework.http.ResponseEntity;
66
import org.springframework.web.bind.annotation.RestController;
77

8-
import jakarta.validation.Valid;
8+
import meet_at_mensa.matching.client.UserClient;
99
import meet_at_mensa.matching.exception.MatchNotFoundException;
1010
import meet_at_mensa.matching.exception.RequestNotFoundException;
1111
import meet_at_mensa.matching.exception.RequestOverlapException;
@@ -17,6 +17,10 @@
1717

1818
import org.openapitools.api.MatchingApi;
1919
import org.openapitools.model.*;
20+
import org.springframework.web.bind.annotation.GetMapping;
21+
import org.springframework.web.bind.annotation.PathVariable;
22+
import org.springframework.web.bind.annotation.RequestParam;
23+
2024

2125
@RestController
2226
public class MatchingController implements MatchingApi {
@@ -134,7 +138,7 @@ public ResponseEntity<Void> getApiV2MatchingRsvpMatchIdReject(UUID matchId) {
134138

135139
// POST @ api/v2/matching/request/submit
136140
@Override
137-
public ResponseEntity<MatchRequest> postApiV2MatchingRequestSubmit(@Valid MatchRequestNew matchRequestNew) {
141+
public ResponseEntity<MatchRequest> postApiV2MatchingRequestSubmit(MatchRequestNew matchRequestNew) {
138142

139143
// 200
140144
try {
@@ -162,7 +166,7 @@ public ResponseEntity<MatchRequest> postApiV2MatchingRequestSubmit(@Valid MatchR
162166

163167
// PUT @ api/v2/matching/request/{requestID}
164168
@Override
165-
public ResponseEntity<MatchRequest> putApiV2MatchingRequestRequestId(UUID requestId, @Valid MatchRequestUpdate matchRequestUpdate) {
169+
public ResponseEntity<MatchRequest> putApiV2MatchingRequestRequestId(UUID requestId, MatchRequestUpdate matchRequestUpdate) {
166170

167171
// 200
168172
try {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package meet_at_mensa.matching.exception;
2+
3+
public class ScheduleException extends RuntimeException {
4+
5+
public ScheduleException() {
6+
super("This operation cannot be run at this time!");
7+
}
8+
9+
public ScheduleException(String message) {
10+
super(message);
11+
}
12+
13+
public ScheduleException(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)