@@ -6,6 +6,7 @@
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

import com.google.gson.Gson;

import repo.Shop;
@@ -14,32 +15,26 @@ public class ClientTest {
public static void main(String[] args) throws IOException {

Gson gson = new Gson();
String string = "";

// INSERE UM VALOR NO BANCO
string = "{"+
"\"nome\": \"abc\","+
"\"idade\": \"10\""+
"}";

try {
URL url = new URL("http://localhost:8080/DBRest/rest/escreve");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(string);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
in.close();
} catch (Exception e) {
e.printStackTrace();
}
String string = "{\"name\": \"abc\" , \"cnpj\": \"10\" , \"template\": \"1\"}";

try {
URL url = new URL("http://localhost:8080/DBRest/rest/shop/add");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(string);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
in.close();
} catch (Exception e) {
e.printStackTrace();
}

// LE O BANCO
URL url = new URL("http://localhost:8080/DBRest/rest/le");
URL url = new URL("http://localhost:8080/DBRest/rest/shop/readAll");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
@@ -49,17 +44,17 @@ public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
while ((line = br.readLine()) != null && !line.isEmpty()) {
sb.append(line);
}
br.close();
System.out.println("banco:");
System.out.println("dataBase:");
Shop[] list = gson.fromJson(sb.toString(), Shop[].class);
int i = 0;
for (Shop p : list) {
i++;
System.out.println(p);
}
System.out.println("banco: "+i);
System.out.println("dataBase stores: " + i);
}
}
@@ -4,11 +4,13 @@
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;

import repo.Shop;
@@ -24,14 +26,14 @@ public class ShopRest {
@Path("add")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public String addStore(String data) throws IOException {
Shop store = gson.fromJson(data, Shop.class);
public String addShop(String data) throws IOException {
Shop shop = gson.fromJson(data, Shop.class);
for(Shop s: repo.read()){
if(store.getCnpj().equals(s.getCnpj())){
if(shop.getCnpj().equals(s.getCnpj())){
return "repetido";
}
}
repo.add(store);
repo.add(shop);
return "novo";
}

@@ -44,24 +46,26 @@ public String readAllShops() {
return json;
}

@DELETE
@Path("remove")
@Consumes(MediaType.APPLICATION_JSON)
public void removeShop(String data){
repo.remove(data);
}

//loads store into memory
@POST
@Path("insert")
@Path("load")
@Consumes(MediaType.APPLICATION_JSON)
public void insertStore(String data){
repo.writeStore(gson.fromJson(data, Shop.class));
public void loadShop(String data){
repo.loadShop(gson.fromJson(data, Shop.class));
}

//retrieve memory store
@GET
@Path("read")
@Produces(MediaType.APPLICATION_JSON)
public String readStore() {
return gson.toJson(repo.readStore());
}

@POST
@Path("remove")
@Consumes(MediaType.APPLICATION_JSON)
public void removeShop(String data){
repo.remove(data);
public String readShop() {
return gson.toJson(repo.readShop());
}
}
@@ -54,7 +54,7 @@ public void updateFridge(String model, int quantity){
public String toString() {
String allfridges="";
for(Fridge f : fridges)
allfridges += f.getBrand() + "-" + f.getModel() + "-" + f.getQuantity() + "\n";
return this.name + "-" + this.cnpj + "-" + this.template + "\n" + allfridges + "\n\n";
allfridges += f.toString() + "\n";
return this.name + "-" + this.cnpj + "-" + this.template + "\n" +"fridges: "+ allfridges + "\n";
}
}
@@ -24,11 +24,11 @@ public List<Shop> read() {
return shopList;
}

public void writeStore(Shop s){
public void loadShop(Shop s){
shop = s;
}

public Shop readStore(){
public Shop readShop(){
return shop;
}