Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Equipo 3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
migudel
Equipo 3
Merge requests
!23
Integración de servicio usuarios
Code
Review changes
Check out branch
Open in Workspace
Download
Patches
Plain diff
Expand sidebar
Merged
Integración de servicio usuarios
dev/monolith/split_services_hotel_and_user
into
develop
Overview
0
Commits
2
Pipelines
0
Changes
2
Merged
Integración de servicio usuarios
migudel
requested to merge
dev/monolith/split_services_hotel_and_user
into
develop
9 months ago
Overview
0
Commits
2
Pipelines
0
Changes
2
0
0
Merge request reports
Compare
develop
version 1
6b7f97b2
9 months ago
develop (base)
and
latest version
latest version
2d6f3903
2 commits,
9 months ago
version 1
6b7f97b2
1 commit,
9 months ago
2 files
+
146
−
76
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
java/roomBooking/src/main/java/com/uva/monolith/services/users/controllers/UserController.java
+
49
−
76
View file @ 2d6f3903
Edit in single-file editor
Open in Web IDE
Show full file
package
com.uva.monolith.services.users.controllers
;
import
java.time.LocalDate
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
@@ -19,15 +17,11 @@ 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.RestController
;
import
org.springframework.web.
server.ResponseStatus
Exception
;
import
org.springframework.web.
client.HttpClientError
Exception
;
import
com.uva.monolith.services.bookings.models.Booking
;
import
com.uva.monolith.services.users.models.Client
;
import
com.uva.monolith.services.users.models.User
;
import
com.uva.monolith.services.users.models.UserRol
;
import
com.uva.monolith.services.users.models.UserStatus
;
import
com.uva.monolith.services.users.repositories.ClientRepository
;
import
com.uva.monolith.services.users.repositories.UserRepository
;
import
com.uva.monolith.services.users.services.UserService
;
@RestController
@RequestMapping
(
"users"
)
@@ -35,103 +29,82 @@ import com.uva.monolith.services.users.repositories.UserRepository;
public
class
UserController
{
@Autowired
private
UserRepository
userRepository
;
@Autowired
private
ClientRepository
clientRepository
;
private
UserService
userService
;
@GetMapping
public
List
<
User
>
getAllUsers
()
{
return
userRepository
.
findAll
();
public
ResponseEntity
<
List
<
User
>>
getAllUsers
()
{
List
<
User
>
users
=
userService
.
getAllUsers
();
return
ResponseEntity
.
ok
(
users
);
}
@GetMapping
(
params
=
{
"email"
})
public
ResponseEntity
<?>
getUserByEmail
(
@RequestParam
String
email
)
{
try
{
return
new
ResponseEntity
<
User
>(
userRepository
.
findByEmail
(
email
).
orElseThrow
(),
HttpStatus
.
ACCEPTED
);
}
catch
(
Exception
e
)
{
return
new
ResponseEntity
<>(
HttpStatus
.
NOT_FOUND
);
return
ResponseEntity
.
ok
(
userService
.
getUserByEmail
(
email
));
}
catch
(
HttpClientErrorException
e
)
{
if
(
e
.
getStatusCode
()
==
HttpStatus
.
NOT_FOUND
)
return
new
ResponseEntity
<
String
>(
HttpStatus
.
NOT_FOUND
);
throw
e
;
}
}
@PostMapping
public
User
addUser
(
@RequestBody
User
user
)
{
// user.setStatus(UserStatus.NO_BOOKINGS);
if
(
user
.
getRol
()
==
null
)
// Rol por defecto
user
.
setRol
(
UserRol
.
CLIENT
);
return
userRepository
.
save
(
user
);
public
ResponseEntity
<?>
addUser
(
@RequestBody
User
user
)
{
userService
.
addUser
(
user
);
return
new
ResponseEntity
<>(
HttpStatus
.
ACCEPTED
);
}
@GetMapping
(
"/{id}"
)
public
User
getUserById
(
@PathVariable
int
id
)
{
return
user
Repo
sitory
.
findById
(
id
).
orElseThrow
(
);
public
ResponseEntity
<?>
getUserById
(
@PathVariable
int
id
)
{
return
Re
s
po
nseEntity
.
ok
(
userService
.
getUserById
(
id
)
);
}
@PutMapping
(
"/{id}"
)
public
User
updateUserData
(
@PathVariable
int
id
,
@RequestBody
Map
<
String
,
String
>
json
)
{
User
target
=
userRepository
.
findById
(
id
).
orElseThrow
();
if
(!
json
.
containsKey
(
"name"
)
||
!
json
.
containsKey
(
"email"
))
{
// TODO cambiar manejo
throw
new
RuntimeException
(
"Missing required fields"
);
public
ResponseEntity
<?>
updateUserData
(
@PathVariable
int
id
,
@RequestBody
Map
<
String
,
String
>
json
)
{
String
name
=
json
.
get
(
"name"
);
String
email
=
json
.
get
(
"email"
);
if
(
name
==
null
||
email
==
null
)
{
return
new
ResponseEntity
<
String
>(
"Missing required fields"
,
HttpStatus
.
BAD_REQUEST
);
}
try
{
User
user
=
userService
.
updateUserData
(
id
,
name
,
email
);
return
new
ResponseEntity
<
User
>(
user
,
HttpStatus
.
OK
);
}
catch
(
HttpClientErrorException
e
)
{
if
(
e
.
getStatusCode
()
==
HttpStatus
.
NOT_FOUND
)
return
new
ResponseEntity
<
String
>(
HttpStatus
.
NOT_FOUND
);
throw
e
;
}
target
.
setName
(
json
.
get
(
"name"
));
target
.
setEmail
(
json
.
get
(
"email"
));
return
userRepository
.
save
(
target
);
}
@PatchMapping
(
"/{id}"
)
public
User
updateUserState
(
@PathVariable
int
id
,
@RequestBody
Map
<
String
,
String
>
json
)
{
Client
target
=
clientRepository
.
findById
(
id
).
orElseThrow
();
public
ResponseEntity
<?>
updateUserState
(
@PathVariable
int
id
,
@RequestBody
Map
<
String
,
String
>
json
)
{
String
strStatus
=
json
.
get
(
"status"
);
if
(
strStatus
==
null
)
{
// TODO cambiar manejo
throw
new
RuntimeException
(
"Missing required fields"
);
return
new
ResponseEntity
<
String
>(
"Missing required fields"
,
HttpStatus
.
BAD_REQUEST
);
}
UserStatus
userStatus
=
UserStatus
.
valueOf
(
strStatus
);
boolean
activeBookings
=
target
.
getBookings
().
stream
()
.
anyMatch
(
booking
->
!
booking
.
getEndDate
().
isBefore
(
LocalDate
.
now
()));
// reserva >= ahora
boolean
inactiveBookings
=
target
.
getBookings
().
stream
()
.
anyMatch
(
booking
->
booking
.
getStartDate
().
isBefore
(
LocalDate
.
now
()));
// reserva < ahora
switch
(
userStatus
)
{
// TODO Buscar como validar las (in)active bookings
case
NO_BOOKINGS:
if
(!
target
.
getBookings
().
isEmpty
())
throw
new
IllegalArgumentException
(
"Invalid State: The user has at least one booking"
);
break
;
case
WITH_ACTIVE_BOOKINGS:
if
(
target
.
getBookings
().
isEmpty
())
throw
new
IllegalArgumentException
(
"Invalid State: The user don't has bookings"
);
if
(!
activeBookings
)
throw
new
IllegalArgumentException
(
"Invalid State: The user don't has active bookings"
);
break
;
case
WITH_INACTIVE_BOOKINGS:
if
(
target
.
getBookings
().
isEmpty
())
throw
new
IllegalArgumentException
(
"Invalid State: The user don't has bookings"
);
if
(!
inactiveBookings
)
throw
new
IllegalArgumentException
(
"Invalid State: The user don't has inactive bookings"
);
break
;
default
:
break
;
try
{
UserStatus
userStatus
=
UserStatus
.
valueOf
(
strStatus
);
return
ResponseEntity
.
ok
(
userService
.
updateUserStatus
(
id
,
userStatus
));
}
catch
(
IllegalArgumentException
e
)
{
return
new
ResponseEntity
<
String
>(
"Unknown user state"
,
HttpStatus
.
BAD_REQUEST
);
}
catch
(
HttpClientErrorException
e
)
{
if
(
e
.
getStatusCode
()
==
HttpStatus
.
NOT_FOUND
)
return
new
ResponseEntity
<
String
>(
HttpStatus
.
NOT_FOUND
);
throw
e
;
}
target
.
setStatus
(
userStatus
);
return
userRepository
.
save
(
target
);
}
@DeleteMapping
(
"/{id}"
)
public
User
deleteUser
(
@PathVariable
Integer
id
)
{
User
target
;
if
((
target
=
userRepository
.
findById
(
id
).
orElseThrow
())
!=
null
)
{
userRepository
.
deleteById
(
id
);
public
ResponseEntity
<?>
deleteUser
(
@PathVariable
Integer
id
)
{
try
{
return
ResponseEntity
.
ok
(
userService
.
deleteUserById
(
id
));
}
catch
(
HttpClientErrorException
e
)
{
if
(
e
.
getStatusCode
()
==
HttpStatus
.
NOT_FOUND
)
return
new
ResponseEntity
<
String
>(
HttpStatus
.
NOT_FOUND
);
throw
e
;
}
return
target
;
}
@GetMapping
(
"/{id}/bookings"
)
public
List
<
Booking
>
getUserBookingsById
(
@PathVariable
int
id
)
{
Client
user
=
clientRepository
.
findById
(
id
).
orElseThrow
();
return
user
.
getBookings
();
}
}
Loading