Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
spring-boot-data-mongodb-curd
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Syed Javed Ali
spring-boot-data-mongodb-curd
Commits
b421c25e
Commit
b421c25e
authored
Mar 02, 2021
by
Syed Javed Ali
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added map(), flatmap(), filter operators to the employee controller class
parent
7036f8c9
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
35 additions
and
20 deletions
+35
-20
EmployeeController.java
.../nisum/example/mongodb/controller/EmployeeController.java
+27
-11
EmployeeServiceImpl.java
...om/nisum/example/mongodb/service/EmployeeServiceImpl.java
+6
-7
IEmployeeService.java
...a/com/nisum/example/mongodb/service/IEmployeeService.java
+1
-0
EmployeeControllerTest.java
...um/example/mongodb/controller/EmployeeControllerTest.java
+1
-2
No files found.
src/main/java/com/nisum/example/mongodb/controller/EmployeeController.java
View file @
b421c25e
...
...
@@ -4,7 +4,6 @@ import com.nisum.example.mongodb.model.Employee;
import
com.nisum.example.mongodb.service.IEmployeeService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
reactor.core.publisher.Flux
;
...
...
@@ -27,6 +26,7 @@ public class EmployeeController {
@PostMapping
(
path
=
"/create"
)
@ResponseStatus
(
HttpStatus
.
CREATED
)
public
Mono
<
Employee
>
create
(
@RequestBody
Employee
e
)
{
return
employeeService
.
insert
(
e
).
log
();
}
...
...
@@ -36,10 +36,12 @@ public class EmployeeController {
* @return Mono
*/
@GetMapping
(
"/{id}"
)
public
ResponseEntity
<
Mono
<
Employee
>>
findById
(
@PathVariable
(
"id"
)
Integer
id
)
{
Mono
<
Employee
>
emp
=
employeeService
.
fetchById
(
id
).
log
();
HttpStatus
status
=
emp
!=
null
?
HttpStatus
.
OK
:
HttpStatus
.
NOT_FOUND
;
return
new
ResponseEntity
<
Mono
<
Employee
>>(
emp
,
status
);
public
Mono
<
ResponseEntity
<
Employee
>>
findById
(
@PathVariable
(
"id"
)
Integer
id
)
{
return
employeeService
.
fetchById
(
id
)
.
log
()
.
map
(
employee
->
new
ResponseEntity
<>(
employee
,
HttpStatus
.
OK
))
.
log
()
.
defaultIfEmpty
(
new
ResponseEntity
<>(
HttpStatus
.
NOT_FOUND
));
}
/**
...
...
@@ -48,10 +50,8 @@ public class EmployeeController {
* @return Flux
*/
@GetMapping
(
"/name/{name}"
)
public
ResponseEntity
<
Flux
<
Employee
>>
findByName
(
@PathVariable
(
"name"
)
String
name
)
{
Flux
<
Employee
>
employeeFlux
=
employeeService
.
fetchByName
(
name
).
log
();
HttpStatus
status
=
employeeFlux
!=
null
?
HttpStatus
.
OK
:
HttpStatus
.
NOT_FOUND
;
return
new
ResponseEntity
<>(
employeeFlux
,
status
);
public
Flux
<
Employee
>
findByName
(
@PathVariable
(
"name"
)
String
name
)
{
return
employeeService
.
fetchByName
(
name
).
log
();
}
...
...
@@ -59,9 +59,11 @@ public class EmployeeController {
* GET method to fetch all records
* @return Flux
*/
@GetMapping
(
value
=
""
,
produces
=
MediaType
.
TEXT_EVENT_STREAM_VALUE
)
@GetMapping
(
value
=
""
)
public
Flux
<
Employee
>
findAll
()
{
Flux
<
Employee
>
emps
=
employeeService
.
fetchAll
().
log
();
Flux
<
Employee
>
emps
=
employeeService
.
fetchAll
()
.
log
()
.
filter
(
employee
->
employee
.
getSalary
()>
2000
);
return
emps
;
}
...
...
@@ -75,6 +77,20 @@ public class EmployeeController {
return
employeeService
.
modify
(
emp
).
log
();
}
@PutMapping
(
"/{id}"
)
public
Mono
<
ResponseEntity
<
Employee
>>
updateById
(
@PathVariable
(
"id"
)
Integer
id
,
@RequestBody
Employee
employee
){
return
employeeService
.
modifyById
(
id
)
.
flatMap
(
emp
->{
emp
.
setSalary
(
employee
.
getSalary
());
return
employeeService
.
insert
(
emp
);
})
.
log
()
.
map
(
updateEmp
->
new
ResponseEntity
<>(
updateEmp
,
HttpStatus
.
OK
))
.
log
()
.
defaultIfEmpty
(
new
ResponseEntity
<>(
HttpStatus
.
NOT_FOUND
));
}
/**
* DELETE method to remove record based on Id
* @param id
...
...
src/main/java/com/nisum/example/mongodb/service/EmployeeServiceImpl.java
View file @
b421c25e
...
...
@@ -50,19 +50,18 @@ public class EmployeeServiceImpl implements IEmployeeService {
return
employeeRepository
.
findByName
(
name
);
}
@Override
public
Mono
<
Employee
>
modifyById
(
Integer
id
)
{
return
employeeRepository
.
findById
(
id
);
}
/**
* perform fetch all records
* @return Flux
*/
@Override
public
Flux
<
Employee
>
fetchAll
()
{
return
employeeRepository
.
findAll
()
/*.delayElements(Duration.ofSeconds(1)).map(
employee -> {
employee.setSalary(4000L);
System.out.println(employee);
return employee;
})*/
;
return
employeeRepository
.
findAll
();
}
/**
...
...
src/main/java/com/nisum/example/mongodb/service/IEmployeeService.java
View file @
b421c25e
...
...
@@ -11,5 +11,6 @@ public interface IEmployeeService {
Flux
<
Employee
>
fetchByName
(
String
name
);
Flux
<
Employee
>
fetchAll
();
Mono
<
Employee
>
modify
(
Employee
emp
);
Mono
<
Employee
>
modifyById
(
Integer
id
);
Mono
<
Void
>
removeById
(
Integer
id
);
}
src/test/java/com/nisum/example/mongodb/controller/EmployeeControllerTest.java
View file @
b421c25e
...
...
@@ -126,8 +126,7 @@ public class EmployeeControllerTest {
.
uri
(
""
)
.
exchange
()
.
expectStatus
().
isOk
()
.
expectBodyList
(
Employee
.
class
)
.
hasSize
(
2
);
.
expectBodyList
(
Employee
.
class
);
Mockito
.
verify
(
repository
,
times
(
1
)).
findAll
();
}
...
...
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