// 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/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/ImageComment.java
// public class ImageComment {
// 
//     public final Long imageId;
//     public final String author;
//     public final String text;
// 
//     public ImageComment(final Long imageId, final String author, final String text) {
//         this.imageId = imageId;
//         this.author = author;
//         this.text = text;
//     }
// 
//     @Override
//     public String toString() {
//         return "ImageComment{" +
//                 "imageId=" + imageId +
//                 ", author='" + author + '\'' +
//                 ", text='" + text + '\'' +
//                 '}';
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/domain/ImagePost.java
// public class ImagePost {
//     public final Long id;
//     public final Title title;
//     public final NumberOfPoints numPoints;
//     public final NumberOfComments numComments;
// 
//     public ImagePost(final Long id, final Title title, final NumberOfPoints points, final NumberOfComments numComments) {
//         this.id = notNull(id);
//         this.title = notNull(title);
//         this.numPoints = notNull(points);
//         this.numComments = notNull(numComments);
//     }
// }
// 
// 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/domain/repository/CommentRepository.java
// public interface CommentRepository {
//     List<ImageComment> findByImageId(final Long id);
// 
//     void addComment(Long imageId, String author, String text);
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/domain/repository/ImageRepository.java
// public interface ImageRepository {
//     List<ImagePost> findAll();
// 
//     Optional<ImagePost> findById(String id);
// 
//     void addImagePost(Title title, byte[] data);
// 
//     Image findImageByPostId(Long id);
// }


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.stereotype.Service;
import se.omegapoint.facepalm.application.transfer.NewImageComment;
import se.omegapoint.facepalm.domain.Image;
import se.omegapoint.facepalm.domain.ImageComment;
import se.omegapoint.facepalm.domain.ImagePost;
import se.omegapoint.facepalm.domain.Title;
import se.omegapoint.facepalm.domain.repository.CommentRepository;
import se.omegapoint.facepalm.domain.repository.ImageRepository;
import java.util.List;
import java.util.Optional;
import static java.util.stream.Collectors.toList;
/*
 * 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.application;




@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);
// 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/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/ImageComment.java
// public class ImageComment {
// 
//     public final Long imageId;
//     public final String author;
//     public final String text;
// 
//     public ImageComment(final Long imageId, final String author, final String text) {
//         this.imageId = imageId;
//         this.author = author;
//         this.text = text;
//     }
// 
//     @Override
//     public String toString() {
//         return "ImageComment{" +
//                 "imageId=" + imageId +
//                 ", author='" + author + '\'' +
//                 ", text='" + text + '\'' +
//                 '}';
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/domain/ImagePost.java
// public class ImagePost {
//     public final Long id;
//     public final Title title;
//     public final NumberOfPoints numPoints;
//     public final NumberOfComments numComments;
// 
//     public ImagePost(final Long id, final Title title, final NumberOfPoints points, final NumberOfComments numComments) {
//         this.id = notNull(id);
//         this.title = notNull(title);
//         this.numPoints = notNull(points);
//         this.numComments = notNull(numComments);
//     }
// }
// 
// 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/domain/repository/CommentRepository.java
// public interface CommentRepository {
//     List<ImageComment> findByImageId(final Long id);
// 
//     void addComment(Long imageId, String author, String text);
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/domain/repository/ImageRepository.java
// public interface ImageRepository {
//     List<ImagePost> findAll();
// 
//     Optional<ImagePost> findById(String id);
// 
//     void addImagePost(Title title, byte[] data);
// 
//     Image findImageByPostId(Long id);
// }


// Path: src/main/java/se/omegapoint/facepalm/application/ImageService.java
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.stereotype.Service;
import se.omegapoint.facepalm.application.transfer.NewImageComment;
import se.omegapoint.facepalm.domain.Image;
import se.omegapoint.facepalm.domain.ImageComment;
import se.omegapoint.facepalm.domain.ImagePost;
import se.omegapoint.facepalm.domain.Title;
import se.omegapoint.facepalm.domain.repository.CommentRepository;
import se.omegapoint.facepalm.domain.repository.ImageRepository;
import java.util.List;
import java.util.Optional;
import static java.util.stream.Collectors.toList;

/*
 * 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.application;




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