package fr.alecharp.picshare.http;

import fr.alecharp.picshare.domain.Event;
import fr.alecharp.picshare.service.EventService;
import net.codestory.http.annotations.Post;
import net.codestory.http.annotations.Prefix;
import net.codestory.http.payload.Payload;

import javax.inject.Inject;
import java.util.Optional;

/**
 * @author Adrien Lecharpentier
 */
@Prefix("/api/event")
public class EventController {
    private final EventService eventService;

    @Inject
    public EventController(EventService eventService) {
        this.eventService = eventService;
    }

    @Post
    public Payload create(Event event) {
        Optional<Event> save = eventService.save(event);
        return save.isPresent() ? Payload.created("/api/event/" + save.get().id()) :
                Payload.badRequest();
    }
}

@@ -103,28 +103,159 @@
 are.
-service.EventService
+domain.Picture;%0Aimport fr.alecharp.picshare.service.EventService;%0Aimport fr.alecharp.picshare.service.PictureService;%0Aimport net.codestory.http.Request
 ;%0Aim
@@ -342,16 +342,59 @@
 Prefix;%0A
+import net.codestory.http.annotations.Put;%0A
 import n
@@ -483,16 +483,88 @@
 ptional;
+%0Aimport java.util.Set;%0A%0Aimport static java.util.stream.Collectors.toSet;
 %0A%0A/**%0A *
@@ -694,16 +694,65 @@
 Service;
+%0A    private final PictureService pictureService;
 %0A%0A    @I
@@ -809,16 +809,47 @@
 tService
+, PictureService pictureService
 ) %7B%0A    
@@ -882,24 +882,70 @@
 entService;%0A
+        this.pictureService = pictureService;%0A
     %7D%0A%0A    @
@@ -1175,11 +1175,624 @@
 );%0A    %7D
+%0A%0A    @Put(%22/:id/pictures%22)%0A    public Payload attachPicture(String id, Request req) %7B%0A        Set%3CPicture%3E pictures = req.parts().stream()%0A                .filter(part -%3E part.name().equals(%22picture%22))%0A                .map(part -%3E pictureService.upload(id, part))%0A                .filter(Optional::isPresent).map(Optional::get)%0A                .collect(toSet());%0A        Optional%3CEvent%3E event = eventService.attachPictures(id, pictures);%0A        if (!event.isPresent()) %7B%0A            return Payload.notFound();%0A        %7D%0A        return Payload.ok().withHeader(%22Location%22, %22/api/event/%22 + event.get().id());%0A    %7D
 %0A%7D%0A

Update even HTTP API to upload images into events