package com.softmotions.ncms.media.rest;

import com.avaje.ebean.EbeanServer;
import com.google.inject.Inject;
import com.softmotions.ncms.media.db.MediaDataManager;
import com.softmotions.ncms.media.model.MediaFolder;

import javax.ws.rs.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

/**
 * Created by shu on 4/23/2014.
 */

/*
+ get 	/folders
+ get 	/folders/id
+ put	/folders/id/id
*/
@Path("media/folders")
public class FoldersRestController {

	@Inject
	MediaDataManager manager;

	@Inject
	EbeanServer ebean;

	@GET
	@Path("/")
	@Produces("text/plain")
	public String getRootFolders() {
		List<MediaFolder> folders = manager.getRootFolders();
		return manager.dump(null, 0);
	}

	@GET
	@Path("/{id}")
	public String getSubSolders(@PathParam("id") Long id) {
		MediaFolder folder = ebean.find(MediaFolder.class, id);
		return manager.dump(folder, 0);
	}

	@PUT
	@Path("/{folderId}/{hostId}")
	public String moveFolder(@PathParam("folderId") Long folderId, @PathParam("hostId") Long hostId) {
		MediaFolder folder = ebean.find(MediaFolder.class, folderId);
		MediaFolder host = ebean.find(MediaFolder.class, hostId);
		folder.setParent(host);
		ebean.update(folder);
		return manager.dump(null, 0);
	}

}

@@ -235,16 +235,50 @@
 s.rs.*;%0A
+import javax.ws.rs.core.Response;%0A
 import j
@@ -431,18 +431,18 @@
 rs/id%0A+ 
-pu
+ge
 t%09/folde
@@ -596,16 +596,20 @@
 @Path(%22/
+show
 %22)%0A%09@Pro
@@ -643,30 +643,20 @@
  String 
-getRootFolders
+show
 () %7B%0A%09%09L
@@ -763,29 +763,212 @@
 h(%22/
-%7Bid%7D%22)%0A%09public String
+%22)%0A%09@Produces(%22application/json%22)%0A%09public List%3CMediaFolder%3E getRootFolders() %7B%0A%09%09List%3CMediaFolder%3E folders = manager.getRootFolders();%0A%09%09return folders;%0A%09%7D%0A%0A%09@GET%0A%09@Path(%22/%7Bid%7D%22)%0A%09public List%3CMediaFolder%3E
  get
@@ -1064,16 +1064,101 @@
 s, id);%0A
+%09%09if(folder == null) %7B%0A%09%09%09throw new RuntimeException(%22Folder not found: %22 + id);%0A%09%09%7D%0A
 %09%09return
@@ -1170,22 +1170,28 @@
 ger.
-dump
+getSubFolders
 (folder
-, 0
 );%0A%09
@@ -1199,10 +1199,10 @@
 %0A%0A%09@
-PU
+GE
 T%0A%09@
@@ -1209,23 +1209,17 @@
 Path(%22/%7B
-folderI
+i
 d%7D/%7Bhost
@@ -1232,22 +1232,24 @@
 %09public 
-String
+Response
  moveFol
@@ -1264,23 +1264,17 @@
 hParam(%22
-folderI
+i
 d%22) Long
@@ -1274,23 +1274,17 @@
 %22) Long 
-folderI
+i
 d, @Path
@@ -1371,78 +1371,278 @@
 ss, 
-folderId);%0A%09%09MediaFolder host = ebean.find(MediaFolder.class, hostId);
+id);%0A%09%09if(folder == null) %7B%0A%09%09%09return Response.status(500).entity(%22Folder not found: %22 + id).build();%0A%09%09%7D%0A%09%09MediaFolder host = ebean.find(MediaFolder.class, hostId);%0A%09%09if(folder == null) %7B%0A%09%09%09return Response.status(500).entity(%22Folder not found: %22 + hostId).build();%0A%09%09%7D
 %0A%09%09f
@@ -1697,36 +1697,66 @@
 %09return 
-manager.dump(null, 0
+Response.status(200).entity(%22moved: %22 + id).build(
 );%0A%09%7D%0A%0A%7D

add folder api