summaryrefslogtreecommitdiff
path: root/clams-server/src/main/java/no/eliashaugsbakk/clams/server/controller/PostController.java
blob: 01deb7d4cd1d7df3e62227123ac1d69883f77f37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package no.eliashaugsbakk.clams.server.controller;

import io.javalin.http.Context;
import io.javalin.http.HttpStatus;
import no.eliashaugsbakk.clams.server.model.Post;
import no.eliashaugsbakk.clams.server.model.PostDTO;
import no.eliashaugsbakk.clams.server.repository.PostsRepo;
import no.eliashaugsbakk.clams.server.service.SlugService;

public class PostController {
  private final PostsRepo postsRepo;
  private final SlugService slugService;

  public PostController(PostsRepo postsRepo, SlugService slugService) {
    this.postsRepo = postsRepo;
    this.slugService = slugService;
  }

  public void handlePostPost(Context ctx) {
    PostDTO newPost = ctx.bodyAsClass(PostDTO.class);
    postsRepo.addPost(new Post(newPost, slugService.toSlug(newPost.title())));
    ctx.status(HttpStatus.CREATED);
  }

  public void handlePutPost(Context ctx) {
    String slug = ctx.pathParam("slug");
    PostDTO updatedPost = ctx.bodyAsClass(PostDTO.class);

    postsRepo.getPost(slug)
        .map(existing -> Post.fromUpdated(existing, updatedPost))
        .ifPresentOrElse(
            postsRepo::updatePost,
            () -> ctx.status(HttpStatus.NOT_FOUND));
  }

  public void handleDeletePost(Context ctx) {
    if (!postsRepo.deletePost(ctx.pathParam("slug"))) {
      ctx.status(HttpStatus.NOT_FOUND);
    } else {
      ctx.status(HttpStatus.NO_CONTENT);
    }
  }
}