Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/main/java/org/hsbc/exception/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.hsbc.exception;

import java.time.LocalDateTime;

public class ErrorResponse {
private LocalDateTime timestamp;
private int status;
private String error;
private String message;
private String path;

public ErrorResponse() {
}

public ErrorResponse(int status, String error, String message, String path) {
this.timestamp = LocalDateTime.now();
this.status = status;
this.error = error;
this.message = message;
this.path = path;
}

public LocalDateTime getTimestamp() {
return timestamp;
}

public void setTimestamp(LocalDateTime timestamp) {
this.timestamp = timestamp;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public String getError() {
return error;
}

public void setError(String error) {
this.error = error;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}
}
64 changes: 64 additions & 0 deletions src/main/java/org/hsbc/exception/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.hsbc.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ErrorResponse> handleResourceNotFoundException(ResourceNotFoundException ex,
WebRequest request) {
ErrorResponse errorResponse = new ErrorResponse(
HttpStatus.NOT_FOUND.value(),
HttpStatus.NOT_FOUND.getReasonPhrase(),
ex.getMessage(),
request.getDescription(false).replace("uri=", ""));
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}

@ExceptionHandler(InsufficientBalanceException.class)
public ResponseEntity<ErrorResponse> handleInsufficientBalanceException(InsufficientBalanceException ex,
WebRequest request) {
ErrorResponse errorResponse = new ErrorResponse(
HttpStatus.BAD_REQUEST.value(),
HttpStatus.BAD_REQUEST.getReasonPhrase(),
ex.getMessage(),
request.getDescription(false).replace("uri=", ""));
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(InvalidPmsIdException.class)
public ResponseEntity<ErrorResponse> handleInvalidPmsIdException(InvalidPmsIdException ex, WebRequest request) {
ErrorResponse errorResponse = new ErrorResponse(
HttpStatus.NOT_FOUND.value(),
HttpStatus.NOT_FOUND.getReasonPhrase(),
ex.getMessage(),
request.getDescription(false).replace("uri=", ""));
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}

@ExceptionHandler(InvalidTransactionIdException.class)
public ResponseEntity<ErrorResponse> handleInvalidTransactionIdException(InvalidTransactionIdException ex,
WebRequest request) {
ErrorResponse errorResponse = new ErrorResponse(
HttpStatus.NOT_FOUND.value(),
HttpStatus.NOT_FOUND.getReasonPhrase(),
ex.getMessage(),
request.getDescription(false).replace("uri=", ""));
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleGlobalException(Exception ex, WebRequest request) {
ErrorResponse errorResponse = new ErrorResponse(
HttpStatus.INTERNAL_SERVER_ERROR.value(),
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),
ex.getMessage(),
request.getDescription(false).replace("uri=", ""));
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.hsbc.exception;

public class InsufficientBalanceException extends RuntimeException {
public InsufficientBalanceException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.hsbc.exception;

public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) {
super(message);
}
}
83 changes: 38 additions & 45 deletions src/main/java/org/hsbc/service/PmsServiceimp.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
package org.hsbc.service;


//Import statemnts
import org.hsbc.entity.PmsEntity;
import org.hsbc.exception.InvalidPmsIdException;
import org.hsbc.repo.PmsRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;


import java.time.LocalDate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

@Service
public class PmsServiceimp implements PmsService {
private static final Logger log =
LoggerFactory.getLogger(PmsServiceimp.class);
@Autowired
private PmsRepository repository;
@Autowired
private WalletService walletService;

// 1️⃣ Add Asset
@Override
public PmsEntity addAsset(PmsEntity asset) {
// Calculate total cost
double totalCost = asset.getBuyPrice() * asset.getQuantity();
// Check and deduct from wallet (this will throw exception if insufficient balance)
walletService.deductMoney(totalCost);
// Set purchase date and buying value
asset.setPurchaseDate(LocalDate.now());
asset.setBuyingValue(totalCost);
return repository.save(asset);
}
private static final Logger log = LoggerFactory.getLogger(PmsServiceimp.class);
@Autowired
private PmsRepository repository;

@Autowired
private WalletService walletService;

// 1️⃣ Add Asset
@Override
public PmsEntity addAsset(PmsEntity asset) {
// Calculate total cost
double totalCost = asset.getBuyPrice() * asset.getQuantity();

// Check and deduct from wallet (this will throw exception if insufficient
// balance)
walletService.deductMoney(totalCost);

// Set purchase date and buying value
asset.setPurchaseDate(LocalDate.now());
asset.setBuyingValue(totalCost);

return repository.save(asset);
}

// 2️⃣ Remove Asset
@Override
Expand Down Expand Up @@ -80,7 +76,8 @@ public double calculatePLPercentage(Long id) throws InvalidPmsIdException {
double buyingValue = asset.getBuyPrice() * asset.getQuantity();
double pl = calculatePL(id);

if (buyingValue == 0) return 0;
if (buyingValue == 0)
return 0;

return (pl / buyingValue) * 100;
}
Expand All @@ -93,6 +90,7 @@ public double getTotalPortfolioValue() {
.mapToDouble(a -> a.getCurrentPrice() * a.getQuantity())
.sum();
}

public PmsServiceimp(PmsRepository repository) {
this.repository = repository;
}
Expand All @@ -101,6 +99,7 @@ public PmsServiceimp(PmsRepository repository) {
public List<PmsEntity> getAllAssets() {
return repository.findAll();
}

@Override
public PmsEntity getAssetById(Long id) throws InvalidPmsIdException {
Optional<PmsEntity> optAsset = repository.findById(id);
Expand All @@ -120,22 +119,16 @@ public PmsEntity updateCurrentPrice(String symbol, double newPrice) {
return repository.save(asset);
}
}
throw new ResponseStatusException(
HttpStatus.NOT_FOUND,
"Asset not found with symbol " + symbol
);
throw new org.hsbc.exception.ResourceNotFoundException("Asset not found with symbol " + symbol);
}
//
// public PmsEntity findAllPms(long id) throws InvalidException {
// Optional<PmsEntity> optProduct = repository.findById(id);
// PmsEntity pmsEntity = optProduct.orElseThrow(
// ()->new InvalidException("Id is not valid: " + id)
// );
//
// return pmsEntity;
// }



//
// public PmsEntity findAllPms(long id) throws InvalidException {
// Optional<PmsEntity> optProduct = repository.findById(id);
// PmsEntity pmsEntity = optProduct.orElseThrow(
// ()->new InvalidException("Id is not valid: " + id)
// );
//
// return pmsEntity;
// }

}
15 changes: 5 additions & 10 deletions src/main/java/org/hsbc/service/WalletServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
import org.hsbc.repo.WalletRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

@Service
public class WalletServiceImpl implements WalletService{
public class WalletServiceImpl implements WalletService {

private static final Logger log =
LoggerFactory.getLogger(WalletServiceImpl.class);
private static final Logger log = LoggerFactory.getLogger(WalletServiceImpl.class);

private final WalletRepository repository;

Expand All @@ -22,8 +19,7 @@ public WalletServiceImpl(WalletRepository repository) {

private WalletEntity getWallet() {
return repository.findById(1L)
.orElseThrow(() -> new ResponseStatusException(
HttpStatus.NOT_FOUND, "Wallet not found"));
.orElseThrow(() -> new org.hsbc.exception.ResourceNotFoundException("Wallet not found"));
}

@Override
Expand All @@ -44,15 +40,14 @@ public double deductMoney(double amount) {
WalletEntity wallet = getWallet();

if (wallet.getBalance() < amount) {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST, "Insufficient balance");
throw new org.hsbc.exception.InsufficientBalanceException("Insufficient balance");
}

wallet.setBalance(wallet.getBalance() - amount);
repository.save(wallet);
return wallet.getBalance();
}

@Override
public WalletEntity getWalletSummary() {
return getWallet();
Expand Down
Loading
Loading