「不向伺服器上傳,相當於玩單機。」——最佳回答一針見血。
// Client (Android)
interface LikeApi {
@POST("posts/{id}/like")
suspend fun toggleLike(@Path("id") Long postId): Result
}
// Server (Spring Boot 範例)
@PostMapping("posts/{id}/like")
public ResponseEntity toggleLike(@PathVariable Long id, @CurrentUser User user) {
boolean liked = likeService.toggle(user.getId(), id);
long count = likeService.count(id);
return ResponseEntity.ok(new LikeResponse(liked, count));
}
// DB 表結構
CREATE TABLE post_likes (
user_id BIGINT NOT NULL,
post_id BIGINT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_id, post_id)
);
CREATE INDEX idx_post_likes_post ON post_likes(post_id);