-
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.
[230] Add the ability to create accounts for the server administrator
Bug: #230 Signed-off-by: Stéphane Bégaudeau <[email protected]>
- Loading branch information
1 parent
4178c7f
commit 77c90d8
Showing
30 changed files
with
1,376 additions
and
24 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
...on/src/main/java/com/svalyn/studio/application/controllers/account/AccountController.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 @@ | ||
/* | ||
* Copyright (c) 2023 Stéphane Bégaudeau. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.svalyn.studio.application.controllers.account; | ||
|
||
import com.svalyn.studio.application.controllers.account.dto.AccountDTO; | ||
import com.svalyn.studio.application.controllers.account.dto.CreateAccountInput; | ||
import com.svalyn.studio.application.controllers.dto.IPayload; | ||
import com.svalyn.studio.application.controllers.dto.PageInfoWithCount; | ||
import com.svalyn.studio.application.services.account.api.IAccountService; | ||
import graphql.relay.Connection; | ||
import graphql.relay.DefaultConnection; | ||
import graphql.relay.DefaultConnectionCursor; | ||
import graphql.relay.DefaultEdge; | ||
import graphql.relay.Edge; | ||
import graphql.relay.Relay; | ||
import jakarta.validation.Valid; | ||
import org.springframework.graphql.data.method.annotation.Argument; | ||
import org.springframework.graphql.data.method.annotation.MutationMapping; | ||
import org.springframework.graphql.data.method.annotation.SchemaMapping; | ||
import org.springframework.stereotype.Controller; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Controller used to retrieve accounts. | ||
* | ||
* @author sbegaudeau | ||
*/ | ||
@Controller | ||
public class AccountController { | ||
|
||
private final IAccountService accountService; | ||
|
||
public AccountController(IAccountService accountService) { | ||
this.accountService = Objects.requireNonNull(accountService); | ||
} | ||
|
||
@SchemaMapping(typeName = "Admin") | ||
public Connection<AccountDTO> accounts(@Argument int page, @Argument int rowsPerPage) { | ||
var pageData = this.accountService.findAll(page, rowsPerPage); | ||
var edges = pageData.stream().map(account -> { | ||
var value = new Relay().toGlobalId("Account", account.username()); | ||
var cursor = new DefaultConnectionCursor(value); | ||
return (Edge<AccountDTO>) new DefaultEdge<>(account, cursor); | ||
}).toList(); | ||
var pageInfo = new PageInfoWithCount(null, null, pageData.hasPrevious(), pageData.hasNext(), pageData.getTotalElements()); | ||
return new DefaultConnection<>(edges, pageInfo); | ||
} | ||
|
||
@MutationMapping | ||
public IPayload createAccount(@Argument @Valid CreateAccountInput input) { | ||
return this.accountService.createAccount(input); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...ation/src/main/java/com/svalyn/studio/application/controllers/account/dto/AccountDTO.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,36 @@ | ||
/* | ||
* Copyright (c) 2023 Stéphane Bégaudeau. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.svalyn.studio.application.controllers.account.dto; | ||
|
||
import com.svalyn.studio.domain.account.AccountRole; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
import java.time.Instant; | ||
|
||
public record AccountDTO( | ||
@NotNull String name, | ||
@NotNull String username, | ||
@NotNull String imageUrl, | ||
@NotNull String email, | ||
@NotNull AccountRole role, | ||
@NotNull Instant createdOn, | ||
@NotNull Instant lastModifiedOn | ||
) { | ||
} |
38 changes: 38 additions & 0 deletions
38
...c/main/java/com/svalyn/studio/application/controllers/account/dto/CreateAccountInput.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,38 @@ | ||
/* | ||
* Copyright (c) 2023 Stéphane Bégaudeau. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.svalyn.studio.application.controllers.account.dto; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* Input used to create new account. | ||
* | ||
* @author sbegaudeau | ||
*/ | ||
public record CreateAccountInput( | ||
@NotNull UUID id, | ||
@NotNull String name, | ||
@NotNull String username, | ||
@NotNull String email, | ||
@NotNull String password | ||
) { | ||
} |
33 changes: 33 additions & 0 deletions
33
...va/com/svalyn/studio/application/controllers/account/dto/CreateAccountSuccessPayload.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,33 @@ | ||
/* | ||
* Copyright (c) 2023 Stéphane Bégaudeau. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.svalyn.studio.application.controllers.account.dto; | ||
|
||
import com.svalyn.studio.application.controllers.dto.IPayload; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* Payload used to indicate that the account has been created. | ||
* | ||
* @author sbegaudeau | ||
*/ | ||
public record CreateAccountSuccessPayload(@NotNull UUID id, @NotNull AccountDTO account) implements IPayload { | ||
} |
42 changes: 42 additions & 0 deletions
42
...cation/src/main/java/com/svalyn/studio/application/controllers/admin/AdminController.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,42 @@ | ||
/* | ||
* Copyright (c) 2023 Stéphane Bégaudeau. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.svalyn.studio.application.controllers.admin; | ||
|
||
import com.svalyn.studio.application.controllers.admin.dto.AdminDTO; | ||
import com.svalyn.studio.application.controllers.viewer.Viewer; | ||
import com.svalyn.studio.domain.account.AccountRole; | ||
import org.springframework.graphql.data.method.annotation.SchemaMapping; | ||
import org.springframework.stereotype.Controller; | ||
|
||
/** | ||
* Controller used to retrieve the admin. | ||
* | ||
* @author sbegaudeau | ||
*/ | ||
@Controller | ||
public class AdminController { | ||
@SchemaMapping(typeName = "Viewer") | ||
public AdminDTO asAdmin(Viewer viewer) { | ||
if (viewer.role().equals(AccountRole.ADMIN)) { | ||
return new AdminDTO(); | ||
} | ||
return null; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...plication/src/main/java/com/svalyn/studio/application/controllers/admin/dto/AdminDTO.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,28 @@ | ||
/* | ||
* Copyright (c) 2023 Stéphane Bégaudeau. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.svalyn.studio.application.controllers.admin.dto; | ||
|
||
/** | ||
* The admin DTO for the GraphQL layer. | ||
* | ||
* @author sbegaudeau | ||
*/ | ||
public record AdminDTO() { | ||
} |
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
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.