Commit 4c44e3c1 authored by Jonathan Cavia's avatar Jonathan Cavia

done

parent 5a7be832
package com.example.test1;
public class InteresDTO {
public double interes;
}
......@@ -3,9 +3,10 @@ package com.example.test1;
public class Saldo {
public double saldo;
Saldo (){}
Saldo (){this.saldo = 0;}
Saldo(double saldo){
this.saldo = saldo;
......@@ -14,5 +15,6 @@ public class Saldo {
double getSaldo(){
return saldo;
}
}
package com.example.test1;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SaldoController {
private final TransaccionService transaccionService;
SaldoController(TransaccionService transaccionService){
this.transaccionService = transaccionService;
}
@GetMapping("/hello")
public String sayHello(@RequestParam(value = "myName", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
@GetMapping("/saldo")
public Saldo returnSaldo() {
return transaccionService.getSaldoActual();
}
}
package com.example.test1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@SpringBootApplication
@RestController
public class TransaccionController {
......@@ -15,14 +14,14 @@ public class TransaccionController {
this.transaccionService = transaccionService;
}
@GetMapping("/hello")
public String sayHello(@RequestParam(value = "myName", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
@GetMapping("/transacciones")
List<TransaccionModel> obtenerTransacciones() {
return transaccionService.obtenerTransacciones();
}
@GetMapping("/saldo")
public Saldo returnSaldo() {
return new Saldo(1000);
@GetMapping("/extracciones")
List<TransaccionModel> obtenerExtracciones() {
return transaccionService.obtenerExtracciones();
}
@PostMapping("/deposito")
......@@ -35,7 +34,10 @@ public class TransaccionController {
return transaccionService.nuevaExtraccion(nuevaExtraccion);
}
// @PostMapping("/interes")
@PostMapping("/interes")
TransaccionModel nuevaExtraccion(@RequestBody InteresDTO extraccionDTO) {
return transaccionService.nuevoInteres(extraccionDTO.interes);
}
......
......@@ -10,7 +10,7 @@ public class TransaccionModel {
private @Id
@GeneratedValue Long id;
public double monto;
public TRANSACTION_TYPE ttype;
private TRANSACTION_TYPE ttype;
public TransaccionModel(double monto){
this.monto = monto;
......@@ -20,6 +20,10 @@ public class TransaccionModel {
}
public void setType(TRANSACTION_TYPE ttype) {
this.ttype = ttype;
}
public Long getId() {
return this.id;
}
......
......@@ -3,7 +3,12 @@ package com.example.test1;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface TransaccionRepository extends CrudRepository<TransaccionModel, Long> {
List<TransaccionModel> findTransaccionesByTtype(TransaccionModel.TRANSACTION_TYPE type);
List<TransaccionModel> findAll();
List<TransaccionModel> findAllByTtype(TransaccionModel.TRANSACTION_TYPE type);
}
\ No newline at end of file
package com.example.test1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TransaccionService {
......@@ -12,15 +13,41 @@ public class TransaccionService {
this.transaccionRepository = transaccionRepository;
}
public TransaccionModel nuevoDeposito(TransaccionModel transaccion){
transaccion.ttype = TransaccionModel.TRANSACTION_TYPE.DEPOSITO;
transaccion.setType(TransaccionModel.TRANSACTION_TYPE.DEPOSITO) ;
return transaccionRepository.save(transaccion);
}
public TransaccionModel nuevaExtraccion(TransaccionModel transaccion){
transaccion.monto *= -1;
transaccion.ttype = TransaccionModel.TRANSACTION_TYPE.EXTRACCION;
transaccion.setType(TransaccionModel.TRANSACTION_TYPE.EXTRACCION);
return transaccionRepository.save(transaccion);
}
public TransaccionModel nuevoInteres(double porcentaje){
TransaccionModel transaccion = new TransaccionModel();
transaccion.setType(TransaccionModel.TRANSACTION_TYPE.INTERES) ;
transaccion.monto = this.getSaldoActual().saldo * porcentaje / 100;
return transaccionRepository.save(transaccion);
}
public Saldo getSaldoActual() {
List<TransaccionModel> transacciones = this.obtenerTransacciones();
Saldo saldo = new Saldo();
for(TransaccionModel transaccion : transacciones) {
saldo.saldo += transaccion.monto;
}
return saldo;
}
public List<TransaccionModel> obtenerTransacciones() {
return transaccionRepository.findAll();
}
public List<TransaccionModel> obtenerExtracciones() {
return transaccionRepository.findAllByTtype(TransaccionModel.TRANSACTION_TYPE.EXTRACCION);
}
}
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