Commit 769afc0b authored by Ali Saeed's avatar Ali Saeed

test with postman and doing some updates

parent 8daca8dd
......@@ -14,6 +14,13 @@ public class Invoice {
this.status = status;
this.value = value;
}
public Invoice() {
this.invoiceID = "";
this.userID = "";
this.description = "description";
this.status = false;
this.value = 0.0;
}
public String getInvoiceID() {
return invoiceID;
......
......@@ -10,7 +10,11 @@ public class User {
this.name = name;
this.balance = balance;
}
public User() {
this.userId = "";
this.name = "ali";
this.balance = 0.0;
}
public String getUserId() {
return userId;
}
......
......@@ -2,9 +2,9 @@ package org.example.resources;
import org.example.models.Invoice;
import org.example.models.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -13,8 +13,13 @@ import java.util.List;
@RestController
@RequestMapping("/invoices")
public class InvoiceResource {
@GetMapping("/getInvoiceDetails/{invoiceId}")
public Invoice getInvoiceDetails(@PathVariable("invoiceId") String invoiceId){
Invoice invoice = new Invoice("2", "1","dddddddddd",false,300.0);
return invoice;
}
@RequestMapping("/{userId}")
@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);
......@@ -23,14 +28,14 @@ public class InvoiceResource {
inv.add(inv2);
return inv;
}
@RequestMapping("/{invoiceId}")
@PostMapping("/updateInvoiceStatus/{invoiceId}")
public boolean updateInvoiceStatus(@PathVariable("invoiceId") String invoiceId){
Invoice invoice = new Invoice("2", "1","dddddddddd",false,545.0);
invoice.setStatus(true);
return true;
}
@RequestMapping()
@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);
......@@ -39,7 +44,7 @@ public class InvoiceResource {
inv.add(invoice2);
return inv;
}
@RequestMapping()
@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);
......
server.port=8081
\ No newline at end of file
......@@ -2,10 +2,16 @@ package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class UsersServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UsersServiceApplication.class, args);
}
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
......@@ -15,6 +15,14 @@ public class Invoice {
this.value = value;
}
public Invoice() {
this.invoiceID = "";
this.userID = "";
this.description = "description";
this.status = false;
this.value = 0.0;
}
public String getInvoiceID() {
return invoiceID;
}
......
......@@ -11,6 +11,12 @@ public class User {
this.balance = balance;
}
public User() {
this.userId = "";
this.name = "ali";
this.balance = 0.0;
}
public String getUserId() {
return userId;
}
......
......@@ -2,80 +2,96 @@ package org.example.resources;
import org.example.models.Invoice;
import org.example.models.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.*;
@RestController
@RequestMapping("/users")
public class UserResource {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/{userId}")
@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);
return inv;
// 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;
}
@RequestMapping("/{userId}/{invoiceId}")
@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);
// Invoice invoice = new Invoice("2", "1","dddddddddd",false,545.0);
Invoice invoice = restTemplate.getForObject("http://localhost:8081/invoices/getInvoiceDetails/" + invoiceId, Invoice.class);
if(user.getBalance() >= invoice.getValue()){
user.setBalance(user.getBalance() - invoice.getValue());
invoice.setStatus(true);
return true;
// 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();
return status;
}
else{
System.out.println("you do not have enough balance!!!");
return false;
}
}
@RequestMapping()
@GetMapping("/getAllUsersHaveUnPaidInvoices")
public HashMap<User,Invoice> getAllUsersHaveUnPaidInvoices(){
User user1 = 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);
// 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: inv)
for (Invoice invoice: invoices)
{
String id = invoice.getUserID();
System.out.println(invoice.toString());
User user = new User();
user.setBalance(500.0);
user.setName("Ali"+invoice.getUserID());
user.setUserId(invoice.getUserID());
userInvoiceHashMap.put(user,invoice);
}
userInvoiceHashMap.put(user1,invoice1);
userInvoiceHashMap.put(user2,invoice2);
return userInvoiceHashMap;
}
@RequestMapping()
@GetMapping("/getAllUsersHavePaidInvoices")
public HashMap<User,Invoice> getAllUsersHavePaidInvoices(){
User user1 = 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);
// 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<>();
for (Invoice invoice: inv)
List<Invoice> invoices = Arrays.asList(restTemplate.getForObject("http://localhost:8081/invoices/getPaidInvoices", Invoice[].class));
for (Invoice invoice: invoices)
{
String id = invoice.getUserID();
System.out.println(invoice.toString());
User user = new User();
user.setBalance(500.0);
user.setName("Ali"+invoice.getUserID());
user.setUserId(invoice.getUserID());
userInvoiceHashMap.put(user,invoice);
}
userInvoiceHashMap.put(user1,invoice1);
userInvoiceHashMap.put(user2,invoice2);
return userInvoiceHashMap;
}
}
server.port=8080
\ No newline at end of file
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