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 {
}
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" +
" \"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>();
String response = null;
......@@ -72,7 +72,7 @@ public class ExternalAPI {
}
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" +
" \"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>();
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 @@
<spring-cloud.version>2022.0.0</spring-cloud.version>
</properties>
<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>
<groupId>org.springframework.boot</groupId>
......
......@@ -20,23 +20,18 @@ public class User {
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
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;
}
......
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;
import org.example.models.Invoice;
import org.example.models.User;
import org.example.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
......@@ -13,29 +14,41 @@ import java.util.*;
@RestController
@RequestMapping("/users")
public class UserResource {
UserRepository userRepository = new UserRepository();
@Autowired
RestTemplate restTemplate;
@GetMapping("/getUnPaidInvoices/{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 null;
}
@PostMapping("/payInvoice/{userId}/{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);
if(user.getBalance() >= invoice.getValue()){
user.setBalance(user.getBalance() - invoice.getValue());
String url = "http://invoicing-service/invoices/updateInvoiceStatus/" + invoiceId;
ResponseEntity<Boolean> response = restTemplate.exchange(url, HttpMethod.POST, null, Boolean.class);
boolean status = response.getBody();
userRepository.updateUser(user);
return status;
}
else{
System.out.println("you do not have enough balance!!!");
return false;
}
}
else {
System.out.println("user does not exist!!");
return false;
}
}
@GetMapping("/getAllUsersHaveUnPaidInvoices")
public HashMap<User,Invoice> getAllUsersHaveUnPaidInvoices(){
......@@ -44,29 +57,28 @@ public class UserResource {
for (Invoice invoice: invoices)
{
System.out.println(invoice.toString());
User user = new User();
user.setBalance(500.0);
user.setName("Ali"+invoice.getUserID());
user.setUserId(invoice.getUserID());
// user.setBalance(500.0);
// user.setName("Ali"+invoice.getUserID());
// user.setUserId(invoice.getUserID());
user = userRepository.getUser(invoice.getUserID());
userInvoiceHashMap.put(user,invoice);
}
return userInvoiceHashMap;
}
@GetMapping("/getAllUsersHavePaidInvoices")
public HashMap<User,Invoice> getAllUsersHavePaidInvoices(){
HashMap<User,Invoice> userInvoiceHashMap = new HashMap<>();
List<Invoice> invoices = Arrays.asList(restTemplate.getForObject("http://invoicing-service/invoices/getPaidInvoices", Invoice[].class));
for (Invoice invoice: invoices)
{
System.out.println(invoice.toString());
User user = new User();
user.setBalance(500.0);
user.setName("Ali"+invoice.getUserID());
user.setUserId(invoice.getUserID());
// user.setBalance(500.0);
// user.setName("Ali"+invoice.getUserID());
// user.setUserId(invoice.getUserID());
user = userRepository.getUser(invoice.getUserID());
userInvoiceHashMap.put(user,invoice);
}
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