package org.openforis.users.web;

import static spark.Spark.get;
import static spark.Spark.staticFileLocation;

import org.openforis.users.manager.EntityManagerFactory;

import spark.Request;
import spark.Response;
import spark.Route;
import spark.servlet.SparkApplication;

/**
 * 
 * @author R. Fontanarosa
 * @author S. Ricci
 *
 */
public class Server implements SparkApplication {

	private static final String JSON_CONTENT_TYPE = "application/json";

	@Override
	public void init() {

		staticFileLocation("/public");

		get("/hello/:name", Views.home);
		
		get("/users/all", JSON_CONTENT_TYPE, listAllUsers, new JsonTransformer());
		
		

	}
	
	private Route listAllUsers = (Request req, Response rsp) -> {
		return EntityManagerFactory.getUserManager().listAll();
	};

}

@@ -58,16 +58,48 @@
 rk.get;%0A
+import static spark.Spark.post;%0A
 import s
@@ -193,16 +193,55 @@
 Factory;
+%0Aimport org.openforis.users.model.User;
 %0A%0Aimport
@@ -650,27 +650,8 @@
 ll%22,
- JSON_CONTENT_TYPE,
  lis
@@ -690,16 +690,64 @@
 );%0A%09%09%0A%09%09
+post(%22/users/%22, addUser, new JsonTransformer());
 %0A%0A%09%7D%0A%09%0A%09
@@ -871,10 +871,323 @@
 );%0A%09%7D;%0A%0A
+%09private Route addUser = (Request req, Response rsp) -%3E %7B%0A%09%09String username = req.queryParams(%22username%22);%0A%09%09String password = req.queryParams(%22password%22);%0A%09%09User user = new User();%0A%09%09user.setUsername(username);%0A%09%09user.setPassword(password);%0A%09%09EntityManagerFactory.getUserManager().save(user);%0A%09%09return user;%0A%09%7D;%0A
 %7D%0A

Add user