package org.binwang.bard.example.db;

import org.binwang.bard.basic.marker.*;
import org.binwang.bard.core.Handler;
import org.binwang.bard.util.db.marker.DBSession;
import org.hibernate.ObjectNotFoundException;
import org.hibernate.Session;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

@LogRequest
@Produces("application/json")
public class SimpleHandler extends Handler {

    @GET
    @Path("/get")
    @Doc("Get a user")
    @HandleErrors({
        @ErrorCase(code = 20000, exception = ObjectNotFoundException.class, logLevel = "DEBUG",
            description = "User not found")
    })
    public User getUser(
        @Doc("User id") @QueryParam("id") @Required long userId,
        @DBSession Session dbSession
    ) {
        User user = new User();
        dbSession.load(user, userId);
        return user;
    }

    @GET
    @Path("/insert")
    @Doc("Insert a user")
    public String insertUser(
        @Doc("User id") @QueryParam("id") @Required long id,
        @Doc("User name") @QueryParam("name") @Required String name,
        @DBSession Session dbSession
    ) {
        User user = new User();
        user.id = id;
        user.name = name;
        dbSession.save(user);
        return "ok";
    }
}

@@ -434,16 +434,58 @@
 dler %7B%0A%0A
+    @DBSession public Session dbSession;%0A%0A
     @GET
@@ -477,32 +477,32 @@
 sion;%0A%0A    @GET%0A
-
     @Path(%22/get%22
@@ -785,46 +785,8 @@
 erId
-,%0A        @DBSession Session dbSession
 %0A   
@@ -1035,16 +1035,16 @@
 ong id,%0A
+
         
@@ -1106,46 +1106,8 @@
 name
-,%0A        @DBSession Session dbSession
 %0A   

move dbSession to field