package klabbristol.holidayservice.handlers;


import klabbristol.holidayservice.dao.HolidayRepo;
import klabbristol.holidayservice.model.Holiday;
import klabbristol.holidayservice.model.NewHoliday;
import ratpack.handling.Context;
import ratpack.handling.Handler;

import java.util.List;
import java.util.Optional;

import static ratpack.jackson.Jackson.fromJson;
import static ratpack.jackson.Jackson.json;

public class HolidaysHandler implements Handler {
    @Override
    public void handle(Context ctx) throws Exception {
        HolidayRepo repo = ctx.get(HolidayRepo.class);

        ctx.byMethod(m -> m
            .post(() -> {
                ctx.parse(fromJson(NewHoliday.class))
                    .then(holiday -> {
                        repo.add(holiday);
                        ctx.getResponse().status(201).send();
                    });
            })
            .get(() -> {
                List<Holiday> holidays =
                    Optional.ofNullable(ctx.getRequest().getQueryParams().get("user"))
                        .map(repo::findByUser)
                        .orElse(repo.findAll());

                ctx.render(json(holidays));
            })
        );
    }
}

@@ -131,16 +131,19 @@
 e.model.
+New
 Holiday;
@@ -154,51 +154,29 @@
 ort 
-klabbristol.holidayservice.model.NewHoliday
+ratpack.exec.Blocking
 ;%0Aim
@@ -244,31 +244,8 @@
 r;%0A%0A
-import java.util.List;%0A
 impo
@@ -706,16 +706,63 @@
         
+ Blocking%0A                            .op(() -%3E
  repo.ad
@@ -771,17 +771,17 @@
 holiday)
-;
+)
 %0A       
@@ -792,24 +792,40 @@
             
+     .then(() -%3E
  ctx.getResp
@@ -849,16 +849,17 @@
 ).send()
+)
 ;%0A      
@@ -936,32 +936,16 @@
     
-List%3CHoliday%3E holidays =
+Blocking
 %0A   
@@ -960,16 +960,27 @@
         
+ .get(() -%3E
  Optiona
@@ -1133,18 +1133,16 @@
 ndAll())
-;%0A
 %0A       
@@ -1145,24 +1145,68 @@
             
+     )%0A                    .then(holidays -%3E
  ctx.render(
@@ -1220,16 +1220,17 @@
 lidays))
+)
 ;%0A      

non blockin IO