Skip to content

Commit

Permalink
Improve API Resources + Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
smanhoff committed Nov 26, 2021
1 parent 18a4358 commit 196056e
Show file tree
Hide file tree
Showing 22 changed files with 319 additions and 230 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ $tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
```
https://carbon.nesbot.com/docs/
## icapps ❤️ PHP
## PHP Boilerplate

For further questions, ask the icapps PHP team.

Expand Down
44 changes: 44 additions & 0 deletions symfony/config/api_platform/resources/authentication.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
App\ApiResource\Auth\Authentication:
shortName: "Authentication"
attributes:
normalization_context:
groups: [ "api-get", "auth:api-get" ]
swagger_definition_name: "GET"
denormalization_context:
groups: [ "api-post", "auth:api-post" ]
swagger_definition_name: "POST"

# itemOperations
itemOperations: []

# collectionOperations
collectionOperations:
# Refresh
post_refresh_api:
status: 200
method: POST
path: /auth/refresh-token
input: App\Dto\Auth\UserRefreshDto
openapi_context:
summary: "Refresh user access token"
description: "Refresh user access token"

# Logout
post_logout_api:
status: 200
method: POST
path: /auth/logout
input: App\Dto\Auth\UserLogoutDto
openapi_context:
summary: "Logout user"
description: "Logout user, remove session and device"

# Password reset init
post_password_reset_api:
status: 200
method: POST
path: /auth/forgot-password/init
input: App\Dto\Auth\UserPasswordResetDto
openapi_context:
summary: "Initiate user forgot password flow"
description: "Initiate a user forgot password flow by sending an email to the user"
24 changes: 24 additions & 0 deletions symfony/config/api_platform/resources/registration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
App\ApiResource\Auth\Register:
shortName: "Registration"
attributes:
normalization_context:
groups: [ "api-get", "register:api-get" ]
swagger_definition_name: "GET"
denormalization_context:
groups: [ "api-post", "register:api-post" ]
swagger_definition_name: "POST"

# itemOperations
itemOperations: []

# collectionOperations
collectionOperations:
# Registration
post_register_api:
method: POST
path: /auth/register
input: App\Dto\Auth\UserRegisterDto
output: App\Dto\User\UserProfileDto
openapi_context:
summary: "Register a new user"
description: "Register a new user"
42 changes: 42 additions & 0 deletions symfony/config/api_platform/resources/user.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
App\ApiResource\User\User:
shortName: "User"
attributes:
normalization_context:
groups: [ "api-get", "profile:api-get" ]
swagger_definition_name: "GET"
denormalization_context:
groups: [ "api-post", "profile:api-post", "password:api-post" ]
swagger_definition_name: "PATCH"

# itemOperations
itemOperations:
# Get user profile.
get:
path: /users/{userSid}/profile
output: App\Dto\User\UserProfileDto
openapi_context:
summary: "Get user profile"
description: "Get user profile"

# Update user profile.
patch:
path: /users/{userSid}/profile
input: App\Dto\User\UserProfileDto
output: App\Dto\User\UserProfileDto
openapi_context:
summary: "Update user profile"
description: "Update user profile"

# Update user password
password_update:
method: PATCH
status: 200
path: /users/{userSid}/change-password
input: App\Dto\User\UserPasswordDto
output: App\Dto\General\StatusDto
openapi_context:
summary: "Update user password"
description: "Update user password"

# collectionOperations
collectionOperations: []
31 changes: 31 additions & 0 deletions symfony/config/api_platform/resources/user_devices.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
App\ApiResource\User\UserDevice:
shortName: "UserDevices"
attributes:
normalization_context:
groups: [ "api-get", "device:api-get" ]
swagger_definition_name: "GET"
denormalization_context:
groups: [ "api-post", "device:api-post" ]
swagger_definition_name: "PATCH"

# itemOperations
itemOperations:
# Get user device.
get:
path: /users/devices/{deviceSid}
output: App\Dto\User\UserDeviceDto
openapi_context:
summary: "Get user device"
description: "Get user device"

# Update user device.
patch:
path: /users/devices/{deviceSid}
input: App\Dto\User\UserDeviceDto
output: App\Dto\User\UserDeviceDto
openapi_context:
summary: "Update user device token"
description: "Update user device token"

# collectionOperations
collectionOperations: []
12 changes: 6 additions & 6 deletions symfony/config/packages/api_platform.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
api_platform:
title: 'icapps ❤ PHP'
title: 'PHP Boilerplate'
description: 'Template project using API platform'
version: '1.0.0'

Expand Down Expand Up @@ -110,7 +110,7 @@ api_platform:

# Mapping entities.
mapping:
paths: ['%kernel.project_dir%/src/Entity', '%kernel.project_dir%/src/ApiResource']
paths: ['%kernel.project_dir%/config/api_platform/resources']

patch_formats:
json: ['application/merge-patch+json']
Expand All @@ -127,19 +127,19 @@ api_platform:
formats:
json:
mime_types: ['application/json']
# jsonld:
# mime_types: ['application/ld+json']
jsonld:
mime_types: ['application/ld+json']
html:
mime_types: ['text/html']

# The list of enabled error formats. The first one will be the default.
error_formats:
json:
mime_types: [ 'application/json' ]
jsonld:
mime_types: ['application/ld+json']
# jsonproblem:
# mime_types: ['application/problem+json']
# jsonld:
# mime_types: ['application/ld+json']

# Global resources defaults
defaults:
Expand Down
55 changes: 0 additions & 55 deletions symfony/src/ApiResource/Auth/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,6 @@

namespace App\ApiResource\Auth;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Dto\Auth\AuthAccessDto;
use App\Dto\General\StatusDto;
use App\Dto\Auth\UserLogoutDto;
use App\Dto\Auth\UserPasswordResetDto;
use App\Dto\Auth\UserRefreshDto;

/**
* @ApiResource(
* routePrefix=AuthAccessDto::AUTH_ROUTE_PREFIX,
* collectionOperations={
* "post_refresh_api"={
* "status"=200,
* "path"="/refresh-token",
* "input"=UserRefreshDto::class,
* "method"="POST",
* "openapi_context"={
* "summary"="Refresh user access token",
* "description"="Refresh user access token"
* }
* },
* "post_logout_api"={
* "status"=200,
* "path"="/logout",
* "input"=UserLogoutDto::class,
* "method"="POST",
* "openapi_context"={
* "summary"="Logout user",
* "description"="Logout user, remove session and device"
* },
* },
* "post_password_reset_api"={
* "status"=200,
* "path"="/forgot-password/init",
* "input"=UserPasswordResetDto::class,
* "method"="POST",
* "openapi_context"={
* "summary"="Initiate user forgot password flow",
* "description"="Initiate a user forgot password flow by sending an email to the user"
* }
* }
* },
* itemOperations={},
* shortName="Authentication",
* normalizationContext={
* "groups"={"auth:api-get", "api-get"},
* "swagger_definition_name"="GET"
* },
* denormalizationContext={
* "groups"={"auth:api-post", "api-post"},
* "swagger_definition_name"="POST"
* },
* output=StatusDto::class
* )
*/
final class Authentication
{
public int $id;
Expand Down
40 changes: 0 additions & 40 deletions symfony/src/ApiResource/Auth/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,6 @@

namespace App\ApiResource\Auth;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Dto\Auth\AuthAccessDto;
use App\Dto\Auth\UserRegisterDto;
use App\Dto\User\UserProfileDto;

/**
* @ApiResource(
* routePrefix=AuthAccessDto::AUTH_ROUTE_PREFIX,
* collectionOperations={
* "post_register_api"={
* "path"= "/register",
* "method"="POST",
* "openapi_context"={
* "summary"="Register a new user",
* "description"="Register a new user"
* }
* }
* },
* itemOperations={},
* shortName="Register",
* normalizationContext={
* "groups"={"api-get", "register:api-get"},
* "swagger_definition_name"="GET",
* "openapi_context"={
* "summary"="Register a new user",
* "description"="Register a new user"
* }
* },
* denormalizationContext={
* "groups"={"register:api-post"},
* "swagger_definition_name"="POST",
* "openapi_context"={
* "summary"="Register a new user",
* "description"="Register a new user"
* }
* },
* input=UserRegisterDto::class,
* output=UserProfileDto::class
* )
*/
final class Register
{
public int $id;
Expand Down
47 changes: 0 additions & 47 deletions symfony/src/ApiResource/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,7 @@
namespace App\ApiResource\User;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Dto\General\StatusDto;
use App\Dto\User\UserProfileDto;
use App\Dto\User\UserPasswordDto;

/**
* @ApiResource(
* routePrefix=UserProfileDto::USER_ROUTE_PREFIX,
* collectionOperations={},
* itemOperations={
* "get"={
* "path"= "/{userSid}/profile",
* "openapi_context"={
* "summary"="Get active user profile",
* "description"="Get active user profile"
* }
* },
* "patch"={
* "path"= "/{userSid}/profile",
* "openapi_context"={
* "summary"="Update user profile",
* "description"="Update user profile"
* }
* },
* "password_update"={
* "status"=200,
* "path"="/{userSid}/password",
* "input"=UserPasswordDto::class,
* "output"=StatusDto::class,
* "method"="PATCH",
* "openapi_context"={
* "summary"="Update user password",
* "description"="Update user password"
* },
* },
* },
* normalizationContext={
* "groups"={"api-get", "profile:api-get"},
* "swagger_definition_name"="GET"
* },
* denormalizationContext={
* "groups"={"api-post", "profile:api-post", "password:api-post"},
* "swagger_definition_name"="PATCH"
* },
* input=UserProfileDto::class,
* output=UserProfileDto::class
* )
*/
final class User
{
/**
Expand Down
Loading

0 comments on commit 196056e

Please sign in to comment.