package de.fred4jupiter.fredbet.service.image;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import de.fred4jupiter.fredbet.domain.ImageGroup;
import de.fred4jupiter.fredbet.repository.ImageGroupRepository;
import de.fred4jupiter.fredbet.web.image.ImageGroupCommand;

@Service
public class ImageGroupService {

	@Autowired
	private ImageGroupRepository imageGroupRepository;

	public List<ImageGroupCommand> findAvailableImageGroups() {
		List<ImageGroup> imageGroups = imageGroupRepository.findAll();

		return imageGroups.stream().map(imageGroup -> mapToImageGroupCommand(imageGroup)).collect(Collectors.toList());
	}

	public void deleteImageGroup(Long imageGroupId) {
		imageGroupRepository.delete(imageGroupId);
	}

	public void addImageGroup(String imageGroupName) {
		ImageGroup foundImageGroup = imageGroupRepository.findByName(imageGroupName);
		if (foundImageGroup != null) {
			throw new ImageGroupExistsException("An image with this group name still exists!");
		} else {
			imageGroupRepository.save(new ImageGroup(imageGroupName));
		}
	}

	private ImageGroupCommand mapToImageGroupCommand(ImageGroup imageGroup) {
		ImageGroupCommand imageGroupCommand = new ImageGroupCommand();
		imageGroupCommand.setId(imageGroup.getId());
		imageGroupCommand.setName(imageGroup.getName());
		return imageGroupCommand;
	}

	public void updateImageGroup(ImageGroupCommand imageGroupCommand) {
		ImageGroup imageGroup = imageGroupRepository.findOne(imageGroupCommand.getId());
		if (imageGroup == null) {
			throw new IllegalArgumentException("Image group with id=" + imageGroupCommand.getId() + " could not be found!");
		}

		imageGroup.setName(imageGroupCommand.getName());
		imageGroupRepository.save(imageGroup);
	}

}

@@ -917,24 +917,654 @@
 oupName) %7B%0D%0A
+%09%09checkImageGroupName(imageGroupName);%0D%0A%09%09imageGroupRepository.save(new ImageGroup(imageGroupName));%0D%0A%09%7D%0D%0A%0D%0A%09public void updateImageGroup(ImageGroupCommand imageGroupCommand) %7B%0D%0A%09%09ImageGroup imageGroup = imageGroupRepository.findOne(imageGroupCommand.getId());%0D%0A%09%09if (imageGroup == null) %7B%0D%0A%09%09%09throw new IllegalArgumentException(%22Image group with id=%22 + imageGroupCommand.getId() + %22 could not be found!%22);%0D%0A%09%09%7D%0D%0A%0D%0A%09%09checkImageGroupName(imageGroupCommand.getName());%0D%0A%0D%0A%09%09imageGroup.setName(imageGroupCommand.getName());%0D%0A%09%09imageGroupRepository.save(imageGroup);%0D%0A%09%7D%0D%0A%0D%0A%09private void checkImageGroupName(String imageGroupName) %7B%0D%0A
 %09%09ImageGroup
@@ -1628,24 +1628,24 @@
 roupName);%0D%0A
-
 %09%09if (foundI
@@ -1758,83 +1758,8 @@
 );%0D%0A
-%09%09%7D else %7B%0D%0A%09%09%09imageGroupRepository.save(new ImageGroup(imageGroupName));%0D%0A
 %09%09%7D%0D
@@ -2007,16 +2007,16 @@
 me());%0D%0A
+
 %09%09return
@@ -2046,414 +2046,7 @@
 %0D%0A%0D%0A
-%09public void updateImageGroup(ImageGroupCommand imageGroupCommand) %7B%0D%0A%09%09ImageGroup imageGroup = imageGroupRepository.findOne(imageGroupCommand.getId());%0D%0A%09%09if (imageGroup == null) %7B%0D%0A%09%09%09throw new IllegalArgumentException(%22Image group with id=%22 + imageGroupCommand.getId() + %22 could not be found!%22);%0D%0A%09%09%7D%0D%0A%0D%0A%09%09imageGroup.setName(imageGroupCommand.getName());%0D%0A%09%09imageGroupRepository.save(imageGroup);%0D%0A%09%7D%0D%0A%0D%0A
 %7D%0D%0A

check image name