// Path: src/main/java/se/omegapoint/facepalm/client/adapters/ImageAdapter.java
// @Adapter
// public class ImageAdapter {
// 
//     private final ImageService imageService;
// 
//     @Autowired
//     public ImageAdapter(final ImageService imageService) {
//         this.imageService = notNull(imageService);
//     }
// 
// 
//     public List<ImagePost> getTopImagePosts() {
//         return imageService.getTopImages().stream()
//                 .map(this::imagePost)
//                 .collect(toList());
//     }
// 
//     public Optional<ImagePost> getImage(final String id) {
//         notBlank(id);
// 
//         return imageService.getImagePost(id).map(this::imagePost);
//     }
// 
//     public List<ImageComment> getCommentsForImage(final Long imageId) {
//         notNull(imageId);
// 
//         return imageService.getCommentsForImage(imageId).stream()
//                 .map(c -> new ImageComment(c.author, c.text))
//                 .collect(toList());
//     }
// 
//     public void addComment(final String imageId, final String text) {
//         notBlank(imageId);
//         notBlank(text);
// 
//         imageService.addComment(new NewImageComment(Long.valueOf(imageId), currentUser(), text));
//     }
// 
//     public void addImage(final ImageUpload imageUpload) {
//         notNull(imageUpload);
//         imageService.addImagePost(new Title(imageUpload.title), imageUpload.data);
//     }
// 
//     public Image fetchImage(final Long id) {
//         notNull(id);
//         return imageService.getImageFor(id);
//     }
// 
//     public String currentUser() {
//         final AuthenticatedUser authenticatedUser = (AuthenticatedUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//         return authenticatedUser.userName;
//     }
// 
//     private ImagePost imagePost(final se.omegapoint.facepalm.domain.ImagePost image) {
//         return new ImagePost(image.id, image.title.value, image.numPoints.value, image.numComments.value);
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/client/models/ImageComment.java
// public class ImageComment {
//     public final String author;
//     public final String text;
// 
//     public ImageComment(final String author, final String text) {
//         this.author = author;
//         this.text = text;
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/client/models/ImagePost.java
// public class ImagePost {
//     public final Long id;
//     public final String title;
//     public final Long numPoints;
//     public final Long numComments;
// 
//     public ImagePost(final Long id, final String title, final Long points, final Long numComments) {
//         this.id = notNull(id);
//         this.title = notBlank(title);
//         this.numPoints = notNull(points);
//         this.numComments = notNull(numComments);
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/client/models/ImageUpload.java
// public class ImageUpload {
//     public final String title;
//     public final byte[] data;
// 
//     public ImageUpload(final String title, final byte[] data) {
//         this.title = notBlank(title);
//         this.data = notNull(data);
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/domain/Image.java
// public class Image {
//     public final byte[] data;
// 
//     public Image(final byte[] data) {
//         this.data = notNull(data);
//     }
// }


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import se.omegapoint.facepalm.client.adapters.ImageAdapter;
import se.omegapoint.facepalm.client.models.ImageComment;
import se.omegapoint.facepalm.client.models.ImagePost;
import se.omegapoint.facepalm.client.models.ImageUpload;
import se.omegapoint.facepalm.domain.Image;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import static org.apache.commons.lang3.Validate.notNull;
    @RequestMapping("/")
    public String index(final Model model) {
        model.addAttribute("posts", imageAdapter.getTopImagePosts());
        return "index";
    }

    @RequestMapping("/image")
    public String image(final @RequestParam String id, final Model model) {
        final Optional<ImagePost> image = imageAdapter.getImage(id);
        if (!image.isPresent()) {
            return "redirect:/404";
        }

        final List<ImageComment> comments = imageAdapter.getCommentsForImage(image.get().id);
        model.addAttribute("image", image.get());
        model.addAttribute("comments", comments);
        return "image";
    }

    @RequestMapping(value = "/comment", method = RequestMethod.POST)
    public String addComment(final @RequestParam("imageId") String imageId, final @RequestParam("text") String text) {
        imageAdapter.addComment(imageId, text);
        return "redirect:/image?id=" + imageId;
    }

    @RequestMapping(value = "/postImage", method = RequestMethod.POST)
    public String postImage(final @RequestParam("title") String title, final @RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                final byte[] data = notNull(file.getBytes());
// Path: src/main/java/se/omegapoint/facepalm/client/adapters/ImageAdapter.java
// @Adapter
// public class ImageAdapter {
// 
//     private final ImageService imageService;
// 
//     @Autowired
//     public ImageAdapter(final ImageService imageService) {
//         this.imageService = notNull(imageService);
//     }
// 
// 
//     public List<ImagePost> getTopImagePosts() {
//         return imageService.getTopImages().stream()
//                 .map(this::imagePost)
//                 .collect(toList());
//     }
// 
//     public Optional<ImagePost> getImage(final String id) {
//         notBlank(id);
// 
//         return imageService.getImagePost(id).map(this::imagePost);
//     }
// 
//     public List<ImageComment> getCommentsForImage(final Long imageId) {
//         notNull(imageId);
// 
//         return imageService.getCommentsForImage(imageId).stream()
//                 .map(c -> new ImageComment(c.author, c.text))
//                 .collect(toList());
//     }
// 
//     public void addComment(final String imageId, final String text) {
//         notBlank(imageId);
//         notBlank(text);
// 
//         imageService.addComment(new NewImageComment(Long.valueOf(imageId), currentUser(), text));
//     }
// 
//     public void addImage(final ImageUpload imageUpload) {
//         notNull(imageUpload);
//         imageService.addImagePost(new Title(imageUpload.title), imageUpload.data);
//     }
// 
//     public Image fetchImage(final Long id) {
//         notNull(id);
//         return imageService.getImageFor(id);
//     }
// 
//     public String currentUser() {
//         final AuthenticatedUser authenticatedUser = (AuthenticatedUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//         return authenticatedUser.userName;
//     }
// 
//     private ImagePost imagePost(final se.omegapoint.facepalm.domain.ImagePost image) {
//         return new ImagePost(image.id, image.title.value, image.numPoints.value, image.numComments.value);
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/client/models/ImageComment.java
// public class ImageComment {
//     public final String author;
//     public final String text;
// 
//     public ImageComment(final String author, final String text) {
//         this.author = author;
//         this.text = text;
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/client/models/ImagePost.java
// public class ImagePost {
//     public final Long id;
//     public final String title;
//     public final Long numPoints;
//     public final Long numComments;
// 
//     public ImagePost(final Long id, final String title, final Long points, final Long numComments) {
//         this.id = notNull(id);
//         this.title = notBlank(title);
//         this.numPoints = notNull(points);
//         this.numComments = notNull(numComments);
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/client/models/ImageUpload.java
// public class ImageUpload {
//     public final String title;
//     public final byte[] data;
// 
//     public ImageUpload(final String title, final byte[] data) {
//         this.title = notBlank(title);
//         this.data = notNull(data);
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/domain/Image.java
// public class Image {
//     public final byte[] data;
// 
//     public Image(final byte[] data) {
//         this.data = notNull(data);
//     }
// }


// Path: src/main/java/se/omegapoint/facepalm/client/controllers/ImageController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import se.omegapoint.facepalm.client.adapters.ImageAdapter;
import se.omegapoint.facepalm.client.models.ImageComment;
import se.omegapoint.facepalm.client.models.ImagePost;
import se.omegapoint.facepalm.client.models.ImageUpload;
import se.omegapoint.facepalm.domain.Image;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import static org.apache.commons.lang3.Validate.notNull;

    @RequestMapping("/")
    public String index(final Model model) {
        model.addAttribute("posts", imageAdapter.getTopImagePosts());
        return "index";
    }

    @RequestMapping("/image")
    public String image(final @RequestParam String id, final Model model) {
        final Optional<ImagePost> image = imageAdapter.getImage(id);
        if (!image.isPresent()) {
            return "redirect:/404";
        }

        final List<ImageComment> comments = imageAdapter.getCommentsForImage(image.get().id);
        model.addAttribute("image", image.get());
        model.addAttribute("comments", comments);
        return "image";
    }

    @RequestMapping(value = "/comment", method = RequestMethod.POST)
    public String addComment(final @RequestParam("imageId") String imageId, final @RequestParam("text") String text) {
        imageAdapter.addComment(imageId, text);
        return "redirect:/image?id=" + imageId;
    }

    @RequestMapping(value = "/postImage", method = RequestMethod.POST)
    public String postImage(final @RequestParam("title") String title, final @RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                final byte[] data = notNull(file.getBytes());
                imageAdapter.addImage(new ImageUpload(title, data));