Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Mathieu Féry
M1Alma WebAndCloud Server
Commits
b7ae91a6
Unverified
Commit
b7ae91a6
authored
Dec 09, 2021
by
Féry Mathieu (Mathius)
Browse files
feat(user-payload): Separate creation and update payload
parent
73ac17b3
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/main/java/fr/univnantes/webandcloud/api/controllers/v1/Users.java
View file @
b7ae91a6
...
...
@@ -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
User
Update
Payload
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
User
Creation
Payload
userPayload
)
throws
ResponseError
{
try
{
userDB
.
getWithoutLinks
(
userPayload
.
id
);
...
...
src/main/java/fr/univnantes/webandcloud/api/core/User.java
View file @
b7ae91a6
...
...
@@ -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
(
User
Creation
Payload
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
)
...
...
src/main/java/fr/univnantes/webandcloud/api/core/UserPayload.java
→
src/main/java/fr/univnantes/webandcloud/api/core/User
Creation
Payload.java
View file @
b7ae91a6
...
...
@@ -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
User
Creation
Payload
{
@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
User
Creation
Payload
(
@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
;
...
...
src/main/java/fr/univnantes/webandcloud/api/core/UserUpdatePayload.java
0 → 100644
View file @
b7ae91a6
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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment