commit after reset
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
|
|||||||
|
net/tinsae/Client.class
|
||||||
|
net/tinsae/Main.class
|
||||||
|
net/tinsae/Delegator.class
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/client/src/main/java/net/tinsae/Client.java
|
||||||
|
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/client/src/main/java/net/tinsae/Delegator.java
|
||||||
|
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/client/src/main/java/net/tinsae/Main.java
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_1.xsd">
|
||||||
|
|
||||||
|
</beans>
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
|
|||||||
|
artifactId=shop
|
||||||
|
groupId=net.tinsae.rest
|
||||||
|
version=1.0-SNAPSHOT
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
net/tinsae/shop/App.class
|
||||||
|
net/tinsae/shop/Item.class
|
||||||
|
net/tinsae/shop/Util.class
|
||||||
|
net/tinsae/shop/Factory.class
|
||||||
|
net/tinsae/shop/InvalidItemException.class
|
||||||
|
net/tinsae/shop/ItemDao.class
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/shop/src/main/java/net/tinsae/shop/App.java
|
||||||
|
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/shop/src/main/java/net/tinsae/shop/Factory.java
|
||||||
|
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/shop/src/main/java/net/tinsae/shop/InvalidItemException.java
|
||||||
|
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/shop/src/main/java/net/tinsae/shop/Item.java
|
||||||
|
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/shop/src/main/java/net/tinsae/shop/ItemDao.java
|
||||||
|
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/shop/src/main/java/net/tinsae/shop/Util.java
|
||||||
@@ -7,19 +7,13 @@ import java.net.http.HttpResponse;
|
|||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
private String url;
|
private String url;
|
||||||
private String table = "/items";
|
|
||||||
|
|
||||||
public Client(String url){
|
public Client(String url, String table){
|
||||||
this.url = url+table;
|
this.url = url+table;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final HttpClient client = HttpClient.newHttpClient();
|
private static final HttpClient client = HttpClient.newHttpClient();
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public int postItem(String json) throws Exception {
|
public int postItem(String json) throws Exception {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
@@ -54,7 +48,7 @@ public class Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public int getItems() throws Exception {
|
public HttpResponse<String> getItems() throws Exception {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI(url))
|
.uri(new URI(url))
|
||||||
.GET()
|
.GET()
|
||||||
@@ -62,10 +56,10 @@ public class Client {
|
|||||||
|
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
System.out.println(response.body());
|
System.out.println(response.body());
|
||||||
return response.statusCode();
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getItem(int id) throws Exception {
|
public HttpResponse<String> getItem(int id) throws Exception {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI(url+"/"+id))
|
.uri(new URI(url+"/"+id))
|
||||||
.GET()
|
.GET()
|
||||||
@@ -73,6 +67,7 @@ public class Client {
|
|||||||
|
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
System.out.println(response.body());
|
System.out.println(response.body());
|
||||||
return response.statusCode();
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,14 +1,17 @@
|
|||||||
package net.tinsae;
|
package net.tinsae;
|
||||||
|
|
||||||
import java.util.Scanner;
|
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 {
|
public class Delegator {
|
||||||
|
|
||||||
private final Client client;
|
private final Client client;
|
||||||
private final Scanner sc = new Scanner(System.in);
|
private final Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
public Delegator(String path) {
|
public Delegator(String path, String table) {
|
||||||
client = new Client(path);
|
client = new Client(path, table);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void executeCommand(String command) {
|
public void executeCommand(String command) {
|
||||||
@@ -25,7 +28,8 @@ public class Delegator {
|
|||||||
try {
|
try {
|
||||||
System.out.print("ID of the item you want to delete: ");
|
System.out.print("ID of the item you want to delete: ");
|
||||||
int id = sc.nextInt();
|
int id = sc.nextInt();
|
||||||
sc.nextLine(); // consume newline
|
// consume next line, safety
|
||||||
|
sc.nextLine();
|
||||||
int response = client.deleteItem(id);
|
int response = client.deleteItem(id);
|
||||||
System.out.println("Command executed with response code " + response);
|
System.out.println("Command executed with response code " + response);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -43,7 +47,8 @@ public class Delegator {
|
|||||||
|
|
||||||
System.out.print("Price: ");
|
System.out.print("Price: ");
|
||||||
float price = sc.nextFloat();
|
float price = sc.nextFloat();
|
||||||
sc.nextLine(); // consume newline
|
// consume newline, is just safety
|
||||||
|
sc.nextLine();
|
||||||
|
|
||||||
int response = client.postItem(makeJson(name, description, price));
|
int response = client.postItem(makeJson(name, description, price));
|
||||||
System.out.println("Command executed with response code " + response);
|
System.out.println("Command executed with response code " + response);
|
||||||
@@ -75,7 +80,8 @@ public class Delegator {
|
|||||||
|
|
||||||
System.out.print("Enter ID: ");
|
System.out.print("Enter ID: ");
|
||||||
int id = sc.nextInt();
|
int id = sc.nextInt();
|
||||||
sc.nextLine(); // consume newline
|
// to be safe
|
||||||
|
sc.nextLine();
|
||||||
client.getItem(id);
|
client.getItem(id);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -84,6 +90,47 @@ public class Delegator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void delegateUpdate() {
|
private void delegateUpdate() {
|
||||||
System.out.println("Update not implemented yet");
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,21 +4,31 @@ import java.util.Scanner;
|
|||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// host url
|
||||||
String path = "http://localhost:8080/shop-1.0-SNAPSHOT/api";
|
String path = "http://localhost:8080/shop-1.0-SNAPSHOT/api";
|
||||||
Delegator delegator = new Delegator(path);
|
|
||||||
|
// 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"+
|
System.out.println("Hello! What do you want to do? allowed actions are \n"+
|
||||||
"Insert, update, delete and read");
|
"Insert, update, delete, read or exit to close");
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
while(sc.hasNext()){
|
while(sc.hasNext()){
|
||||||
String response = sc.nextLine().strip();
|
String response = sc.nextLine().strip();
|
||||||
if(response.equalsIgnoreCase("exit")){
|
if(response.equalsIgnoreCase("exit") || response.equalsIgnoreCase("done")){
|
||||||
return;
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
delegator.executeCommand(response);
|
delegator.executeCommand(response);
|
||||||
|
System.out.println("Continue with another command: ");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
// we must close scanner. leaks happen in java too.
|
||||||
sc.close();
|
sc.close();
|
||||||
|
System.out.println("Program ended. \nDon't forget to close the server too. \n Good bye!!!");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,22 +1,30 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>net.tinsae.rest</groupId>
|
<groupId>net.tinsae</groupId>
|
||||||
<artifactId>shop</artifactId>
|
<artifactId>client</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<name>shop</name>
|
<build>
|
||||||
<packaging>war</packaging>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>net.tinsae.Main</mainClass>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
<maven.compiler.target>21</maven.compiler.target>
|
|
||||||
<maven.compiler.source>21</maven.compiler.source>
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
<junit.version>5.13.2</junit.version>
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
|
<junit.version>5.10.0</junit.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>jakarta.enterprise</groupId>
|
<groupId>jakarta.enterprise</groupId>
|
||||||
@@ -30,18 +38,7 @@
|
|||||||
<version>4.0.0</version>
|
<version>4.0.0</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.xerial</groupId>
|
|
||||||
<artifactId>sqlite-jdbc</artifactId>
|
|
||||||
<version>3.45.3.0</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>jakarta.servlet</groupId>
|
|
||||||
<artifactId>jakarta.servlet-api</artifactId>
|
|
||||||
<version>6.1.0</version>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter-api</artifactId>
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
@@ -54,15 +51,16 @@
|
|||||||
<version>${junit.version}</version>
|
<version>${junit.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>2.15.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>2.16.2</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-war-plugin</artifactId>
|
|
||||||
<version>3.4.0</version>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
</project>
|
</project>
|
||||||
Reference in New Issue
Block a user