package com.tymiller.controller;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@RestController
@RequestMapping("/rest")
public class PhotoResource {

	private String imageName;
	
	@Autowired
	private PhotoService photoService;
	
	@RequestMapping(value="/photo/upload", method = RequestMethod.POST)
	public String upload(HttpServletResponse response, HttpServletRequest request) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Iterator<String> it = multipartRequest.getFileNames();
		MultipartFile multipartFile = multipartRequest.getFile(it.next());
		String fileName = multipartFile.getOriginalFilename();
		imageName=fileName;
		
		String path = new File("target" + File.separator
							  + "classes" + File.separator
							  +"static" + File.separator
							  + "images").getAbsolutePath() + File.separator + fileName;
		try {
			multipartFile.transferTo(new File(path));
			System.out.println(path);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return "Upload Success!";
	}
}

@@ -261,16 +261,76 @@
 owired;%0A
+import org.springframework.web.bind.annotation.RequestBody;%0A
 import o
@@ -636,16 +636,50 @@
 quest;%0A%0A
+import com.tymiller.model.Photo;%0A%0A
 @RestCon
@@ -1632,10 +1632,200 @@
 s!%22;%0A%09%7D%0A
+%09%0A%09@RequestMapping(value=%22/photo/add%22, method = RequestMethod.POST)%0A%09public Photo addPhoto(@RequestBody Photo photo) %7B%0A%09%09photo.setImageName(imageName);%0A%09%09return photoService.save(photo);%0A%09%7D%0A
 %7D%0A

add method to save photo