forked from RemiAncay/dai-lab-http-infrastructure
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>ch.heig.dai</groupId> | ||
<artifactId>ch.heig.dai</artifactId> | ||
<version>1.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<maven.compiler.source>21</maven.compiler.source> | ||
<maven.compiler.target>21</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<version>3.6.0</version> | ||
<configuration> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
<archive> | ||
<manifest> | ||
<mainClass>ch.heig.dai.Main</mainClass> | ||
</manifest> | ||
</archive> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>make-assembly</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>single</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.javalin</groupId> | ||
<artifactId>javalin</artifactId> | ||
<version>5.6.3</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
<version>2.0.7</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>2.15.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package ch.heig.dai; | ||
|
||
import io.javalin.*; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
|
||
Javalin app = Javalin.create().start(7000); | ||
|
||
UserController userController = new UserController(); | ||
app.get("/api/users", userController::getAll); | ||
app.get("/api/users/{id}", userController::getOne); | ||
app.post("/api/users/", userController::create); | ||
app.put("/api/users/{id}", userController::update); | ||
app.delete("/api/users/{id}", userController::delete); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ch.heig.dai; | ||
|
||
public class User { | ||
public String firstName = ""; | ||
public String lastName = ""; | ||
|
||
public User() { } | ||
|
||
public User(String firstName, String lastName) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package ch.heig.dai; | ||
|
||
import io.javalin.http.Context; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
class UserController { | ||
|
||
// "Database" of users | ||
// Since the server is multi-threaded, we need to use a thread-safe data structure | ||
// such as ConcurrentHashMap or HashMap | ||
private ConcurrentHashMap<Integer, User> users = new ConcurrentHashMap<Integer, User>(); | ||
private int lastId = 0; | ||
|
||
public UserController() { | ||
// Add some users to the "database" | ||
users.put(++lastId, new User("Anita", "Braig")); | ||
users.put(++lastId, new User("Bill", "Ding")); | ||
users.put(++lastId, new User("Chris P.", "Bacon")); | ||
} | ||
|
||
public void getOne(Context ctx) { | ||
int id = Integer.parseInt(ctx.pathParam("id")); | ||
ctx.json(users.get(id)); | ||
} | ||
|
||
public void getAll(Context ctx) { | ||
ctx.json(users); | ||
} | ||
|
||
public void create(Context ctx) { | ||
User user = ctx.bodyAsClass(User.class); | ||
users.put(++lastId, user); | ||
ctx.status(201); | ||
} | ||
|
||
public void delete(Context ctx) { | ||
int id = Integer.parseInt(ctx.pathParam("id")); | ||
users.remove(id); | ||
ctx.status(204); | ||
} | ||
|
||
public void update(Context ctx) { | ||
int id = Integer.parseInt(ctx.pathParam("id")); | ||
User user = ctx.bodyAsClass(User.class); | ||
users.put(id, user); | ||
ctx.status(200); | ||
} | ||
|
||
} |