From 9ebaae1298d8bbb5df1b90fe3447db5772784d26 Mon Sep 17 00:00:00 2001
From: Radheshhathwar <radheshhathwar.bh@gmail.com>
Date: Tue, 29 Aug 2023 18:12:28 +0530
Subject: [PATCH] Added fetchAll and fetchBy uniqueRefNum

---
 .../controller/UserController.java            | 13 ++++++++-
 .../userManagement/handler/UserHandler.java   | 12 ++++++--
 .../repository/TransactionRepository.java     |  1 +
 .../userManagement/services/UserService.java  | 29 +++++++++++++++++--
 src/main/resources/application.properties     |  2 +-
 5 files changed, 50 insertions(+), 7 deletions(-)

diff --git a/src/main/java/com/tarento/upsmf/userManagement/controller/UserController.java b/src/main/java/com/tarento/upsmf/userManagement/controller/UserController.java
index 4b5f006..d4dc2c7 100644
--- a/src/main/java/com/tarento/upsmf/userManagement/controller/UserController.java
+++ b/src/main/java/com/tarento/upsmf/userManagement/controller/UserController.java
@@ -2,6 +2,7 @@ package com.tarento.upsmf.userManagement.controller;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.tarento.upsmf.userManagement.handler.UserHandler;
+import com.tarento.upsmf.userManagement.model.Transaction;
 import com.tarento.upsmf.userManagement.services.PaymentService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
@@ -10,6 +11,7 @@ import org.springframework.web.bind.annotation.*;
 
 import java.io.IOException;
 import java.net.URISyntaxException;
+import java.util.List;
 import java.util.Map;
 
 @RestController
@@ -86,5 +88,14 @@ public class UserController {
     public String usrOTP(@RequestBody JsonNode body) throws IOException {
         return userHandler.usrOTP(body);
     }
-  
+
+    @GetMapping(value = "/transaction", produces = "application/json")
+    public ResponseEntity<List<Transaction>> getAllTransactions() {
+        return userHandler.getAllTransactions();
+    }
+
+    @GetMapping(value = "/transaction/{uniqueRefNumber}", produces = "application/json")
+    public ResponseEntity<?> getTransactionByUniqueRefNumber(@PathVariable String uniqueRefNumber) {
+        return userHandler.getTransactionByUniqueRefNumber(uniqueRefNumber);
+    }
 }
diff --git a/src/main/java/com/tarento/upsmf/userManagement/handler/UserHandler.java b/src/main/java/com/tarento/upsmf/userManagement/handler/UserHandler.java
index d842b6b..96571cc 100644
--- a/src/main/java/com/tarento/upsmf/userManagement/handler/UserHandler.java
+++ b/src/main/java/com/tarento/upsmf/userManagement/handler/UserHandler.java
@@ -2,6 +2,7 @@ package com.tarento.upsmf.userManagement.handler;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.tarento.upsmf.userManagement.model.Payment;
+import com.tarento.upsmf.userManagement.model.Transaction;
 import com.tarento.upsmf.userManagement.services.UserService;
 import com.tarento.upsmf.userManagement.utility.*;
 import org.slf4j.Logger;
@@ -12,6 +13,7 @@ import org.springframework.stereotype.Component;
 
 import java.io.IOException;
 import java.net.URISyntaxException;
+import java.util.List;
 import java.util.Map;
 
 @Component
@@ -141,8 +143,14 @@ public class UserHandler {
     }
 
     public String usrOTP(JsonNode body) throws IOException {
-        logger.info("OTP mail to user with body {}",body);
+        logger.info("OTP mail to user with body {}", body);
         return userService.usrOTP(body);
     }
-      
+    public ResponseEntity<List<Transaction>> getAllTransactions() {
+        return userService.getAllTransactions();
+    }
+
+    public ResponseEntity<?> getTransactionByUniqueRefNumber(String uniqueRefNumber) {
+        return userService.getTransactionByUniqueRefNumber(uniqueRefNumber);
+    }
 }
diff --git a/src/main/java/com/tarento/upsmf/userManagement/repository/TransactionRepository.java b/src/main/java/com/tarento/upsmf/userManagement/repository/TransactionRepository.java
index eea1126..f79305e 100644
--- a/src/main/java/com/tarento/upsmf/userManagement/repository/TransactionRepository.java
+++ b/src/main/java/com/tarento/upsmf/userManagement/repository/TransactionRepository.java
@@ -4,4 +4,5 @@ import com.tarento.upsmf.userManagement.model.Transaction;
 import org.springframework.data.jpa.repository.JpaRepository;
 
 public interface TransactionRepository extends JpaRepository<Transaction,Long> {
+    Transaction findByUniqueRefNumber(String uniqueRefNumber);
 }
diff --git a/src/main/java/com/tarento/upsmf/userManagement/services/UserService.java b/src/main/java/com/tarento/upsmf/userManagement/services/UserService.java
index 99314c1..8d15ed6 100644
--- a/src/main/java/com/tarento/upsmf/userManagement/services/UserService.java
+++ b/src/main/java/com/tarento/upsmf/userManagement/services/UserService.java
@@ -1,7 +1,8 @@
 package com.tarento.upsmf.userManagement.services;
 
 import com.fasterxml.jackson.databind.JsonNode;
-import com.tarento.upsmf.userManagement.model.Payment;
+import com.tarento.upsmf.userManagement.model.Transaction;
+import com.tarento.upsmf.userManagement.repository.TransactionRepository;
 import com.tarento.upsmf.userManagement.utility.KeycloakTokenRetriever;
 import com.tarento.upsmf.userManagement.utility.KeycloakUserCredentialPersister;
 import com.tarento.upsmf.userManagement.utility.SunbirdRCKeycloakTokenRetriever;
@@ -20,8 +21,11 @@ import org.springframework.web.util.UriComponentsBuilder;
 
 import javax.annotation.PostConstruct;
 import java.io.IOException;
-import java.net.*;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 
 @Component
 @PropertySource({ "classpath:application.properties" })
@@ -44,6 +48,9 @@ public class UserService {
     @Autowired
     private KeycloakUserCredentialPersister keycloakUserCredentialPersister;
 
+    @Autowired
+    private TransactionRepository transactionRepository;
+
     private static Environment environment;
     private String BASE_URL;
     private String KEYCLOAK_BASEURL;
@@ -197,5 +204,21 @@ public class UserService {
         logger.info("OTP mail to user with body {}",body);
         return keycloakUserCredentialPersister.sendOTPMail(body);
     }
+    public ResponseEntity<List<Transaction>> getAllTransactions() {
+        List<Transaction> result = transactionRepository.findAll();
+        if (result.isEmpty()) {
+            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
+        }
+        return new ResponseEntity<>(result, HttpStatus.OK);
+    }
+
+    public ResponseEntity<Transaction> getTransactionByUniqueRefNumber(String uniqueRefNumber) {
+        Transaction transaction = transactionRepository.findByUniqueRefNumber(uniqueRefNumber);
+        if (transaction == null) {
+            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
+        }
+        return new ResponseEntity<>(transaction, HttpStatus.OK);
+    }
+
 
-}
+}
\ No newline at end of file
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index a31f09e..f787335 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -27,7 +27,7 @@ spring.datasource.password=postgres
 spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
 spring.jpa.properties.hibernate.format_sql=true
 spring.jpa.show-sql=false
-spring.jpa.hibernate.ddl-auto=create
+spring.jpa.hibernate.ddl-auto=none
 
 otp.mail.endpoint = /api/v1/keycloak/mail/sendOTP
 user.create.mail.endpoint =/api/v1/keycloak/mail/userCreate
-- 
GitLab