Commit 36f4d83a authored by Ali Saeed's avatar Ali Saeed

integrate users-service with redis-database contains information about users

parent 9810ca5f
...@@ -56,7 +56,7 @@ public class ExternalAPI { ...@@ -56,7 +56,7 @@ public class ExternalAPI {
} }
static public List<Invoice> getUnPaidInvoices(){ static public List<Invoice> getUnPaidInvoices(){
String prompt = "{\n" + 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" + " \"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 between 1 to 4, please just return this list of objects without any words else\"\n" +
"}"; "}";
ArrayList<Invoice> invoices = new ArrayList<Invoice>(); ArrayList<Invoice> invoices = new ArrayList<Invoice>();
String response = null; String response = null;
...@@ -72,7 +72,7 @@ public class ExternalAPI { ...@@ -72,7 +72,7 @@ public class ExternalAPI {
} }
static public List<Invoice> getPaidInvoices(){ static public List<Invoice> getPaidInvoices(){
String prompt = "{\n" + 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" + " \"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 and userId is between 1 to 4, please just return this list of objects without any words else\"\n" +
"}"; "}";
ArrayList<Invoice> invoices = new ArrayList<Invoice>(); ArrayList<Invoice> invoices = new ArrayList<Invoice>();
String response = null; String response = null;
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -19,6 +19,16 @@ ...@@ -19,6 +19,16 @@
<spring-cloud.version>2022.0.0</spring-cloud.version> <spring-cloud.version>2022.0.0</spring-cloud.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
......
...@@ -20,23 +20,18 @@ public class User { ...@@ -20,23 +20,18 @@ public class User {
public String getUserId() { public String getUserId() {
return userId; return userId;
} }
public void setUserId(String userId) { public void setUserId(String userId) {
this.userId = userId; this.userId = userId;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public double getBalance() { public double getBalance() {
return balance; return balance;
} }
public void setBalance(double balance) { public void setBalance(double balance) {
this.balance = balance; this.balance = balance;
} }
......
package org.example.repositories;
import com.google.gson.Gson;
import org.example.models.User;
import redis.clients.jedis.Jedis;
public class UserRepository {
private Jedis jedis;
private Gson gson;
public UserRepository() {
jedis = new Jedis("localhost", 6379);
gson = new Gson();
}
public boolean exist(String userId){
if (jedis.exists(userId)) {
return true;
}
return false;
}
public void addUser(User user) {
String userJson = convertToJson(user);
jedis.set(user.getUserId(), userJson);
}
public User getUser(String userId) {
String userJson = jedis.get(userId);
return convertFromJson(userJson,userId);
}
public void updateUser(User user) {
if (jedis.exists(user.getUserId())) {
String userJson = convertToJson(user);
jedis.set(user.getUserId(), userJson);
}
}
public void deleteUser(String userId) {
jedis.del(userId);
}
private String convertToJson(User user) {
UserJsonData userJsonData = new UserJsonData();
userJsonData.setName(user.getName());
userJsonData.setBalance(user.getBalance());
return gson.toJson(userJsonData);
}
private User convertFromJson(String userJson,String userId) {
UserJsonData userJsonData = gson.fromJson(userJson, UserJsonData.class);
User user = new User();
user.setUserId(userId);
user.setName(userJsonData.getName());
user.setBalance(userJsonData.getBalance());
return user;
}
private static class UserJsonData {
private String name;
private double balance;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
}
...@@ -2,6 +2,7 @@ package org.example.resources; ...@@ -2,6 +2,7 @@ package org.example.resources;
import org.example.models.Invoice; import org.example.models.Invoice;
import org.example.models.User; import org.example.models.User;
import org.example.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
...@@ -13,29 +14,41 @@ import java.util.*; ...@@ -13,29 +14,41 @@ import java.util.*;
@RestController @RestController
@RequestMapping("/users") @RequestMapping("/users")
public class UserResource { public class UserResource {
UserRepository userRepository = new UserRepository();
@Autowired @Autowired
RestTemplate restTemplate; RestTemplate restTemplate;
@GetMapping("/getUnPaidInvoices/{userId}") @GetMapping("/getUnPaidInvoices/{userId}")
public List<Invoice> getUnPaidInvoices(@PathVariable("userId") String userId){ public List<Invoice> getUnPaidInvoices(@PathVariable("userId") String userId){
List<Invoice> invoices = Arrays.asList(restTemplate.getForObject("http://localhost:8081/invoices/getUnPaidInvoicesForSpecificUser/" + userId, Invoice[].class)); if(userRepository.exist(userId)) {
List<Invoice> invoices = Arrays.asList(restTemplate.getForObject("http://invoicing-service/invoices/getUnPaidInvoicesForSpecificUser/" + userId, Invoice[].class));
return invoices; return invoices;
} }
return null;
}
@PostMapping("/payInvoice/{userId}/{invoiceId}") @PostMapping("/payInvoice/{userId}/{invoiceId}")
public boolean payInvoice(@PathVariable("userId") String userId,@PathVariable("invoiceId") String invoiceId){ public boolean payInvoice(@PathVariable("userId") String userId,@PathVariable("invoiceId") String invoiceId){
User user = new User(userId,"Ali",5000.0); User user = new User();
if(userRepository.exist(userId)){
user = userRepository.getUser(userId);
Invoice invoice = restTemplate.getForObject("http://invoicing-service/invoices/getInvoiceDetails/" + invoiceId, Invoice.class); Invoice invoice = restTemplate.getForObject("http://invoicing-service/invoices/getInvoiceDetails/" + invoiceId, Invoice.class);
if(user.getBalance() >= invoice.getValue()){ if(user.getBalance() >= invoice.getValue()){
user.setBalance(user.getBalance() - invoice.getValue()); user.setBalance(user.getBalance() - invoice.getValue());
String url = "http://invoicing-service/invoices/updateInvoiceStatus/" + invoiceId; String url = "http://invoicing-service/invoices/updateInvoiceStatus/" + invoiceId;
ResponseEntity<Boolean> response = restTemplate.exchange(url, HttpMethod.POST, null, Boolean.class); ResponseEntity<Boolean> response = restTemplate.exchange(url, HttpMethod.POST, null, Boolean.class);
boolean status = response.getBody(); boolean status = response.getBody();
userRepository.updateUser(user);
return status; return status;
} }
else{ else{
System.out.println("you do not have enough balance!!!"); System.out.println("you do not have enough balance!!!");
return false; return false;
} }
}
else {
System.out.println("user does not exist!!");
return false;
}
} }
@GetMapping("/getAllUsersHaveUnPaidInvoices") @GetMapping("/getAllUsersHaveUnPaidInvoices")
public HashMap<User,Invoice> getAllUsersHaveUnPaidInvoices(){ public HashMap<User,Invoice> getAllUsersHaveUnPaidInvoices(){
...@@ -44,29 +57,28 @@ public class UserResource { ...@@ -44,29 +57,28 @@ public class UserResource {
for (Invoice invoice: invoices) for (Invoice invoice: invoices)
{ {
System.out.println(invoice.toString()); System.out.println(invoice.toString());
User user = new User(); User user = new User();
user.setBalance(500.0); // user.setBalance(500.0);
user.setName("Ali"+invoice.getUserID()); // user.setName("Ali"+invoice.getUserID());
user.setUserId(invoice.getUserID()); // user.setUserId(invoice.getUserID());
user = userRepository.getUser(invoice.getUserID());
userInvoiceHashMap.put(user,invoice); userInvoiceHashMap.put(user,invoice);
} }
return userInvoiceHashMap; return userInvoiceHashMap;
} }
@GetMapping("/getAllUsersHavePaidInvoices") @GetMapping("/getAllUsersHavePaidInvoices")
public HashMap<User,Invoice> getAllUsersHavePaidInvoices(){ public HashMap<User,Invoice> getAllUsersHavePaidInvoices(){
HashMap<User,Invoice> userInvoiceHashMap = new HashMap<>(); HashMap<User,Invoice> userInvoiceHashMap = new HashMap<>();
List<Invoice> invoices = Arrays.asList(restTemplate.getForObject("http://invoicing-service/invoices/getPaidInvoices", Invoice[].class)); List<Invoice> invoices = Arrays.asList(restTemplate.getForObject("http://invoicing-service/invoices/getPaidInvoices", Invoice[].class));
for (Invoice invoice: invoices) for (Invoice invoice: invoices)
{ {
System.out.println(invoice.toString()); System.out.println(invoice.toString());
User user = new User(); User user = new User();
user.setBalance(500.0); // user.setBalance(500.0);
user.setName("Ali"+invoice.getUserID()); // user.setName("Ali"+invoice.getUserID());
user.setUserId(invoice.getUserID()); // user.setUserId(invoice.getUserID());
user = userRepository.getUser(invoice.getUserID());
userInvoiceHashMap.put(user,invoice); userInvoiceHashMap.put(user,invoice);
} }
return userInvoiceHashMap; return userInvoiceHashMap;
......
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