Unverified Commit b7ae91a6 authored by Féry Mathieu (Mathius)'s avatar Féry Mathieu (Mathius)
Browse files

feat(user-payload): Separate creation and update payload

parent 73ac17b3
......@@ -18,7 +18,8 @@ import fr.univnantes.webandcloud.api.core.ShortPost;
import fr.univnantes.webandcloud.api.core.ShortUser;
import fr.univnantes.webandcloud.api.core.Token;
import fr.univnantes.webandcloud.api.core.User;
import fr.univnantes.webandcloud.api.core.UserPayload;
import fr.univnantes.webandcloud.api.core.UserCreationPayload;
import fr.univnantes.webandcloud.api.core.UserUpdatePayload;
import fr.univnantes.webandcloud.api.responses.BadRequest;
import fr.univnantes.webandcloud.api.responses.NotFoundException;
import fr.univnantes.webandcloud.api.responses.ResponseError;
......@@ -92,15 +93,12 @@ public class Users extends BaseController {
@Nullable
public User updateUser(
@Parameter(description = "Token for identify your account", example = "abcEFG145") @RequestHeader(required = false) @Nullable String token,
@Parameter(description = "Payload of User") @RequestBody @NonNull UserPayload userPayload)
@Parameter(description = "Payload of User") @RequestBody @NonNull UserUpdatePayload userPayload)
throws ResponseError {
User currentUser = tokenService.getUserRelatedWithoutLinks(token);
if (currentUser == null)
throw new BadRequest("Token must be valid");
if (!userPayload.id.equals(currentUser.getId()))
throw new BadRequest("User id is immutable");
else
userDB.put(currentUser.updateFromPayload(userPayload));
userDB.put(currentUser.updateFromPayload(userPayload));
return currentUser;
}
......@@ -148,7 +146,7 @@ public class Users extends BaseController {
@Operation(summary = "Create user")
@ApiResponse(responseCode = "200", description = "User Created")
@RequestMapping(value = "/users/create", method = RequestMethod.POST)
public Token createUser(@Parameter(description = "Payload of User") @RequestBody @NonNull UserPayload userPayload)
public Token createUser(@Parameter(description = "Payload of User") @RequestBody @NonNull UserCreationPayload userPayload)
throws ResponseError {
try {
userDB.getWithoutLinks(userPayload.id);
......
......@@ -41,13 +41,11 @@ public class User implements WithIdentifier {
this(id, userName, email, uri, description, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
}
public User(UserPayload payload) {
public User(UserCreationPayload payload) {
this(payload.id, payload.userName, payload.email, payload.uri, payload.description);
}
public User updateFromPayload(UserPayload payload) {
if (payload.id != null)
setId(payload.id);
public User updateFromPayload(UserUpdatePayload payload) {
if (payload.userName != null)
userName = payload.userName;
if (payload.uri != null)
......
......@@ -8,8 +8,8 @@ import org.springframework.lang.Nullable;
import io.swagger.v3.oas.annotations.media.Schema;
@Schema(description = "User Payload")
public class UserPayload {
@Schema(description = "User Creation Payload")
public class UserCreationPayload {
@Schema(description = "Id of User", example = "johnDoe")
public final String id;
......@@ -23,7 +23,7 @@ public class UserPayload {
public final String description;
@JsonCreator
public UserPayload(@NonNull @JsonProperty("id") String id, @NonNull @JsonProperty("userName") String userName,
public UserCreationPayload(@NonNull @JsonProperty("id") String id, @NonNull @JsonProperty("userName") String userName,
@NonNull @JsonProperty("email") String email, @Nullable @JsonProperty("uri") String uri,
@Nullable @JsonProperty("description") String description) {
this.id = id;
......
package fr.univnantes.webandcloud.api.core;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import io.swagger.v3.oas.annotations.media.Schema;
@Schema(description = "User Update Payload")
public class UserUpdatePayload {
@Schema(description = "UserName of User", example = "DarkSasukeOf44")
public final String userName;
@Schema(description = "Uri of image related to this User", example = "foo.org/someImage.png")
public final String uri;
@Schema(description = "Description of User", example = "Some Description here")
public final String description;
@JsonCreator
public UserUpdatePayload(@NonNull @JsonProperty("userName") String userName,
@Nullable @JsonProperty("uri") String uri, @Nullable @JsonProperty("description") String description) {
this.userName = userName;
this.uri = uri;
this.description = description;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment