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
11 changes: 6 additions & 5 deletions src/main/java/org/hsbc/controller/PmsController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hsbc.controller;

import org.hsbc.entity.PmsEntity;
import org.hsbc.exception.InvalidPmsIdException;
import org.hsbc.service.PmsService;
import org.hsbc.service.PmsServiceimp;
import org.slf4j.Logger;
Expand All @@ -26,7 +27,7 @@ public List<PmsEntity> getAllAssets() {
return service.getAllAssets();
}
@GetMapping("/{id}")
public PmsEntity getAssetById(@PathVariable Long id) {
public PmsEntity getAssetById(@PathVariable Long id) throws InvalidPmsIdException {
return service.getAssetById(id);
}

Expand All @@ -39,25 +40,25 @@ public PmsEntity addAsset(@RequestBody PmsEntity asset) {
}

@DeleteMapping("/remove/{id}")
public String removeAsset(@PathVariable Long id) {
public String removeAsset(@PathVariable Long id) throws InvalidPmsIdException {
service.removeAsset(id);
return "Asset removed successfully";
}

@PutMapping("/update-quantity/{id}")
public PmsEntity updateQuantity(
@PathVariable Long id,
@RequestParam int quantity) {
@RequestParam int quantity) throws InvalidPmsIdException {
return service.updateQuantity(id, quantity);
}

@GetMapping("/pl/{id}")
public double getPL(@PathVariable Long id) {
public double getPL(@PathVariable Long id) throws InvalidPmsIdException {
return service.calculatePL(id);
}

@GetMapping("/pl-percentage/{id}")
public double getPLPercentage(@PathVariable Long id) {
public double getPLPercentage(@PathVariable Long id) throws InvalidPmsIdException {
return service.calculatePLPercentage(id);
}

Expand Down
26 changes: 0 additions & 26 deletions src/main/java/org/hsbc/exception/InvalidException.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/main/java/org/hsbc/exception/InvalidPmsIdException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.hsbc.exception;

public class InvalidPmsIdException extends Exception {

public InvalidPmsIdException() {
}

public InvalidPmsIdException(String message) {
super(message);
}

public InvalidPmsIdException(Throwable cause) {
super(cause);
}

public InvalidPmsIdException(String message, Throwable cause) {
super(message, cause);
}

public InvalidPmsIdException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.hsbc.exception;

public class InvalidTransactionIdException extends Exception {

public InvalidTransactionIdException() {
}

public InvalidTransactionIdException(String message) {
super(message);
}

public InvalidTransactionIdException(Throwable cause) {
super(cause);
}

public InvalidTransactionIdException(String message, Throwable cause) {
super(message, cause);
}

public InvalidTransactionIdException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

}
18 changes: 10 additions & 8 deletions src/main/java/org/hsbc/service/PmsService.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package org.hsbc.service;

import org.hsbc.entity.PmsEntity;

import java.util.List;
import org.hsbc.entity.PmsEntity;
import org.hsbc.exception.InvalidPmsIdException;

public interface PmsService {

PmsEntity addAsset(PmsEntity asset);

void removeAsset(Long id);
void removeAsset(Long id) throws InvalidPmsIdException;

PmsEntity updateQuantity(Long id, int newQuantity);
PmsEntity updateQuantity(Long id, int newQuantity) throws InvalidPmsIdException;

double calculatePL(Long id);
double calculatePL(Long id) throws InvalidPmsIdException;

double calculatePLPercentage(Long id);
double calculatePLPercentage(Long id) throws InvalidPmsIdException;

double getTotalPortfolioValue();

List<PmsEntity> getAllAssets();
PmsEntity getAssetById(Long id);

PmsEntity getAssetById(Long id) throws InvalidPmsIdException;

}
}
110 changes: 53 additions & 57 deletions src/main/java/org/hsbc/service/PmsServiceimp.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package org.hsbc.service;

import org.hsbc.entity.PmsEntity;
import org.hsbc.exception.InvalidException;
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.util.List;
import java.util.Optional;
Expand All @@ -20,65 +18,63 @@ public class PmsServiceimp implements PmsService {
@Autowired
private PmsRepository repository;

// 1️⃣ Add Asset
@Override
public PmsEntity addAsset(PmsEntity asset) {
asset.setBuyingValue(asset.getBuyPrice() * asset.getQuantity());
return repository.save(asset);
}
// 1️⃣ Add Asset
@Override
public PmsEntity addAsset(PmsEntity asset) {
asset.setBuyingValue(asset.getBuyPrice() * asset.getQuantity());
return repository.save(asset);
}

// 2️⃣ Remove Asset
@Override
public void removeAsset(Long id) {
repository.deleteById(id);
}
// 2️⃣ Remove Asset
@Override
public void removeAsset(Long id) throws InvalidPmsIdException {
getAssetById(id);
repository.deleteById(id);
}

// 3️⃣ Update Quantity
@Override
public PmsEntity updateQuantity(Long id, int newQuantity) {
PmsEntity asset = repository.findById(id)
.orElseThrow(() -> new RuntimeException("Asset not found"));
// 3️⃣ Update Quantity
@Override
public PmsEntity updateQuantity(Long id, int newQuantity) throws InvalidPmsIdException {
PmsEntity asset = getAssetById(id);

asset.setQuantity(newQuantity);
asset.setBuyingValue(asset.getBuyPrice() * newQuantity);
asset.setQuantity(newQuantity);
asset.setBuyingValue(asset.getBuyPrice() * newQuantity);

return repository.save(asset);
}
return repository.save(asset);
}

// 4️⃣ Calculate Profit / Loss
@Override
public double calculatePL(Long id) {
PmsEntity asset = repository.findById(id)
.orElseThrow(() -> new RuntimeException("Asset not found"));
// 4️⃣ Calculate Profit / Loss
@Override
public double calculatePL(Long id) throws InvalidPmsIdException {
PmsEntity asset = getAssetById(id);

double buyingValue = asset.getBuyPrice() * asset.getQuantity();
double currentValue = asset.getCurrentPrice() * asset.getQuantity();
double buyingValue = asset.getBuyPrice() * asset.getQuantity();
double currentValue = asset.getCurrentPrice() * asset.getQuantity();

return currentValue - buyingValue;
}
return currentValue - buyingValue;
}

// 5️⃣ Calculate P/L Percentage
@Override
public double calculatePLPercentage(Long id) {
PmsEntity asset = repository.findById(id)
.orElseThrow(() -> new RuntimeException("Asset not found"));
// 5️⃣ Calculate P/L Percentage
@Override
public double calculatePLPercentage(Long id) throws InvalidPmsIdException {
PmsEntity asset = getAssetById(id);

double buyingValue = asset.getBuyPrice() * asset.getQuantity();
double pl = calculatePL(id);
double buyingValue = asset.getBuyPrice() * asset.getQuantity();
double pl = calculatePL(id);

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

return (pl / buyingValue) * 100;
}
return (pl / buyingValue) * 100;
}

// 6️⃣ Total Portfolio Value
@Override
public double getTotalPortfolioValue() {
return repository.findAll()
.stream()
.mapToDouble(a -> a.getCurrentPrice() * a.getQuantity())
.sum();
}
// 6️⃣ Total Portfolio Value
@Override
public double getTotalPortfolioValue() {
return repository.findAll()
.stream()
.mapToDouble(a -> a.getCurrentPrice() * a.getQuantity())
.sum();
}
public PmsServiceimp(PmsRepository repository) {
this.repository = repository;
}
Expand All @@ -88,12 +84,12 @@ public List<PmsEntity> getAllAssets() {
return repository.findAll();
}
@Override
public PmsEntity getAssetById(Long id) {
return repository.findById(id)
.orElseThrow(() -> new ResponseStatusException(
HttpStatus.NOT_FOUND,
"Asset not found with id " + id
));
public PmsEntity getAssetById(Long id) throws InvalidPmsIdException {
Optional<PmsEntity> optAsset = repository.findById(id);
if (optAsset.isEmpty()) {
throw new InvalidPmsIdException("Asset not found with id " + id);
}
return optAsset.get();
}
//
// public PmsEntity findAllPms(long id) throws InvalidException {
Expand All @@ -108,4 +104,4 @@ public PmsEntity getAssetById(Long id) {



}
}
9 changes: 8 additions & 1 deletion src/main/java/org/hsbc/service/TransactionService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hsbc.service;

import org.hsbc.entity.TransactionEntity;
import org.hsbc.exception.InvalidTransactionIdException;

import java.util.List;

Expand All @@ -11,4 +12,10 @@ public interface TransactionService {

List<TransactionEntity> getTransactionsBySymbol(String symbol);

}
TransactionEntity getTransactionById(Long id) throws InvalidTransactionIdException;

TransactionEntity updateTransaction(TransactionEntity transaction) throws InvalidTransactionIdException;

void deleteTransaction(Long id) throws InvalidTransactionIdException;

}
26 changes: 24 additions & 2 deletions src/main/java/org/hsbc/service/TransactionSeviceimp.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.hsbc.service;

import org.hsbc.entity.TransactionEntity;

import org.hsbc.exception.InvalidTransactionIdException;
import org.hsbc.repo.TransactionRepo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class TransactionSeviceimp implements TransactionService {
Expand All @@ -34,4 +35,25 @@ public List<TransactionEntity> getAllTransactions() {
public List<TransactionEntity> getTransactionsBySymbol(String symbol) {
return repository.findBySymbol(symbol);
}
}

@Override
public TransactionEntity getTransactionById(Long id) throws InvalidTransactionIdException {
Optional<TransactionEntity> optTransaction = repository.findById(id);
if (optTransaction.isEmpty()) {
throw new InvalidTransactionIdException("Transaction not found with id " + id);
}
return optTransaction.get();
}

@Override
public TransactionEntity updateTransaction(TransactionEntity transaction) throws InvalidTransactionIdException {
getTransactionById(transaction.getTransactionId()); // Validate existence
return repository.save(transaction);
}

@Override
public void deleteTransaction(Long id) throws InvalidTransactionIdException {
getTransactionById(id); // Validate existence
repository.deleteById(id);
}
}
Loading
Loading