classDiagram
class Post {
Long id
String content
Instant createdAt
Instant updatedAt
Instant publishedAt
boolean isDraft
int likes
+getId() Long
+setId(Long id) void
+getContent() String
+setContent(String content) void
+isDraft() boolean
+setDraft(boolean isDraft) void
+getPublishedAt() Instant
+setPublishedAt(Instant publishedAt) void
+getLikes() int
+setLikes(int likes) void
+incrementLikes() int
}
class PostService {
-Map~Long, Post~ posts
-AtomicLong idGenerator
+createDraft(String content) Post
+publishPost(Long id) Post
+deletePost(Long id) boolean
+getPost(Long id) Post
+getPublishedPosts() List~Post~
+getDraftPosts() List~Post~
+searchPosts(SearchParams params) List~Post~
+likePost(Long id) Post
+getPostLikes(Long id) Integer
}
class PostController {
-PostService postService
+createDraft(Map payload) ResponseEntity~Post~
+publishPost(Long id) ResponseEntity~Post~
+getPost(Long id) ResponseEntity~Post~
+deletePost(Long id) ResponseEntity~void~
+getPublishedPosts() ResponseEntity~List~Post~~
+getDraftPosts() ResponseEntity~List~Post~~
+likePost(Long id) ResponseEntity~Post~
+getPostLikes(Long id) ResponseEntity~Map~String, Integer~~
}
class SearchParams {
String keyword
Boolean isDraft
Instant publishedAfter
Instant publishedBefore
}
PostController --> PostService : 依存
PostService --> Post : 使用
Post *-- SearchParams : 内部クラス
graph TD
Client([クライアント]) -->|リクエスト| API[API Layer\nPostController]
API -->|処理委譲| Service[Service Layer\nPostService]
Service -->|データ操作| Model[Model Layer\nPost]
Model -->|保存| DB[(インメモリストレージ)]
API -->|レスポンス| Client
sequenceDiagram
participant Client as クライアント
participant Controller as PostController
participant Service as PostService
participant Model as Post
Client->>Controller: POST /api/posts/drafts
Controller->>Service: createDraft(content)
Service->>Model: new Post(content)
Service->>Service: posts.put(post.getId(), post)
Service-->>Controller: 作成されたPost
Controller-->>Client: HTTP 201 Created + Post JSON
Client->>Controller: PUT /api/posts/drafts/{id}/publish
Controller->>Service: publishPost(id)
Service->>Model: post.setDraft(false)
Service->>Model: post.setPublishedAt(now)
Service-->>Controller: 公開されたPost
Controller-->>Client: HTTP 200 OK + Post JSON
- JDK 21以上
- 【任意】 Gradle 8.4以上 ※Gradle Wrapperでもかまいません。サンプルコマンドでも使用しています。
-
ハンズオンディレクトリに移動します。
cd /workspaces/github-demo-2506/demo/
-
必要な依存関係をインストールします。
./gradlew build
-
アプリケーションを起動します。
./gradlew bootRun
-
アプリケーションが起動したら、以下のURLでAPIを利用できます。
curl -X POST http://localhost:8080/api/posts/drafts \
-H "Content-Type: application/json" \
-d '{"content": "This is a draft post."}'
curl -X PUT http://localhost:8080/api/posts/drafts/{id}/publish
curl -X GET http://localhost:8080/api/posts/{id}
curl -X DELETE http://localhost:8080/api/posts/{id}
curl -X GET http://localhost:8080/api/posts/published
curl -X GET http://localhost:8080/api/posts/drafts
curl -X POST http://localhost:8080/api/posts/{id}/like
curl -X GET http://localhost:8080/api/posts/{id}/likes