// Path: src/main/java/se/omegapoint/facepalm/application/ImageService.java
// @Service
// public class ImageService {
// 
//     @Autowired
//     private ImageRepository imageRepository;
// 
//     @Autowired
//     private CommentRepository commentRepository;
// 
//     public List<ImagePost> getTopImages() {
//         return imageRepository.findAll();
//     }
// 
//     public Optional<ImagePost> getImagePost(final String id) {
//         notBlank(id);
// 
//         return imageRepository.findById(id);
//     }
// 
//     public List<ImageComment> getCommentsForImage(final Long id) {
//         notNull(id);
// 
//         return commentRepository.findByImageId(id).stream().collect(toList());
//     }
// 
//     public void addComment(final NewImageComment newImageComment) {
//         notNull(newImageComment);
// 
//         commentRepository.addComment(newImageComment.imageId, newImageComment.author, newImageComment.text);
//     }
// 
//     public void addImagePost(final Title title, final byte[] data) {
//         notNull(title);
//         notNull(data);
//         imageRepository.addImagePost(title, data);
//     }
// 
//     public Image getImageFor(final Long id) {
//         notNull(id);
//         return imageRepository.findImageByPostId(id);
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/application/transfer/NewImageComment.java
// public class NewImageComment {
//     public final Long imageId;
//     public final String author;
//     public final String text;
// 
//     public NewImageComment(final Long imageId, final String author, final String text) {
//         this.imageId = notNull(imageId);
//         this.author = notBlank(author);
//         this.text = notBlank(text);
//     }
// }
// 
// 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/client/security/AuthenticatedUser.java
// public class AuthenticatedUser {
//     public final String userName;
// 
//     public AuthenticatedUser(final String userName) {
//         this.userName = notBlank(userName);
//     }
// }
// 
// 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/domain/Title.java
// public class Title {
//     private static final int MAN_LENGTH = 100;
// 
//     public final String value;
// 
//     public Title(final String value) {
//         this.value = notBlank(value);
//         isTrue(this.value.length() < MAN_LENGTH, "Title can be maximum 100 characters");
//     }
// }


import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang3.Validate.notBlank;
import static org.apache.commons.lang3.Validate.notNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import se.omegapoint.facepalm.application.ImageService;
import se.omegapoint.facepalm.application.transfer.NewImageComment;
import se.omegapoint.facepalm.client.config.Adapter;
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.client.security.AuthenticatedUser;
import se.omegapoint.facepalm.domain.Image;
import se.omegapoint.facepalm.domain.Title;
import java.util.List;
import java.util.Optional;
/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package se.omegapoint.facepalm.client.adapters;




@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))
// Path: src/main/java/se/omegapoint/facepalm/application/ImageService.java
// @Service
// public class ImageService {
// 
//     @Autowired
//     private ImageRepository imageRepository;
// 
//     @Autowired
//     private CommentRepository commentRepository;
// 
//     public List<ImagePost> getTopImages() {
//         return imageRepository.findAll();
//     }
// 
//     public Optional<ImagePost> getImagePost(final String id) {
//         notBlank(id);
// 
//         return imageRepository.findById(id);
//     }
// 
//     public List<ImageComment> getCommentsForImage(final Long id) {
//         notNull(id);
// 
//         return commentRepository.findByImageId(id).stream().collect(toList());
//     }
// 
//     public void addComment(final NewImageComment newImageComment) {
//         notNull(newImageComment);
// 
//         commentRepository.addComment(newImageComment.imageId, newImageComment.author, newImageComment.text);
//     }
// 
//     public void addImagePost(final Title title, final byte[] data) {
//         notNull(title);
//         notNull(data);
//         imageRepository.addImagePost(title, data);
//     }
// 
//     public Image getImageFor(final Long id) {
//         notNull(id);
//         return imageRepository.findImageByPostId(id);
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/application/transfer/NewImageComment.java
// public class NewImageComment {
//     public final Long imageId;
//     public final String author;
//     public final String text;
// 
//     public NewImageComment(final Long imageId, final String author, final String text) {
//         this.imageId = notNull(imageId);
//         this.author = notBlank(author);
//         this.text = notBlank(text);
//     }
// }
// 
// 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/client/security/AuthenticatedUser.java
// public class AuthenticatedUser {
//     public final String userName;
// 
//     public AuthenticatedUser(final String userName) {
//         this.userName = notBlank(userName);
//     }
// }
// 
// 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/domain/Title.java
// public class Title {
//     private static final int MAN_LENGTH = 100;
// 
//     public final String value;
// 
//     public Title(final String value) {
//         this.value = notBlank(value);
//         isTrue(this.value.length() < MAN_LENGTH, "Title can be maximum 100 characters");
//     }
// }


// Path: src/main/java/se/omegapoint/facepalm/client/adapters/ImageAdapter.java
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang3.Validate.notBlank;
import static org.apache.commons.lang3.Validate.notNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import se.omegapoint.facepalm.application.ImageService;
import se.omegapoint.facepalm.application.transfer.NewImageComment;
import se.omegapoint.facepalm.client.config.Adapter;
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.client.security.AuthenticatedUser;
import se.omegapoint.facepalm.domain.Image;
import se.omegapoint.facepalm.domain.Title;
import java.util.List;
import java.util.Optional;

/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package se.omegapoint.facepalm.client.adapters;




@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());