-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ALL api services done except collection types
Added Base model that every model class has id
- Loading branch information
Showing
17 changed files
with
683 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package dk.via.Controller.API; | ||
|
||
import dk.via.Model.BaseModel; | ||
import dk.via.Model.SmallModels; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
import java.io.IOException; | ||
|
||
public abstract class APIService { | ||
|
||
OkHttpClient client = new OkHttpClient(); | ||
Request request; | ||
|
||
APIService() { | ||
client = new OkHttpClient(); | ||
} | ||
|
||
void deleteObject(String type, BaseModel object) { | ||
String url = SmallModels.BASE_URL + "type" + object.getId(); | ||
|
||
try { | ||
request = new Request.Builder() | ||
.url(url) | ||
.delete() | ||
.build(); | ||
Response response = client.newCall(request).execute(); | ||
System.out.println(response.body().string()); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package dk.via.Controller.API; | ||
|
||
import com.google.gson.Gson; | ||
import dk.via.Model.Car; | ||
import dk.via.Model.SmallModels; | ||
import okhttp3.FormBody; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import okhttp3.Response; | ||
|
||
import java.io.IOException; | ||
|
||
public class CarsAPIService extends APIService { | ||
|
||
// GET api/Cars | ||
public Car[] getAllCars() { | ||
Car[] cars = null; | ||
|
||
try { | ||
String url = SmallModels.BASE_URL + "Cars"; | ||
|
||
request = new Request.Builder().url(url).build(); | ||
|
||
Response responses = null; | ||
|
||
try { | ||
responses = client.newCall(request).execute(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
String jsonData = responses.body().string(); | ||
|
||
Gson gson = new Gson(); | ||
gson.toJson(jsonData); | ||
|
||
cars = gson.fromJson(jsonData, Car[].class); | ||
|
||
for (Car car : cars) { | ||
System.out.println(car.getVIN()); | ||
} | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return cars; | ||
} | ||
|
||
// GET api/Cars/{id} | ||
public Car getCar(int id) { | ||
Car car = null; | ||
try { | ||
String url = SmallModels.BASE_URL + "Cars/" + id; | ||
request = new Request.Builder().url(url).build(); | ||
Response responses = null; | ||
|
||
try { | ||
responses = client.newCall(request).execute(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
String jsonData = responses.body().string(); | ||
Gson gson = new Gson(); | ||
gson.toJson(jsonData); | ||
car = gson.fromJson(jsonData, Car[].class)[0]; | ||
car.toString(); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return car; | ||
} | ||
|
||
// PUT api/Cars/{id} | ||
public void updateCar(Car car) { | ||
//TODO treba pozriet to ID ci je to VIN cislo alebo id | ||
String url = SmallModels.BASE_URL + "Cars/" + car.getVIN(); | ||
try { | ||
RequestBody formBody = new FormBody.Builder() | ||
.add("VIN", car.getVIN()) | ||
.add("Model", car.getModel()) | ||
.add("Weight", String.valueOf(car.getWeight())) | ||
.build(); | ||
|
||
request = new Request.Builder() | ||
.url(url) | ||
.put(formBody) | ||
.build(); | ||
|
||
Response response = client.newCall(request).execute(); | ||
System.out.println(response.body().string()); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
// POST api/Cars VIN:string, Model:string, Weight: decimal number, Parts:Collection of Part | ||
public void addCar(Car car) { | ||
//TODO treba dorobit parts | ||
String url = SmallModels.BASE_URL + "Cars/"; | ||
try { | ||
RequestBody formBody = new FormBody.Builder() | ||
.add("VIN", car.getVIN()) | ||
.add("Model", car.getModel()) | ||
.add("Weight", String.valueOf(car.getWeight())) | ||
.build(); | ||
|
||
request = new Request.Builder() | ||
.url(url) | ||
.post(formBody) | ||
.build(); | ||
|
||
Response response = client.newCall(request).execute(); | ||
System.out.println(response.body().string()); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
// DELETE api/Cars/{id} | ||
public void deleteCar(Car car) { | ||
super.deleteObject("Cars", car); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package dk.via.Controller.API; | ||
|
||
import com.google.gson.Gson; | ||
import dk.via.Model.Package; | ||
import dk.via.Model.SmallModels; | ||
import okhttp3.FormBody; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import okhttp3.Response; | ||
|
||
import java.io.IOException; | ||
|
||
public class PackagesAPIService extends APIService { | ||
|
||
// GET api/Packages | ||
public Package[] getAllPackages() { | ||
Package[] packages = null; | ||
|
||
try { | ||
String url = SmallModels.BASE_URL + "Packages"; | ||
request = new Request.Builder().url(url).build(); | ||
Response responses = null; | ||
|
||
try { | ||
responses = client.newCall(request).execute(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
String jsonData = responses.body().string(); | ||
Gson gson = new Gson(); | ||
gson.toJson(jsonData); | ||
|
||
packages = gson.fromJson(jsonData, Package[].class); | ||
|
||
for (Package pack : packages) { | ||
System.out.println(pack.toString()); | ||
|
||
} | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return packages; | ||
} | ||
|
||
|
||
// GET api/Packages/{id} | ||
public Package getPackage(int id) { | ||
Package pack = null; | ||
try { | ||
String url = SmallModels.BASE_URL + "Packages/" + id; | ||
request = new Request.Builder().url(url).build(); | ||
Response responses = null; | ||
|
||
try { | ||
responses = client.newCall(request).execute(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
String jsonData = responses.body().string(); | ||
Gson gson = new Gson(); | ||
gson.toJson(jsonData); | ||
pack = gson.fromJson(jsonData, Package[].class)[0]; | ||
pack.toString(); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return pack; | ||
} | ||
|
||
// PUT api/Packages/{id} | ||
public void updatePackage(Package pack) { | ||
//TODO treba spravit parts | ||
String url = SmallModels.BASE_URL + "Packages/" + pack.getId(); | ||
try { | ||
RequestBody formBody = new FormBody.Builder() | ||
.add("Repacking", String.valueOf(pack.getRepacking())) | ||
.build(); | ||
|
||
request = new Request.Builder() | ||
.url(url) | ||
.put(formBody) | ||
.build(); | ||
|
||
Response response = client.newCall(request).execute(); | ||
System.out.println(response.body().string()); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
// POST api/Packages Id:Integer, Repacking:Bool, Parts: Collection of Part | ||
public void addPackage(Package pack) { | ||
//TODO treba dorobit parts a id | ||
String url = SmallModels.BASE_URL + "Packages/"; | ||
try { | ||
RequestBody formBody = new FormBody.Builder() | ||
.add("Repacking", String.valueOf(pack.getRepacking())) | ||
.build(); | ||
|
||
request = new Request.Builder() | ||
.url(url) | ||
.post(formBody) | ||
.build(); | ||
|
||
Response response = client.newCall(request).execute(); | ||
System.out.println(response.body().string()); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
// DELETE api/Packages/{id} | ||
public void deletePackage(Package pack) { | ||
super.deleteObject("Packages", pack); | ||
} | ||
|
||
} |
Oops, something went wrong.