|
| 1 | +import io.javalin.http.Context; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class UserControllerJ { |
| 5 | + |
| 6 | + public record User(String name, String email) {} |
| 7 | + |
| 8 | + private static final Map<String, User> users; |
| 9 | + |
| 10 | + static { |
| 11 | + var tempMap = Map.of( |
| 12 | + randomId(), new User( "Alice", "[email protected]"), |
| 13 | + randomId(), new User( "Bob", "[email protected]"), |
| 14 | + randomId(), new User( "Carol", "[email protected]"), |
| 15 | + randomId(), new User( "Dave", "[email protected]") |
| 16 | + ); |
| 17 | + users = new HashMap<>(tempMap); |
| 18 | + } |
| 19 | + |
| 20 | + public static void getAllUserIds(Context ctx) { |
| 21 | + ctx.json(users.keySet()); |
| 22 | + } |
| 23 | + |
| 24 | + public static void createUser(Context ctx) { |
| 25 | + users.put(randomId(), ctx.bodyAsClass(User.class)); |
| 26 | + } |
| 27 | + |
| 28 | + public static void getUser(Context ctx) { |
| 29 | + ctx.json(users.get(ctx.pathParam("userId"))); |
| 30 | + } |
| 31 | + |
| 32 | + public static void updateUser(Context ctx) { |
| 33 | + users.put(ctx.pathParam("userId"), ctx.bodyAsClass(User.class)); |
| 34 | + } |
| 35 | + |
| 36 | + public static void deleteUser(Context ctx) { |
| 37 | + users.remove(ctx.pathParam("userId")); |
| 38 | + } |
| 39 | + |
| 40 | + private static String randomId() { |
| 41 | + return UUID.randomUUID().toString(); |
| 42 | + } |
| 43 | + |
| 44 | +} |
0 commit comments