This repository has been archived by the owner on May 22, 2021. It is now read-only.
generated from E-Edu/microservice-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
384 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/de/themorpheus/edu/gateway/backend/BackendAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package de.themorpheus.edu.gateway.backend; | ||
|
||
import lombok.Getter; | ||
|
||
public class BackendAPI { | ||
|
||
@Getter private final UserService userService = new UserService(); | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/de/themorpheus/edu/gateway/backend/Service.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package de.themorpheus.edu.gateway.backend; | ||
|
||
import com.google.gson.FieldNamingPolicy; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import okhttp3.MediaType; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
|
||
@RequiredArgsConstructor | ||
public abstract class Service { | ||
|
||
protected static final Gson GSON = new GsonBuilder() | ||
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) | ||
.create(); | ||
|
||
private static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); | ||
|
||
private final String uri; | ||
|
||
@Getter(AccessLevel.PROTECTED) | ||
private final OkHttpClient client = new OkHttpClient(); | ||
|
||
protected Request.Builder request(String route) { | ||
return new Request.Builder() | ||
.url(uri + route); | ||
} | ||
|
||
protected RequestBody body(Object obj) { | ||
return RequestBody.create(GSON.toJson(obj), JSON); | ||
} | ||
|
||
protected <T> T response(Class<T> clazz, String json) { | ||
return GSON.fromJson(json, clazz); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/de/themorpheus/edu/gateway/backend/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package de.themorpheus.edu.gateway.backend; | ||
|
||
import de.themorpheus.edu.gateway.backend.user.UserCreateDTO; | ||
import de.themorpheus.edu.gateway.backend.user.UserErrorResponseDTO; | ||
import de.themorpheus.edu.gateway.backend.user.UserLoginRequestDTO; | ||
import de.themorpheus.edu.gateway.backend.user.UserLoginResponseDTO; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import javax.annotation.Nullable; | ||
import javax.validation.constraints.NotNull; | ||
import java.io.IOException; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
public class UserService extends Service { | ||
|
||
public UserService() { | ||
super("https://user.e-edu.the-morpheus.de"); | ||
} | ||
|
||
public RegisterResponse createUser( | ||
@NotNull String email, | ||
@NotNull String password, | ||
@NotNull String firstName, | ||
@NotNull String lastName, | ||
@Nullable String teacherToken) throws IOException { | ||
|
||
Request request = request("/user") | ||
.post(body(new UserCreateDTO( | ||
email, | ||
password, | ||
firstName, | ||
lastName, | ||
teacherToken | ||
))) | ||
.build(); | ||
try (Response response = getClient().newCall(request).execute()) { | ||
if (response.code() != 201) return response(RegisterResponse.class, response.body().string()); | ||
return new RegisterResponse(null); | ||
} | ||
} | ||
|
||
public LoginResponse login(String email, String password) throws IOException { | ||
Request request = request("/user/login") | ||
.post(body(new UserLoginRequestDTO(email, password))).build(); | ||
try (Response response = getClient().newCall(request).execute()) { | ||
|
||
if (response.code() != 200) { | ||
UserErrorResponseDTO errorResponseDTO = response(UserErrorResponseDTO.class, response.body().string()); | ||
return new LoginResponse(null, errorResponseDTO.getError()); | ||
} | ||
|
||
UserLoginResponseDTO responseDTO = response(UserLoginResponseDTO.class, response.body().string()); | ||
return new LoginResponse(responseDTO.getSession(), null); | ||
} | ||
} | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public static class LoginResponse { | ||
private final String session; | ||
private final String error; | ||
} | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public static class RegisterResponse { | ||
private final String error; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/de/themorpheus/edu/gateway/backend/user/UserCreateDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package de.themorpheus.edu.gateway.backend.user; | ||
|
||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Data | ||
@RequiredArgsConstructor | ||
public class UserCreateDTO { | ||
|
||
private final String email; | ||
private final String password; | ||
private final String firstName; | ||
private final String lastName; | ||
private final String teacherToken; | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/de/themorpheus/edu/gateway/backend/user/UserErrorResponseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package de.themorpheus.edu.gateway.backend.user; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
public class UserErrorResponseDTO { | ||
|
||
private String error; | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/de/themorpheus/edu/gateway/backend/user/UserLoginRequestDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package de.themorpheus.edu.gateway.backend.user; | ||
|
||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Data | ||
@RequiredArgsConstructor | ||
public class UserLoginRequestDTO { | ||
|
||
private final String email; | ||
private final String password; | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/de/themorpheus/edu/gateway/backend/user/UserLoginResponseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package de.themorpheus.edu.gateway.backend.user; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
public class UserLoginResponseDTO { | ||
|
||
private String session; | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/de/themorpheus/edu/gateway/graphql/dto/user/UserRegisterDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package de.themorpheus.edu.gateway.graphql.dto.user; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import javax.validation.constraints.NotBlank; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class UserRegisterDTO { | ||
|
||
@NotBlank private String passwordHash; | ||
@NotBlank private String email; | ||
@NotBlank private String firstName; | ||
@NotBlank private String lastName; | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/de/themorpheus/edu/gateway/graphql/dto/user/UserRegisterResultDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package de.themorpheus.edu.gateway.graphql.dto.user; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import javax.validation.constraints.NotNull; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class UserRegisterResultDTO { | ||
|
||
@NotNull | ||
private UserAuthResultType result; | ||
|
||
public enum UserAuthResultType { | ||
SUCCESS, | ||
KEY_ALREADY_REGISTERED, | ||
REGISTRATION_FAILED, | ||
MAINTENANCE | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.