Skip to content

Commit

Permalink
Javalin first
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabh.kumar committed Oct 14, 2023
1 parent ca179ec commit 4e5d59e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 50 deletions.
15 changes: 8 additions & 7 deletions javalin-demo/src/main/java/org/saurabh/users/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private UserController() {
tags = {"User"},
requestBody = @OpenApiRequestBody(content = {@OpenApiContent(from = NewUserRequest.class)}),
responses = {
@OpenApiResponse(status = "201"),
@OpenApiResponse(status = "201", content = {@OpenApiContent(from = User.class)}),
@OpenApiResponse(status = "400", content = {@OpenApiContent(from = ErrorResponse.class)})
}
)
Expand Down Expand Up @@ -87,19 +87,20 @@ public static void getOne(Context ctx) {
tags = {"User"},
requestBody = @OpenApiRequestBody(content = {@OpenApiContent(from = NewUserRequest.class)}),
responses = {
@OpenApiResponse(status = "204"),
@OpenApiResponse(status = "200", content = {@OpenApiContent(from = User.class)}),
@OpenApiResponse(status = "400", content = {@OpenApiContent(from = ErrorResponse.class)}),
@OpenApiResponse(status = "404", content = {@OpenApiContent(from = ErrorResponse.class)})
}
)
public static void update(Context ctx) {
final var user = UserService.findById(validPathParamUserId(ctx));
if (user == null) {
throw new NotFoundResponse("User not found");
final var userId = validPathParamUserId(ctx);
if (UserService.findById(userId) == null) {
throw new NotFoundResponse("User %s not found".formatted(userId));
} else {
final var newUser = ctx.bodyAsClass(NewUserRequest.class);
UserService.update(user.id(), newUser.name(), newUser.email());
ctx.status(204);
final var updatedUser = UserService.update(userId, newUser.name(), newUser.email());
ctx.status(200);
ctx.json(updatedUser);
}
}

Expand Down
90 changes: 47 additions & 43 deletions javalin-demo/src/main/resources/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ info:
title: User API
version: "1.0"
servers:
- url: /
- url: /
paths:
/users:
get:
Expand Down Expand Up @@ -32,7 +32,7 @@ paths:
description: OK
summary: Get all users
tags:
- User
- User
x-accepts: application/json
post:
operationId: createUser
Expand Down Expand Up @@ -68,22 +68,22 @@ paths:
description: Bad Request
summary: Create user
tags:
- User
- User
x-content-type: application/json
x-accepts: application/json
/users/{userId}:
delete:
operationId: deleteUserById
parameters:
- description: The user ID
explode: false
in: path
name: userId
required: true
schema:
format: int32
type: integer
style: simple
- description: The user ID
explode: false
in: path
name: userId
required: true
schema:
format: int32
type: integer
style: simple
responses:
"500":
content:
Expand Down Expand Up @@ -113,20 +113,20 @@ paths:
description: Not Found
summary: Delete user by ID
tags:
- User
- User
x-accepts: application/json
get:
operationId: getUserById
parameters:
- description: The user ID
explode: false
in: path
name: userId
required: true
schema:
format: int32
type: integer
style: simple
- description: The user ID
explode: false
in: path
name: userId
required: true
schema:
format: int32
type: integer
style: simple
responses:
"500":
content:
Expand Down Expand Up @@ -160,20 +160,20 @@ paths:
description: Not Found
summary: Get user by ID
tags:
- User
- User
x-accepts: application/json
patch:
operationId: updateUserById
parameters:
- description: The user ID
explode: false
in: path
name: userId
required: true
schema:
format: int32
type: integer
style: simple
- description: The user ID
explode: false
in: path
name: userId
required: true
schema:
format: int32
type: integer
style: simple
requestBody:
content:
application/json:
Expand All @@ -192,8 +192,12 @@ paths:
schema:
$ref: '#/components/schemas/ErrorResponse'
description: Service Unavailable
"204":
description: No Content
"200":
description: Ok
content:
application/json:
schema:
$ref: '#/components/schemas/User'
"400":
content:
application/json:
Expand All @@ -208,7 +212,7 @@ paths:
description: Not Found
summary: Update user by ID
tags:
- User
- User
x-content-type: application/json
x-accepts: application/json
components:
Expand All @@ -227,9 +231,9 @@ components:
type: string
type: object
required:
- status
- title
- type
- status
- title
- type
type: object
User:
example:
Expand All @@ -245,9 +249,9 @@ components:
email:
type: string
required:
- email
- id
- name
- email
- id
- name
type: object
NewUserRequest:
example:
Expand All @@ -259,7 +263,7 @@ components:
email:
type: string
required:
- email
- name
- email
- name
type: object

0 comments on commit 4e5d59e

Please sign in to comment.