done task 5

This commit is contained in:
2026-01-26 22:15:05 +01:00
parent 100dec28bc
commit b7a3190bff
49 changed files with 97 additions and 27 deletions

View File

@@ -0,0 +1,44 @@
<?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>net.tinsae</groupId>
<artifactId>client</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<junit.version>5.10.0</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>4.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,73 @@
package net.tinsae;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Client {
private String url;
public Client(String url, String table){
this.url = url+table;
}
private static final HttpClient client = HttpClient.newHttpClient();
public int postItem(String json) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.statusCode();
}
public int putItem(int id, String json) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url + "/" + id))
.header("Content-Type", "application/json")
.PUT(HttpRequest.BodyPublishers.ofString(json))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.statusCode();
}
public int deleteItem(int id) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url + "/" + id))
.DELETE()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.statusCode();
}
public HttpResponse<String> getItems() throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
return response;
}
public HttpResponse<String> getItem(int id) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url+"/"+id))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
return response;
}
}

View File

@@ -0,0 +1,136 @@
package net.tinsae;
import java.util.Scanner;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class Delegator {
private final Client client;
private final Scanner sc = new Scanner(System.in);
public Delegator(String path, String table) {
client = new Client(path, table);
}
public void executeCommand(String command) {
switch (command) {
case "delete" -> delegateDelete();
case "insert" -> delegateInsert();
case "read" -> delegateRead();
case "update" -> delegateUpdate();
default -> System.err.println("Unknown command, please repeat");
}
}
private void delegateDelete() {
try {
System.out.print("ID of the item you want to delete: ");
int id = sc.nextInt();
// consume next line, safety
sc.nextLine();
int response = client.deleteItem(id);
System.out.println("Command executed with response code " + response);
} catch (Exception e) {
System.err.println("Error in executing delete command: " + e.getMessage());
}
}
private void delegateInsert() {
try {
System.out.print("Name: ");
String name = sc.nextLine().strip();
System.out.print("Description: ");
String description = sc.nextLine();
System.out.print("Price: ");
float price = sc.nextFloat();
// consume newline, is just safety
sc.nextLine();
int response = client.postItem(makeJson(name, description, price));
System.out.println("Command executed with response code " + response);
} catch (Exception e) {
System.err.println("Error in executing insert command: " + e.getMessage());
}
}
private String makeJson(String name, String desc, float price) {
return String.format("""
{
"name": "%s",
"description": "%s",
"price": %s
}
""", name, desc, price);
}
private void delegateRead() {
try {
System.out.print("Read All or One? (A/O): ");
String input = sc.nextLine();
if (input.equalsIgnoreCase("A")) {
client.getItems();
return;
}
System.out.print("Enter ID: ");
int id = sc.nextInt();
// to be safe
sc.nextLine();
client.getItem(id);
} catch (Exception e) {
System.err.println("Error in executing read command: " + e.getMessage());
}
}
private void delegateUpdate() {
try {
System.out.print("Enter ID: ");
int id = sc.nextInt();
// consume next line
sc.nextLine();
// get old values
String response = client.getItem(id).body();
ObjectMapper mapper = new ObjectMapper();
ObjectNode node = (ObjectNode) mapper.readTree(response);
while (true) {
System.out.println("Which field do you want to update? (name, description, price, done)");
String field = sc.nextLine().trim().toLowerCase();
if (field.equals("done")) {
break;
}
if(!node.has(field)){
System.out.println(" no "+field+" field in database. please choose apropriately");
continue;
}
System.out.println("New value for "+field+": ");
String value = sc.nextLine().trim();
if (field.equals("price")) {
node.put(field, Double.parseDouble(value));
} else {
node.put(field, value);
}
}
String updatedJson = mapper.writeValueAsString(node);
client.putItem(id, updatedJson);
System.out.println("Update successful!");
} catch (Exception e) {
System.err.println("Error in executing update command: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,34 @@
package net.tinsae;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// host url
String path = "http://localhost:8080/shop-1.0-SNAPSHOT/api";
// table name
String table = "/items";
Delegator delegator = new Delegator(path, table);
System.out.println("Hello! What do you want to do? allowed actions are \n"+
"Insert, update, delete, read or exit to close");
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String response = sc.nextLine().strip();
if(response.equalsIgnoreCase("exit") || response.equalsIgnoreCase("done")){
break;
}
delegator.executeCommand(response);
System.out.println("Continue with another command: ");
}
// we must close scanner. leaks happen in java too.
sc.close();
System.out.println("Program ended. \nDon't forget to close the server too. \n Good bye!!!");
}
}

View File

@@ -0,0 +1,3 @@
artifactId=client
groupId=net.tinsae
version=1.0-SNAPSHOT