Skip to content

Commit

Permalink
how to build an api
Browse files Browse the repository at this point in the history
  • Loading branch information
Simeline committed Dec 14, 2023
1 parent 69826f0 commit d619721
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
63 changes: 63 additions & 0 deletions pom.xml
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.
17 changes: 17 additions & 0 deletions src/main/java/ch/heig/dai/Main.java
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);
}
}
13 changes: 13 additions & 0 deletions src/main/java/ch/heig/dai/User.java
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;
}
}
49 changes: 49 additions & 0 deletions src/main/java/ch/heig/dai/UserController.java
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);
}

}

0 comments on commit d619721

Please sign in to comment.