Commit 04b1b432 authored by Ali Saeed's avatar Ali Saeed

integrate invoicing-service with external api to generate invoices

parent 769afc0b
......@@ -28,6 +28,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -2,12 +2,11 @@ package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class InvoicingServiceApplication {
public static void main(String[] args) {
SpringApplication.run(InvoicingServiceApplication.class, args);
}
}
package org.example.resources;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.example.models.Invoice;
import org.springframework.web.bind.annotation.PathVariable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ExternalAPI {
public static Invoice getInvoiceDetails(String invoiceId) throws IOException {
String prompt = "{\n" +
" \"prompt\": \"generate just one random json object contains these attributes: value, status, description, userID, invoiceID. where status is false and value is double and invoiceId is " + invoiceId + ", please just return this object without any words else\"\n" +
"}";
String response = sendRequestToAPI(prompt);
String jsonString = "";
int firstIndex = response.toString().indexOf("{");
int endIndex = response.toString().indexOf("}");
jsonString = response.toString().substring(firstIndex, endIndex + 1);
Gson gson = new Gson();
Invoice invoice = gson.fromJson(jsonString, Invoice.class);
System.out.println(invoice.toString());
return invoice;
}
static public List<Invoice> getUnPaidInvoicesForSpecificUser(String userId){
String prompt = "{\n" +
" \"prompt\": \"generate list of random json objects each object contains these attributes: value, status, description, userID, invoiceID. where status is false and value is double and invoiceId is unique and userId is " + userId+ ", please just return this list of objects without any words else\"\n" +
"}";
ArrayList<Invoice> invoices = new ArrayList<Invoice>();
String response = null;
try {
response = sendRequestToAPI(prompt);
} catch (IOException e) {
throw new RuntimeException(e);
}
String jsonString = "";
int firstIndex = response.toString().indexOf("[");
int endIndex = response.toString().indexOf("]");
jsonString = response.toString().substring(firstIndex, endIndex + 1);
Type listType = new TypeToken<List<Invoice>>() {}.getType();
Gson gson = new Gson();
invoices = gson.fromJson(jsonString, listType);
return invoices;
}
static public List<Invoice> getUnPaidInvoices(){
String prompt = "{\n" +
" \"prompt\": \"generate list of random json objects each object contains these attributes: value, status, description, userID, invoiceID. where status is false and value is double and invoiceId is unique, please just return this list of objects without any words else\"\n" +
"}";
ArrayList<Invoice> invoices = new ArrayList<Invoice>();
String response = null;
try {
response = sendRequestToAPI(prompt);
} catch (IOException e) {
throw new RuntimeException(e);
}
String jsonString = "";
int firstIndex = response.toString().indexOf("[");
int endIndex = response.toString().indexOf("]");
jsonString = response.toString().substring(firstIndex, endIndex + 1);
Type listType = new TypeToken<List<Invoice>>() {}.getType();
Gson gson = new Gson();
invoices = gson.fromJson(jsonString, listType);
return invoices;
}
static public List<Invoice> getPaidInvoices(){
String prompt = "{\n" +
" \"prompt\": \"generate list of random json objects each object contains these attributes: value, status, description, userID, invoiceID. where status is true and value is double and invoiceId is unique, please just return this list of objects without any words else\"\n" +
"}";
ArrayList<Invoice> invoices = new ArrayList<Invoice>();
String response = null;
try {
response = sendRequestToAPI(prompt);
} catch (IOException e) {
throw new RuntimeException(e);
}
String jsonString = "";
int firstIndex = response.toString().indexOf("[");
int endIndex = response.toString().indexOf("]");
jsonString = response.toString().substring(firstIndex, endIndex + 1);
Type listType = new TypeToken<List<Invoice>>() {}.getType();
Gson gson = new Gson();
invoices = gson.fromJson(jsonString, listType);
return invoices;
}
public static String sendRequestToAPI(String prompt) throws IOException{
URL url = new URL("https://fumes-api.onrender.com/llama3");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
String requestBody = prompt;
try (OutputStream outputStream = connection.getOutputStream()) {
byte[] input = requestBody.getBytes("utf-8");
outputStream.write(input, 0, input.length);
}
StringBuilder response = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) {
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
}
catch (IOException e){
System.out.println(e.getMessage());
}
connection.disconnect();
return response.toString();
}
}
package org.example.resources;
import org.example.models.Invoice;
import org.example.models.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@RestController
......@@ -15,42 +12,37 @@ import java.util.List;
public class InvoiceResource {
@GetMapping("/getInvoiceDetails/{invoiceId}")
public Invoice getInvoiceDetails(@PathVariable("invoiceId") String invoiceId){
Invoice invoice = new Invoice("2", "1","dddddddddd",false,300.0);
Invoice invoice = null;
try {
invoice = ExternalAPI.getInvoiceDetails(invoiceId);
} catch (IOException e){
throw new RuntimeException(e);
}
return invoice;
}
@GetMapping("/getUnPaidInvoicesForSpecificUser/{userId}")
public List<Invoice> getUnPaidInvoicesForSpecificUser(@PathVariable("userId") String userId){
Invoice inv1 = new Invoice("1", "1","ssssssd",false,55.0);
Invoice inv2 = new Invoice("2", "1","dddddddddd",false,545.0);
ArrayList<Invoice> inv = new ArrayList<Invoice>();
inv.add(inv1);
inv.add(inv2);
List<Invoice> inv = new ArrayList<>();
inv = ExternalAPI.getUnPaidInvoicesForSpecificUser(userId);
return inv;
}
@PostMapping("/updateInvoiceStatus/{invoiceId}")
public boolean updateInvoiceStatus(@PathVariable("invoiceId") String invoiceId){
Invoice invoice = new Invoice("2", "1","dddddddddd",false,545.0);
invoice.setStatus(true);
/* here some logic like get invoice from database and set the status to true */
return true;
}
@GetMapping("/getUnPaidInvoices")
public List<Invoice> getUnPaidInvoices(){
Invoice invoice1 = new Invoice("2", "1","dddddddddd",false,545.0);
Invoice invoice2 = new Invoice("3", "2","dddddddddd",false,545.0);
ArrayList<Invoice> inv = new ArrayList<Invoice>();
inv.add(invoice1);
inv.add(invoice2);
return inv;
List<Invoice> invoices = new ArrayList<Invoice>();
invoices = ExternalAPI.getUnPaidInvoices();
return invoices;
}
@GetMapping("/getPaidInvoices")
public List<Invoice> getPaidInvoices(){
Invoice invoice1 = new Invoice("2", "1","dddddddddd",true,545.0);
Invoice invoice2 = new Invoice("3", "2","dddddddddd",true,545.0);
ArrayList<Invoice> inv = new ArrayList<Invoice>();
inv.add(invoice1);
inv.add(invoice2);
return inv;
List<Invoice> invoices = new ArrayList<Invoice>();
invoices = ExternalAPI.getPaidInvoices();
return invoices;
}
}
......@@ -18,25 +18,15 @@ public class UserResource {
@GetMapping("/getUnPaidInvoices/{userId}")
public List<Invoice> getUnPaidInvoices(@PathVariable("userId") String userId){
// Invoice inv1 = new Invoice("1", "1","ssssssd",false,55.0);
// Invoice inv2 = new Invoice("2", "1","dddddddddd",false,545.0);
//
// System.out.println(userId);
//
// ArrayList<Invoice> inv = new ArrayList<Invoice>();
// inv.add(inv1);
// inv.add(inv2);
List<Invoice> invoices = Arrays.asList(restTemplate.getForObject("http://localhost:8081/invoices/getUnPaidInvoicesForSpecificUser/" + userId, Invoice[].class));
return invoices;
}
@PostMapping("/payInvoice/{userId}/{invoiceId}")
public boolean payInvoice(@PathVariable("userId") String userId,@PathVariable("invoiceId") String invoiceId){
User user = new User("1","Ali",500.0);
// Invoice invoice = new Invoice("2", "1","dddddddddd",false,545.0);
User user = new User(userId,"Ali",5000.0);
Invoice invoice = restTemplate.getForObject("http://localhost:8081/invoices/getInvoiceDetails/" + invoiceId, Invoice.class);
if(user.getBalance() >= invoice.getValue()){
user.setBalance(user.getBalance() - invoice.getValue());
// boolean status = restTemplate.postForObject("http://localhost:8081/invoices/updateInvoiceStatus/" + invoiceId, boolean.class);
String url = "http://localhost:8081/invoices/updateInvoiceStatus/" + invoiceId;
ResponseEntity<Boolean> response = restTemplate.exchange(url, HttpMethod.POST, null, Boolean.class);
boolean status = response.getBody();
......@@ -49,13 +39,6 @@ public class UserResource {
}
@GetMapping("/getAllUsersHaveUnPaidInvoices")
public HashMap<User,Invoice> getAllUsersHaveUnPaidInvoices(){
// User user = new User("1","Ali",500.0);
// User user2 = new User("2","Hasan",5000.0);
// Invoice invoice1 = new Invoice("2", "1","dddddddddd",false,545.0);
// Invoice invoice2 = new Invoice("3", "2","dddddddddd",false,545.0);
// ArrayList<Invoice> inv = new ArrayList<Invoice>();
// inv.add(invoice1);
// inv.add(invoice2);
List<Invoice> invoices = Arrays.asList(restTemplate.getForObject("http://localhost:8081/invoices/getUnPaidInvoices", Invoice[].class));
HashMap<User,Invoice> userInvoiceHashMap = new HashMap<>();
for (Invoice invoice: invoices)
......@@ -72,13 +55,7 @@ public class UserResource {
}
@GetMapping("/getAllUsersHavePaidInvoices")
public HashMap<User,Invoice> getAllUsersHavePaidInvoices(){
// User user = new User("1","Ali",500.0);
// User user2 = new User("2","Hasan",5000.0);
// Invoice invoice1 = new Invoice("2", "1","dddddddddd",true,545.0);
// Invoice invoice2 = new Invoice("3", "2","dddddddddd",true,545.0);
// ArrayList<Invoice> inv = new ArrayList<Invoice>();
// inv.add(invoice1);
// inv.add(invoice2);
HashMap<User,Invoice> userInvoiceHashMap = new HashMap<>();
List<Invoice> invoices = Arrays.asList(restTemplate.getForObject("http://localhost:8081/invoices/getPaidInvoices", Invoice[].class));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment