Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Antonio Rueda
/
UJACoin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
1f5f3cd5
authored
Dec 15, 2020
by
Antonio Rueda
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Primera versión del controlador RESTful
parent
0f58d14c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
151 additions
and
1 deletions
src/main/java/es/ujaen/dae/ujacoin/app/UjaCoinApp.java
src/main/java/es/ujaen/dae/ujacoin/controladoresREST/ControladorREST.java
src/main/java/es/ujaen/dae/ujacoin/app/UjaCoinApp.java
View file @
1f5f3cd5
...
...
@@ -10,7 +10,8 @@ import org.springframework.boot.autoconfigure.domain.EntityScan;
*/
@SpringBootApplication
(
scanBasePackages
={
"es.ujaen.dae.ujacoin.servicios"
,
"es.ujaen.dae.ujacoin.repositorios"
"es.ujaen.dae.ujacoin.repositorios"
,
"es.ujaen.dae.ujacoin.controladoresREST"
})
@EntityScan
(
basePackages
=
"es.ujaen.dae.ujacoin.entidades"
)
public
class
UjaCoinApp
{
...
...
src/main/java/es/ujaen/dae/ujacoin/controladoresREST/ControladorREST.java
0 → 100644
View file @
1f5f3cd5
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package
es
.
ujaen
.
dae
.
ujacoin
.
controladoresREST
;
import
es.ujaen.dae.ujacoin.controladoresREST.DTO.DTOCliente
;
import
es.ujaen.dae.ujacoin.controladoresREST.DTO.DTOCuenta
;
import
es.ujaen.dae.ujacoin.controladoresREST.DTO.DTOMovimiento
;
import
es.ujaen.dae.ujacoin.controladoresREST.DTO.DTOTarjeta
;
import
es.ujaen.dae.ujacoin.entidades.Cliente
;
import
es.ujaen.dae.ujacoin.entidades.Cuenta
;
import
es.ujaen.dae.ujacoin.excepciones.ClienteNoRegistrado
;
import
es.ujaen.dae.ujacoin.excepciones.ClienteYaRegistrado
;
import
es.ujaen.dae.ujacoin.excepciones.TarjetaYaRegistrada
;
import
es.ujaen.dae.ujacoin.servicios.ServicioUjaCoin
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.stream.Collectors
;
import
javax.validation.ConstraintViolationException
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
import
org.springframework.web.bind.annotation.RestController
;
/**
* Controlador REST
* @author ajrueda
*/
@RestController
@RequestMapping
(
"/ujacoin"
)
public
class
ControladorREST
{
@Autowired
ServicioUjaCoin
servicios
;
@ExceptionHandler
(
ConstraintViolationException
.
class
)
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
public
void
handlerViolacionRestricciones
(
ConstraintViolationException
e
)
{
// return ResponseEntity.badRequest().body(e.getMessage());
}
@ExceptionHandler
(
ClienteNoRegistrado
.
class
)
@ResponseStatus
(
HttpStatus
.
NOT_FOUND
)
public
void
handlerClienteNoRegistrado
(
ClienteNoRegistrado
e
)
{
}
@PostMapping
(
"/clientes"
)
ResponseEntity
<
DTOCuenta
>
altaCliente
(
@RequestBody
DTOCliente
cliente
)
{
try
{
Cuenta
cuenta
=
servicios
.
altaCliente
(
cliente
.
aCliente
());
return
ResponseEntity
.
status
(
HttpStatus
.
CREATED
).
body
(
new
DTOCuenta
(
cuenta
));
}
catch
(
ClienteYaRegistrado
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
CONFLICT
).
build
();
}
}
@GetMapping
(
"/clientes/{dni}"
)
ResponseEntity
<
DTOCliente
>
loginCliente
(
@PathVariable
String
dni
,
@RequestParam
String
clave
)
{
Optional
<
Cliente
>
cliente
=
servicios
.
loginCliente
(
dni
,
clave
);
return
cliente
.
map
(
c
->
ResponseEntity
.
ok
(
new
DTOCliente
(
c
)))
.
orElse
(
ResponseEntity
.
notFound
().
build
());
}
@PostMapping
(
"/clientes/{dni}/tarjetas"
)
ResponseEntity
<
Void
>
altaTarjeta
(
@PathVariable
String
dni
,
@RequestBody
DTOTarjeta
tarjeta
)
{
try
{
servicios
.
registrarTarjeta
(
dni
,
tarjeta
.
aTarjeta
());
return
ResponseEntity
.
status
(
HttpStatus
.
CREATED
).
build
();
}
catch
(
TarjetaYaRegistrada
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
CONFLICT
).
build
();
}
}
@GetMapping
(
"/clientes/{dni}/tarjetas"
)
@ResponseStatus
(
HttpStatus
.
OK
)
List
<
DTOTarjeta
>
verTarjetas
(
@PathVariable
String
dni
)
{
return
servicios
.
verTarjetas
(
dni
).
stream
()
.
map
(
t
->
new
DTOTarjeta
(
t
)).
collect
(
Collectors
.
toList
());
}
@GetMapping
(
"/clientes/{dni}/tarjetas/{num}"
)
ResponseEntity
<
DTOTarjeta
>
verTarjeta
(
@PathVariable
String
dni
,
@PathVariable
String
num
)
{
return
servicios
.
verTarjetas
(
dni
).
stream
()
.
filter
(
t
->
t
.
getNum
().
equals
(
num
))
.
findFirst
()
.
map
(
t
->
ResponseEntity
.
ok
(
new
DTOTarjeta
(
t
)))
.
orElse
(
ResponseEntity
.
notFound
().
build
());
}
@PostMapping
(
"/clientes/{dni}/cuentas"
)
@ResponseStatus
(
HttpStatus
.
CREATED
)
DTOCuenta
altaCuenta
(
@PathVariable
String
dni
)
{
return
new
DTOCuenta
(
servicios
.
crearCuenta
(
dni
));
}
@GetMapping
(
"/clientes/{dni}/cuentas"
)
@ResponseStatus
(
HttpStatus
.
OK
)
List
<
DTOCuenta
>
verCuentas
(
@PathVariable
String
dni
)
{
return
servicios
.
verCuentas
(
dni
).
stream
()
.
map
(
c
->
new
DTOCuenta
(
c
)).
collect
(
Collectors
.
toList
());
}
@GetMapping
(
"/clientes/{dni}/cuentas/{num}"
)
ResponseEntity
<
DTOCuenta
>
verCuenta
(
@PathVariable
String
dni
,
@PathVariable
String
num
)
{
return
servicios
.
verCuentas
(
dni
).
stream
()
.
filter
(
c
->
c
.
getNum
().
equals
(
num
))
.
findFirst
()
.
map
(
c
->
ResponseEntity
.
ok
(
new
DTOCuenta
(
c
)))
.
orElse
(
ResponseEntity
.
notFound
().
build
());
}
@PostMapping
(
"/clientes/{dni}/cuentas/{numCuenta}/movimientos"
)
ResponseEntity
<
Void
>
registrarMovimiento
(
@PathVariable
String
dni
,
@PathVariable
String
numCuenta
,
@RequestBody
DTOMovimiento
movimiento
)
{
if
(
movimiento
.
getTipo
()
==
null
)
{
return
ResponseEntity
.
badRequest
().
build
();
}
switch
(
movimiento
.
getTipo
().
toLowerCase
())
{
case
"ingreso"
:
servicios
.
ingreso
(
numCuenta
,
movimiento
.
getNumTarjeta
(),
movimiento
.
getImporte
());
break
;
case
"reintegro"
:
servicios
.
reintegro
(
numCuenta
,
movimiento
.
getNumTarjeta
(),
movimiento
.
getImporte
());
break
;
case
"transferencia"
:
servicios
.
transferencia
(
numCuenta
,
movimiento
.
getNumCuenta
(),
movimiento
.
getImporte
());
break
;
default
:
return
ResponseEntity
.
badRequest
().
build
();
}
return
ResponseEntity
.
ok
().
build
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment