package fr.univnantes.atal.web.trashnao.webservices;

import fr.univnantes.atal.web.trashnao.model.User;
import fr.univnantes.atal.web.trashnao.persistence.PMF;
import fr.univnantes.atal.web.trashnao.security.TokenVerifier;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserService extends WebService {
    
    PersistenceManager pm = PMF.get().getPersistenceManager();
    
    @Override
    protected void get(
            HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        User user = TokenVerifier.getUser(request);
        Map<String, String> data = new HashMap<>();
        data.put("id", user.getGoogleId());
        data.put("email", user.getEmail());
    }

    @Override
    protected void post(
            HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        error(request, response, "Unsupported method: POST");
    }

    @Override
    protected void put(
            HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        error(request, response, "Unsupported method: PUT");
    }

    @Override
    protected void delete(
            HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        User user = TokenVerifier.getUser(request);
        pm.deletePersistent(user);
    }
}

@@ -509,21 +509,17 @@
 rvice %7B%0A
-    
 %0A
+
     Pers
@@ -577,13 +577,9 @@
 ();%0A
-    
 %0A
+
     
@@ -849,16 +849,48 @@
 ap%3C%3E();%0A
+        if (user != null) %7B%0A    
         
@@ -925,32 +925,36 @@
 eId());%0A        
+    
 data.put(%22email%22
@@ -969,24 +969,187 @@
 etEmail());%0A
+        %7D else %7B%0A            error(request,%0A                    response,%0A                    %22Couldn't identify the user thanks to the access_token.%22);%0A        %7D%0A
     %7D%0A%0A    @
@@ -1841,32 +1841,64 @@
 tUser(request);%0A
+        if (user != null) %7B%0A    
         pm.delet
@@ -1916,16 +1916,179 @@
 (user);%0A
+        %7D else %7B%0A            error(request,%0A                    response,%0A                    %22Couldn't identify the user thanks to the access_token.%22);%0A        %7D%0A
     %7D%0A%7D%0A

handle authentication for the user webservice