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

feat(user): Add management of user editing

parent 488715d7
Pipeline #34216 passed with stages
in 3 minutes and 43 seconds
......@@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.RestController;
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.responses.BadRequest;
import fr.univnantes.webandcloud.api.responses.NotFoundException;
import fr.univnantes.webandcloud.api.responses.ResponseError;
......@@ -68,6 +69,25 @@ public class Users extends BaseController {
return userDB.get(id);
}
@CrossOrigin
@Operation(summary = "Update user")
@ApiResponse(responseCode = "200", description = "Update user", content = @Content(mediaType = "application/json", schema = @Schema(implementation = User.class)))
@RequestMapping(value = "/users/edit", method = RequestMethod.POST)
@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)
throws ResponseError {
User currentUser = tokenService.getUserRelated(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));
return currentUser;
}
@CrossOrigin
@Operation(summary = "Follow user")
@ApiResponse(responseCode = "200", description = "Follow a user")
......@@ -112,13 +132,13 @@ 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 User user)
public Token createUser(@Parameter(description = "Payload of User") @RequestBody @NonNull UserPayload userPayload)
throws ResponseError {
try {
User res = getUser(user.getId());
getUser(userPayload.id);
throw new BadRequest("User already exist");
} catch (NotFoundException ignored) {
user = this.userDB.add(user);
User user = this.userDB.add(new User(userPayload));
Token token = new Token(user);
token = this.tokenDB.add(token);
return token;
......
......@@ -3,12 +3,6 @@ package fr.univnantes.webandcloud.api.core;
import java.util.ArrayList;
import java.util.List;
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 = "Some user")
......@@ -44,10 +38,18 @@ public class User implements WithIdentifier {
this(id, userName, email, uri, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
}
@JsonCreator
public User(@NonNull @JsonProperty("userName") String userName, @NonNull @JsonProperty("email") String email,
@Nullable @JsonProperty("uri") String uri) {
this(userName, userName, email, uri, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
public User(UserPayload payload) {
this(payload.id, payload.userName, payload.email, payload.uri);
}
public User updateFromPayload(UserPayload payload) {
if (payload.id != null)
setId(payload.id);
if (payload.userName != null)
userName = payload.userName;
if (payload.uri != null)
uri = payload.uri;
return this;
}
@Override
......
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 Payload")
public class UserPayload {
@Schema(description = "Id of User", example = "johnDoe")
public final String id;
@Schema(description = "UserName of User", example = "DarkSasukeOf44")
public final String userName;
@Schema(description = "Email of User", example = "example@example.org")
public final String email;
@Schema(description = "Uri of image related to this User", example = "foo.org/someImage.png")
public final String uri;
@JsonCreator
public UserPayload(@NonNull @JsonProperty("id") String id, @NonNull @JsonProperty("userName") String userName,
@NonNull @JsonProperty("email") String email, @Nullable @JsonProperty("uri") String uri) {
this.id = id;
this.userName = userName;
this.email = email;
this.uri = uri;
}
}
......@@ -84,6 +84,6 @@ public class OAuthUserDataService {
firstName = nameSplit[0];
}
}
return new User(nickName, email, null);
return new User(null, nickName, email, null);
}
}
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