Created
July 20, 2021 12:48
-
-
Save Azahe/048d607a3446895ed24e95cf0d8eb24e to your computer and use it in GitHub Desktop.
FluentScratch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
class Scratch { | |
public static void main(String[] args) { | |
Javalin.create("port or something", Routings | |
.path("/api") | |
.get("/users", UserController::getAll) | |
.post("/users", UserController::create) | |
.subPath("/users") | |
.get("/:user-id", UserController::getOne) | |
.patch("/:user-id", UserController::update) | |
.delete("/:user-id", UserController::delete) | |
.path("/back-to-root") | |
.get("/:user-id", UserController::getOne) | |
); | |
} | |
} | |
class Javalin { | |
public static ServerInstance create(String params, RoutingsCollector collected) { // potentially vararg | |
ServerInstance server = new ServerInstance(); | |
server.resolve(collected.routings()); | |
return server; | |
} | |
} | |
class Routings { | |
public static RoutingsCollector path(String path) { | |
return new RoutingsCollector(path, new ArrayList<>()); | |
} | |
} | |
class RoutingsCollector { | |
private final String rootPath; | |
private final List<Routing> routings; | |
public RoutingsCollector(String path, List<Routing> routings) { | |
this.rootPath = path; | |
this.routings = routings; | |
} | |
public RoutingsCollector get(String path, Runnable mapping) { | |
this.routings.add(new Routing(rootPath + path, mapping)); | |
return this; | |
} | |
public RoutingsCollector post(String path, Runnable mapping) { | |
this.routings.add(new Routing(rootPath + path, mapping)); | |
return this; | |
} | |
public RoutingsCollector patch(String path, Runnable mapping) { | |
this.routings.add(new Routing(rootPath + path, mapping)); | |
return this; | |
} | |
public RoutingsCollector delete(String path, Runnable mapping) { | |
this.routings.add(new Routing(rootPath + path, mapping)); | |
return this; | |
} | |
public RoutingsCollector subPath(String path) { | |
return new RoutingsCollector(rootPath + path, routings); | |
} | |
public RoutingsCollector path(String path) { | |
return new RoutingsCollector(path, routings); | |
} | |
public List<Routing> routings() { | |
return routings; | |
} | |
} | |
class Routing { | |
private final String path; | |
private final Runnable mapping; | |
public Routing(String path, Runnable mapping) { | |
this.path = path; | |
this.mapping = mapping; | |
} | |
@Override | |
public String toString() { | |
return "Routing{" + | |
"path='" + path + '\'' + | |
", mapping=" + mapping + | |
'}'; | |
} | |
} | |
class ServerInstance { | |
public void resolve(List<Routing> routings) { | |
// as a form of a test | |
System.out.println("routings = " + routings); | |
throw new RuntimeException("TODO"); | |
} | |
} | |
class UserController { | |
public static void getAll() {} | |
public static void create() {} | |
public static void getOne() {} | |
public static void update() {} | |
public static void delete() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment