commit after reset
This commit is contained in:
@@ -7,19 +7,13 @@ import java.net.http.HttpResponse;
|
||||
|
||||
public class Client {
|
||||
private String url;
|
||||
private String table = "/items";
|
||||
|
||||
public Client(String url){
|
||||
public Client(String url, String table){
|
||||
this.url = url+table;
|
||||
}
|
||||
|
||||
private static final HttpClient client = HttpClient.newHttpClient();
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public int postItem(String json) throws Exception {
|
||||
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()
|
||||
.uri(new URI(url))
|
||||
.GET()
|
||||
@@ -62,10 +56,10 @@ public class Client {
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
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()
|
||||
.uri(new URI(url+"/"+id))
|
||||
.GET()
|
||||
@@ -73,6 +67,7 @@ public class Client {
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
System.out.println(response.body());
|
||||
return response.statusCode();
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +1,17 @@
|
||||
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) {
|
||||
client = new Client(path);
|
||||
public Delegator(String path, String table) {
|
||||
client = new Client(path, table);
|
||||
}
|
||||
|
||||
public void executeCommand(String command) {
|
||||
@@ -25,7 +28,8 @@ public class Delegator {
|
||||
try {
|
||||
System.out.print("ID of the item you want to delete: ");
|
||||
int id = sc.nextInt();
|
||||
sc.nextLine(); // consume newline
|
||||
// consume next line, safety
|
||||
sc.nextLine();
|
||||
int response = client.deleteItem(id);
|
||||
System.out.println("Command executed with response code " + response);
|
||||
} catch (Exception e) {
|
||||
@@ -43,7 +47,8 @@ public class Delegator {
|
||||
|
||||
System.out.print("Price: ");
|
||||
float price = sc.nextFloat();
|
||||
sc.nextLine(); // consume newline
|
||||
// consume newline, is just safety
|
||||
sc.nextLine();
|
||||
|
||||
int response = client.postItem(makeJson(name, description, price));
|
||||
System.out.println("Command executed with response code " + response);
|
||||
@@ -75,7 +80,8 @@ public class Delegator {
|
||||
|
||||
System.out.print("Enter ID: ");
|
||||
int id = sc.nextInt();
|
||||
sc.nextLine(); // consume newline
|
||||
// to be safe
|
||||
sc.nextLine();
|
||||
client.getItem(id);
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -84,6 +90,47 @@ public class Delegator {
|
||||
}
|
||||
|
||||
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 static void main(String[] args) {
|
||||
|
||||
// host url
|
||||
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"+
|
||||
"Insert, update, delete and read");
|
||||
"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")){
|
||||
return;
|
||||
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!!!");
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user