package org.binwang.bard.example.crud;

import org.binwang.bard.basic.marker.Doc;
import org.binwang.bard.basic.marker.Required;
import org.binwang.bard.core.Handler;

import javax.ws.rs.*;
import java.util.HashMap;
import java.util.Map;

@Produces("application/json")
@Path("/user")
public class CrudHandler extends Handler {

    public static Map<Integer, User> UserStorage = new HashMap<>();

    @GET
    @Path("/{id}")
    @Doc("Get a user by id")
    public User getUser(@PathParam("id") @Required int id) {
        return UserStorage.get(id);
    }

    @PUT
    @Doc("Create a new user")
    public User insertUser(
        @FormParam("id") @Required int id,
        @FormParam("name") String name) {
        User user = new User();
        user.id = id;
        user.name = name;
        UserStorage.put(id, user);
        return user;
    }

    @DELETE
    @Path("/{id}")
    @Doc("Delete a user")
    public String deleteUser(@PathParam("id") @Required int id) {
        UserStorage.remove(id);
        return "ok";
    }

    @POST
    @Doc("Update a user")
    public User updateUser(
        @FormParam("id") @Required int id,
        @FormParam("name") String name) {
        User user = UserStorage.get(id);
        if (user == null) {
            return null;
        }
        if (name != null) {
            user.name = name;
        }
        UserStorage.put(id, user);
        return user;
    }
}

@@ -75,16 +75,115 @@
 er.Doc;%0A
+import org.binwang.bard.basic.marker.ErrorCase;%0Aimport org.binwang.bard.basic.marker.HandleErrors;%0A
 import o
@@ -221,16 +221,16 @@
 quired;%0A
-
 import o
@@ -994,32 +994,197 @@
 Delete a user%22)%0A
+    @HandleErrors(%7B%0A        @ErrorCase(code = 20000, logLevel = %22DEBUG%22, exception = UserNotFoundException.class,%0A            description = %22user not found%22)%0A    %7D)%0A
     public Strin
@@ -1178,22 +1178,20 @@
  public 
-String
+User
  deleteU
@@ -1220,32 +1220,61 @@
 Required int id)
+ throws UserNotFoundException
  %7B%0A        UserS
@@ -1259,32 +1259,44 @@
 eption %7B%0A       
+ User user =
  UserStorage.rem
@@ -1300,24 +1300,111 @@
 remove(id);%0A
+        if (user == null) %7B%0A            throw new UserNotFoundException(id);%0A        %7D%0A
         retu
@@ -1410,12 +1410,12 @@
 urn 
-%22ok%22
+user
 ;%0A  
@@ -1767,32 +1767,32 @@
 .put(id, user);%0A
-
         return u
@@ -1801,11 +1801,213 @@
 r;%0A    %7D
+%0A%0A    public static class UserNotFoundException extends Exception %7B%0A        public UserNotFoundException(Integer userId) %7B%0A            super(%22user %22 + userId.toString() + %22 not found.%22);%0A        %7D%0A    %7D
 %0A%7D%0A

add user not found exception while delete user