Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
Webflux_mongodb
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
Ravinder Pannala
Webflux_mongodb
Commits
f5a9e3dc
Commit
f5a9e3dc
authored
Mar 13, 2023
by
Ravinder Pannala
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Real time scenarios for Webflux
parent
9df1fc68
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
126 additions
and
30 deletions
+126
-30
OrgEmployeeDTO.java
...mple/nisum/webfluxmongodb/zipping/DTO/OrgEmployeeDTO.java
+17
-0
DepartmentController.java
...bfluxmongodb/zipping/controller/DepartmentController.java
+14
-3
EmployeeController.java
...webfluxmongodb/zipping/controller/EmployeeController.java
+2
-2
OrganizationController.java
...luxmongodb/zipping/controller/OrganizationController.java
+16
-9
OrganizationRepository.java
...luxmongodb/zipping/repository/OrganizationRepository.java
+3
-0
DepartmentService.java
...sum/webfluxmongodb/zipping/service/DepartmentService.java
+34
-14
EmployeeService.java
...nisum/webfluxmongodb/zipping/service/EmployeeService.java
+10
-1
OrganizationService.java
...m/webfluxmongodb/zipping/service/OrganizationService.java
+30
-1
No files found.
src/main/java/com/example/nisum/webfluxmongodb/zipping/DTO/OrgEmployeeDTO.java
0 → 100644
View file @
f5a9e3dc
package
com
.
example
.
nisum
.
webfluxmongodb
.
zipping
.
DTO
;
import
com.example.nisum.webfluxmongodb.zipping.model.Employee
;
import
lombok.*
;
import
java.util.List
;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
public
class
OrgEmployeeDTO
{
private
String
name
;
private
List
<
Employee
>
employeeList
;
}
src/main/java/com/example/nisum/webfluxmongodb/zipping/controller/DepartmentController.java
View file @
f5a9e3dc
...
...
@@ -2,6 +2,7 @@ package com.example.nisum.webfluxmongodb.zipping.controller;
import
com.example.nisum.webfluxmongodb.zipping.DTO.DepartmentEmployeeDto
;
import
com.example.nisum.webfluxmongodb.zipping.model.Department
;
import
com.example.nisum.webfluxmongodb.zipping.model.Employee
;
import
com.example.nisum.webfluxmongodb.zipping.service.DepartmentService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.PathVariable
;
...
...
@@ -10,6 +11,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RestController
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
reactor.util.function.Tuple2
;
import
java.util.List
;
@RestController
@RequestMapping
(
"/api/dept"
)
...
...
@@ -28,14 +32,21 @@ public class DepartmentController {
return
departmentService
.
getAll
();
}
@RequestMapping
(
"/get/{orgId}"
)
@RequestMapping
(
"/get/
org/
{orgId}"
)
private
Flux
<
Department
>
getByOrgId
(
@PathVariable
int
orgId
){
return
departmentService
.
findByOrganizationId
(
orgId
);
}
//Get Department by Id
@RequestMapping
(
"/get/{deptId}"
)
private
Mono
<
DepartmentEmployeeDto
>
findByDepartmentId
(
@PathVariable
int
id
){
return
departmentService
.
findById
(
id
);
private
Mono
<
DepartmentEmployeeDto
>
findByDepartmentId
(
@PathVariable
int
deptId
){
return
departmentService
.
findById
(
deptId
);
}
@RequestMapping
(
"/getAll"
)
private
Flux
<
DepartmentEmployeeDto
>
findAllDepartmentAndEmployees
(){
return
departmentService
.
findAllDepartmentEmployees
();
}
}
src/main/java/com/example/nisum/webfluxmongodb/zipping/controller/EmployeeController.java
View file @
f5a9e3dc
...
...
@@ -28,12 +28,12 @@ public class EmployeeController {
}
@RequestMapping
(
"/get/{orgId}"
)
@RequestMapping
(
"/get/
org/
{orgId}"
)
private
Flux
<
Employee
>
findByOrganizationId
(
@PathVariable
int
orgId
)
{
return
employeeService
.
findByOrganizationId
(
orgId
);
}
@RequestMapping
(
"/get/
{
Id}"
)
@RequestMapping
(
"/get/
dept/{department
Id}"
)
private
Flux
<
Employee
>
findByDepartmentId
(
@PathVariable
int
departmentId
)
{
return
employeeService
.
findByDepartmentId
(
departmentId
);
}
...
...
src/main/java/com/example/nisum/webfluxmongodb/zipping/controller/OrganizationController.java
View file @
f5a9e3dc
package
com
.
example
.
nisum
.
webfluxmongodb
.
zipping
.
controller
;
import
com.example.nisum.webfluxmongodb.zipping.DTO.OrgEmployeeDTO
;
import
com.example.nisum.webfluxmongodb.zipping.Exception.ResourceNotFoundException
;
import
com.example.nisum.webfluxmongodb.zipping.model.Employee
;
import
com.example.nisum.webfluxmongodb.zipping.model.Organization
;
import
com.example.nisum.webfluxmongodb.zipping.service.EmployeeService
;
import
com.example.nisum.webfluxmongodb.zipping.service.OrganizationService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.mongodb.core.aggregation.BooleanOperators
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.client.ResourceAccessException
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
...
...
@@ -32,10 +27,22 @@ public class OrganizationController {
return
organizationService
.
getAll
();
}
@RequestMapping
(
"/get/{id}"
)
//Get All employees based on Organization Id
@GetMapping
(
"/get/{id}"
)
private
Mono
<
Organization
>
findById
(
@PathVariable
int
id
)
{
Mono
<
Organization
>
organizationMono
=
organizationService
.
findById
(
id
);
organizationMono
.
switchIfEmpty
(
Mono
.
error
(
new
ResourceNotFoundException
(
"Resource not found"
)));
return
organizationMono
;
}
//Get ALl EMployees by OrganizationName
@GetMapping
(
"/getByName/{name}"
)
private
Mono
<
OrgEmployeeDTO
>
findByOrgName
(
@PathVariable
String
name
){
return
organizationService
.
findByOrgName
(
name
);
}
@DeleteMapping
(
"/delete/{id}"
)
private
Mono
<
ResponseEntity
<
Void
>>
delete
(
@PathVariable
int
id
){
return
organizationService
.
deleteById
(
id
);
}
}
src/main/java/com/example/nisum/webfluxmongodb/zipping/repository/OrganizationRepository.java
View file @
f5a9e3dc
package
com
.
example
.
nisum
.
webfluxmongodb
.
zipping
.
repository
;
import
com.example.nisum.webfluxmongodb.zipping.model.Employee
;
import
com.example.nisum.webfluxmongodb.zipping.model.Organization
;
import
org.springframework.data.mongodb.repository.ReactiveMongoRepository
;
import
org.springframework.stereotype.Repository
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
import
reactor.core.publisher.Mono
;
@Repository
public
interface
OrganizationRepository
extends
ReactiveMongoRepository
<
Organization
,
Integer
>
{
Mono
<
Organization
>
findByName
(
String
name
);
}
src/main/java/com/example/nisum/webfluxmongodb/zipping/service/DepartmentService.java
View file @
f5a9e3dc
package
com
.
example
.
nisum
.
webfluxmongodb
.
zipping
.
service
;
import
com.example.nisum.webfluxmongodb.zipping.DTO.DepartmentEmployeeDto
;
import
com.example.nisum.webfluxmongodb.zipping.Exception.ResourceNotFoundException
;
import
com.example.nisum.webfluxmongodb.zipping.model.Department
;
import
com.example.nisum.webfluxmongodb.zipping.model.Employee
;
import
com.example.nisum.webfluxmongodb.zipping.repository.DepartmentRepository
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
reactor.core.Disposable
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
reactor.util.function.Tuple2
;
import
java.util.ArrayList
;
import
java.util.List
;
@Service
...
...
@@ -37,19 +34,42 @@ public class DepartmentService {
}
public
Mono
<
DepartmentEmployeeDto
>
findById
(
int
id
)
{
List
<
Employee
>
employees
=
new
ArrayList
<>();
Mono
<
DepartmentEmployeeDto
>
map
=
departmentRepository
.
findById
(
id
).
zipWhen
(
dept
->
{
Flux
<
Employee
>
employeeFlux
=
employeeService
.
findByDepartmentId
(
dept
.
getId
()).
switchIfEmpty
(
Flux
.
just
(
new
Employee
()));
Mono
<
DepartmentEmployeeDto
>
map
=
departmentRepository
.
findById
(
id
).
zipWhen
(
dept
->
employeeService
.
findByDepartmentId
(
dept
.
getId
()).
collectList
()).
map
(
tuple
->
{
DepartmentEmployeeDto
departmentEmployeeDto
=
new
DepartmentEmployeeDto
();
Department
dept
=
tuple
.
getT1
();
List
<
Employee
>
employeeList
=
tuple
.
getT2
();
departmentEmployeeDto
.
setName
(
dept
.
getName
());
departmentEmployeeDto
.
setEmployeeList
(
employeeList
);
return
departmentEmployeeDto
;
});
return
map
;
}
public
Flux
<
DepartmentEmployeeDto
>
findAllDepartmentEmployees
()
{
Flux
<
DepartmentEmployeeDto
>
map
=
departmentRepository
.
findAll
().
flatMap
(
this
::
apply
)
.
map
(
tuple
->
{
DepartmentEmployeeDto
departmentEmployeeDto
=
new
DepartmentEmployeeDto
();
departmentEmployeeDto
.
setName
(
dept
.
getName
());
employeeFlux
.
collectList
().
subscribe
(
employees:
:
addAll
);
departmentEmployeeDto
.
setEmployeeList
(
employees
);
return
Mono
.
just
(
departmentEmployeeDto
);
}).
switchIfEmpty
(
Mono
.
error
(
new
ResourceNotFoundException
(
"Department id not found"
))).
map
(
tuple
->
{
DepartmentEmployeeDto
t2
=
tuple
.
getT2
();
return
t2
;
departmentEmployeeDto
.
setName
(
tuple
.
getT1
().
getName
());
departmentEmployeeDto
.
setEmployeeList
(
tuple
.
getT2
());
return
departmentEmployeeDto
;
});
return
map
;
}
private
Mono
<
Tuple2
<
Department
,
List
<
Employee
>>>
apply
(
Department
department
)
{
return
Mono
.
just
(
department
)
.
zipWith
(
employeeService
.
getAll
().
filter
(
it
->
it
.
getDepartmentId
()
==
department
.
getId
()).
collectList
())
.
map
(
tuple
->
{
tuple
.
getT1
().
setEmployees
(
tuple
.
getT2
());
return
tuple
;
});
}
public
Mono
<
Void
>
deleteDepartment
(
Department
dept
)
{
return
departmentRepository
.
delete
(
dept
);
}
public
Mono
<
Void
>
delete
(
Flux
<
Department
>
departmentFlux
)
{
return
departmentRepository
.
deleteAll
(
departmentFlux
);
}
}
src/main/java/com/example/nisum/webfluxmongodb/zipping/service/EmployeeService.java
View file @
f5a9e3dc
...
...
@@ -22,10 +22,19 @@ public class EmployeeService {
}
public
Flux
<
Employee
>
findByOrganizationId
(
int
orgId
)
{
return
employeeRepository
.
findByOrganizationId
(
orgId
);
Flux
<
Employee
>
employeeFlux
=
employeeRepository
.
findByOrganizationId
(
orgId
);
return
employeeFlux
;
}
public
Flux
<
Employee
>
findByDepartmentId
(
int
departmentId
)
{
return
employeeRepository
.
findByDepartmentId
(
departmentId
);
}
public
Mono
<
Void
>
deleteEmployee
(
Employee
emp
)
{
return
employeeRepository
.
delete
(
emp
);
}
public
Mono
<
Void
>
delete
(
Flux
<
Employee
>
employeeFlux
)
{
return
employeeRepository
.
deleteAll
(
employeeFlux
);
}
}
src/main/java/com/example/nisum/webfluxmongodb/zipping/service/OrganizationService.java
View file @
f5a9e3dc
package
com
.
example
.
nisum
.
webfluxmongodb
.
zipping
.
service
;
import
com.example.nisum.webfluxmongodb.zipping.DTO.OrgEmployeeDTO
;
import
com.example.nisum.webfluxmongodb.zipping.Exception.ResourceNotFoundException
;
import
com.example.nisum.webfluxmongodb.zipping.model.Department
;
import
com.example.nisum.webfluxmongodb.zipping.model.Employee
;
import
com.example.nisum.webfluxmongodb.zipping.model.Organization
;
import
com.example.nisum.webfluxmongodb.zipping.repository.OrganizationRepository
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Service
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
reactor.util.function.Tuple2
;
import
java.util.ArrayList
;
import
java.util.List
;
@Service
...
...
@@ -49,4 +51,31 @@ public class OrganizationService {
return
organizationMono
;
}
public
Mono
<
OrgEmployeeDTO
>
findByOrgName
(
String
name
)
{
Mono
<
Tuple2
<
Organization
,
List
<
Employee
>>>
tuple2Mono
=
organizationRepository
.
findByName
(
name
)
.
switchIfEmpty
(
Mono
.
error
(
new
ResourceNotFoundException
(
"Organization details not found with Name"
)))
.
zipWhen
(
org
->
employeeService
.
findByOrganizationId
(
org
.
getId
()).
collectList
());
Mono
<
OrgEmployeeDTO
>
orgEmployeeDTOMono
=
tuple2Mono
.
switchIfEmpty
(
Mono
.
error
(
new
ResourceNotFoundException
(
"Employee details not found"
))).
map
(
tuple
->
{
Organization
org
=
tuple
.
getT1
();
List
<
Employee
>
employeeList
=
tuple
.
getT2
();
OrgEmployeeDTO
orgEmployeeDTO
=
new
OrgEmployeeDTO
();
orgEmployeeDTO
.
setName
(
org
.
getName
());
orgEmployeeDTO
.
setEmployeeList
(
employeeList
);
return
orgEmployeeDTO
;
});
return
orgEmployeeDTOMono
;
}
public
Mono
<
ResponseEntity
<
Void
>>
deleteById
(
int
id
)
{
return
organizationRepository
.
findById
(
id
).
flatMap
(
org
->
{
Flux
<
Department
>
departmentFlux
=
departmentService
.
findByOrganizationId
(
org
.
getId
());
departmentService
.
delete
(
departmentFlux
);
Flux
<
Employee
>
employeeFlux
=
employeeService
.
findByOrganizationId
(
org
.
getId
());
Mono
<
Void
>
delete
=
employeeService
.
delete
(
employeeFlux
);
return
organizationRepository
.
delete
(
org
).
then
(
Mono
.
just
(
new
ResponseEntity
<
Void
>(
HttpStatus
.
OK
)));
}).
defaultIfEmpty
(
new
ResponseEntity
<>(
HttpStatus
.
NOT_FOUND
));
}
}
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