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
c48e453e
Commit
c48e453e
authored
Mar 08, 2021
by
Syed Javed Ali
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added EmployeeMapper class to convert model to dto and viceversa, Added Slf4J for logging
parent
b421c25e
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
108 additions
and
69 deletions
+108
-69
build.gradle
build.gradle
+3
-0
MongoConfig.java
...in/java/com/nisum/example/mongodb/config/MongoConfig.java
+0
-37
EmployeeController.java
.../nisum/example/mongodb/controller/EmployeeController.java
+28
-9
EmployeeMapper.java
...java/com/nisum/example/mongodb/mapper/EmployeeMapper.java
+12
-0
EmployeeDTO.java
...ain/java/com/nisum/example/mongodb/model/EmployeeDTO.java
+29
-0
EmployeeRepository.java
.../nisum/example/mongodb/repository/EmployeeRepository.java
+2
-1
EmployeeServiceImpl.java
...om/nisum/example/mongodb/service/EmployeeServiceImpl.java
+25
-13
IEmployeeService.java
...a/com/nisum/example/mongodb/service/IEmployeeService.java
+7
-7
application.properties
src/main/resources/application.properties
+2
-2
No files found.
build.gradle
View file @
c48e453e
...
...
@@ -26,6 +26,9 @@ dependencies {
annotationProcessor
'org.projectlombok:lombok'
testImplementation
'org.springframework.boot:spring-boot-starter-test'
testImplementation
'io.projectreactor:reactor-test'
implementation
'org.mapstruct:mapstruct:1.4.2.Final'
annotationProcessor
'org.mapstruct:mapstruct-processor:1.4.2.Final'
}
test
{
...
...
src/main/java/com/nisum/example/mongodb/config/MongoConfig.java
deleted
100644 → 0
View file @
b421c25e
package
com
.
nisum
.
example
.
mongodb
.
config
;
import
com.mongodb.reactivestreams.client.MongoClient
;
import
com.mongodb.reactivestreams.client.MongoClients
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration
;
import
org.springframework.data.mongodb.core.ReactiveMongoTemplate
;
import
org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories
;
@Configuration
@EnableReactiveMongoRepositories
(
basePackages
=
"com.nisum.example.mongodb.repository"
)
public
class
MongoConfig
extends
AbstractReactiveMongoConfiguration
{
@Value
(
"${port}"
)
private
String
port
;
@Value
(
"${dbname}"
)
private
String
dbName
;
@Override
public
MongoClient
reactiveMongoClient
()
{
return
MongoClients
.
create
();
}
@Override
protected
String
getDatabaseName
()
{
return
dbName
;
}
@Bean
public
ReactiveMongoTemplate
reactiveMongoTemplate
()
{
return
new
ReactiveMongoTemplate
(
reactiveMongoClient
(),
getDatabaseName
());
}
}
src/main/java/com/nisum/example/mongodb/controller/EmployeeController.java
View file @
c48e453e
package
com
.
nisum
.
example
.
mongodb
.
controller
;
import
com.nisum.example.mongodb.mapper.EmployeeMapper
;
import
com.nisum.example.mongodb.model.Employee
;
import
com.nisum.example.mongodb.service.IEmployeeService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
...
...
@@ -12,22 +14,29 @@ import reactor.core.publisher.Mono;
/**
* Controller class having http POST, GET, UPDATE and DELETE methods
*/
@Slf4j
@RestController
public
class
EmployeeController
{
@Autowired
EmployeeMapper
employeeMapper
;
@Autowired
private
IEmployeeService
employeeService
;
/**
* POST method to perform record creation
* @param e
* @param e
mployee
* @return Mono
*/
@PostMapping
(
path
=
"/create"
)
@ResponseStatus
(
HttpStatus
.
CREATED
)
public
Mono
<
Employee
>
create
(
@RequestBody
Employee
e
)
{
public
Mono
<
Employee
>
create
(
@RequestBody
Employee
e
mployee
)
{
return
employeeService
.
insert
(
e
).
log
();
log
.
info
(
"Calling service insert method..."
);
return
employeeService
.
insert
(
employeeMapper
.
toDto
(
employee
))
.
map
(
employeeMapper:
:
toModel
)
.
log
();
}
/**
...
...
@@ -37,10 +46,11 @@ public class EmployeeController {
*/
@GetMapping
(
"/{id}"
)
public
Mono
<
ResponseEntity
<
Employee
>>
findById
(
@PathVariable
(
"id"
)
Integer
id
)
{
log
.
info
(
"Calling service fetchById method..."
);
return
employeeService
.
fetchById
(
id
)
.
map
(
employeeMapper:
:
toModel
)
.
log
()
.
map
(
employee
->
new
ResponseEntity
<>(
employee
,
HttpStatus
.
OK
))
.
log
()
.
defaultIfEmpty
(
new
ResponseEntity
<>(
HttpStatus
.
NOT_FOUND
));
}
...
...
@@ -51,7 +61,10 @@ public class EmployeeController {
*/
@GetMapping
(
"/name/{name}"
)
public
Flux
<
Employee
>
findByName
(
@PathVariable
(
"name"
)
String
name
)
{
return
employeeService
.
fetchByName
(
name
).
log
();
log
.
info
(
"Calling service fetchByName"
);
return
employeeService
.
fetchByName
(
name
)
.
map
(
employeeMapper:
:
toModel
)
.
log
();
}
...
...
@@ -59,9 +72,11 @@ public class EmployeeController {
* GET method to fetch all records
* @return Flux
*/
@GetMapping
(
value
=
""
)
@GetMapping
(
path
=
""
)
public
Flux
<
Employee
>
findAll
()
{
log
.
info
(
"Calling service fetchAll() method..."
);
Flux
<
Employee
>
emps
=
employeeService
.
fetchAll
()
.
map
(
employeeMapper:
:
toModel
)
.
log
()
.
filter
(
employee
->
employee
.
getSalary
()>
2000
);
return
emps
;
...
...
@@ -74,16 +89,20 @@ public class EmployeeController {
*/
@PutMapping
(
"/update"
)
public
Mono
<
Employee
>
update
(
@RequestBody
Employee
emp
)
{
return
employeeService
.
modify
(
emp
).
log
();
log
.
info
(
"Calling service modify() method..."
);
return
employeeService
.
modify
(
employeeMapper
.
toDto
(
emp
))
.
map
(
employeeMapper:
:
toModel
)
.
log
();
}
@PutMapping
(
"/{id}"
)
@PutMapping
(
"
update
/{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
);
return
employeeService
.
insert
(
employeeMapper
.
toDto
(
employee
))
.
map
(
employeeMapper:
:
toModel
);
})
.
log
()
.
map
(
updateEmp
->
new
ResponseEntity
<>(
updateEmp
,
HttpStatus
.
OK
))
...
...
src/main/java/com/nisum/example/mongodb/mapper/EmployeeMapper.java
0 → 100644
View file @
c48e453e
package
com
.
nisum
.
example
.
mongodb
.
mapper
;
import
com.nisum.example.mongodb.model.Employee
;
import
com.nisum.example.mongodb.model.EmployeeDTO
;
import
org.mapstruct.Mapper
;
@Mapper
(
componentModel
=
"spring"
)
public
interface
EmployeeMapper
{
EmployeeDTO
toDto
(
Employee
employee
);
Employee
toModel
(
EmployeeDTO
employeeDTO
);
}
src/main/java/com/nisum/example/mongodb/model/EmployeeDTO.java
0 → 100644
View file @
c48e453e
package
com
.
nisum
.
example
.
mongodb
.
model
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
org.springframework.context.annotation.Scope
;
import
org.springframework.context.annotation.ScopedProxyMode
;
import
org.springframework.data.annotation.Id
;
import
org.springframework.data.mongodb.core.mapping.Document
;
/**
* DTO class for Employee
* Fields id, name and salary
*/
@Scope
(
scopeName
=
"request"
,
proxyMode
=
ScopedProxyMode
.
TARGET_CLASS
)
@Document
@Data
@NoArgsConstructor
@AllArgsConstructor
public
class
EmployeeDTO
{
@Id
private
Integer
id
;
private
String
name
;
private
Long
salary
;
}
src/main/java/com/nisum/example/mongodb/repository/EmployeeRepository.java
View file @
c48e453e
package
com
.
nisum
.
example
.
mongodb
.
repository
;
import
com.nisum.example.mongodb.model.Employee
;
import
com.nisum.example.mongodb.model.EmployeeDTO
;
import
org.springframework.data.mongodb.repository.Query
;
import
org.springframework.data.mongodb.repository.ReactiveMongoRepository
;
import
reactor.core.publisher.Flux
;
public
interface
EmployeeRepository
extends
ReactiveMongoRepository
<
Employee
,
Integer
>
{
public
interface
EmployeeRepository
extends
ReactiveMongoRepository
<
Employee
DTO
,
Integer
>
{
@Query
(
"{ 'name': ?0 }"
)
Flux
<
Employee
>
findByName
(
final
String
name
);
}
src/main/java/com/nisum/example/mongodb/service/EmployeeServiceImpl.java
View file @
c48e453e
package
com
.
nisum
.
example
.
mongodb
.
service
;
import
com.nisum.example.mongodb.model.Employee
;
import
com.nisum.example.mongodb.mapper.EmployeeMapper
;
import
com.nisum.example.mongodb.model.EmployeeDTO
;
import
com.nisum.example.mongodb.repository.EmployeeRepository
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
reactor.core.publisher.Flux
;
...
...
@@ -11,21 +13,25 @@ import reactor.core.publisher.Mono;
*Service class having methods for CURD operations
* Methods having Mono<T> and Flux<T> as return types
*/
@Slf4j
@Service
public
class
EmployeeServiceImpl
implements
IEmployeeService
{
@Autowired
private
EmployeeRepository
employeeRepository
;
@Autowired
private
EmployeeMapper
employeeMapper
;
/**
* perform create operation
* @param emp
* @param emp
loyeeDTO
* @return Mono
*/
@Override
public
Mono
<
Employee
>
insert
(
Employee
emp
)
{
return
employeeRepository
.
save
(
emp
);
public
Mono
<
Employee
DTO
>
insert
(
EmployeeDTO
employeeDTO
)
{
log
.
info
(
"calling repository save method..."
);
return
employeeRepository
.
save
(
emp
loyeeDTO
);
}
/**
...
...
@@ -34,7 +40,8 @@ public class EmployeeServiceImpl implements IEmployeeService {
* @return Mono
*/
@Override
public
Mono
<
Employee
>
fetchById
(
Integer
id
)
{
public
Mono
<
EmployeeDTO
>
fetchById
(
Integer
id
)
{
log
.
info
(
"Calling repository findById..."
);
return
employeeRepository
.
findById
(
id
);
}
...
...
@@ -46,12 +53,15 @@ public class EmployeeServiceImpl implements IEmployeeService {
* @return Flux
*/
@Override
public
Flux
<
Employee
>
fetchByName
(
String
name
)
{
return
employeeRepository
.
findByName
(
name
);
public
Flux
<
EmployeeDTO
>
fetchByName
(
String
name
)
{
log
.
info
(
"Calling repository fetchByName"
);
return
employeeRepository
.
findByName
(
name
)
.
map
(
employeeMapper:
:
toDto
);
}
@Override
public
Mono
<
Employee
>
modifyById
(
Integer
id
)
{
public
Mono
<
EmployeeDTO
>
modifyById
(
Integer
id
)
{
return
employeeRepository
.
findById
(
id
);
}
...
...
@@ -60,18 +70,20 @@ public class EmployeeServiceImpl implements IEmployeeService {
* @return Flux
*/
@Override
public
Flux
<
Employee
>
fetchAll
()
{
public
Flux
<
EmployeeDTO
>
fetchAll
()
{
log
.
info
(
"Calling repository findAll() method.."
);
return
employeeRepository
.
findAll
();
}
/**
* perform modify of object
* @param emp
* @param emp
DTO
* @return updated object of type Mono
*/
@Override
public
Mono
<
Employee
>
modify
(
Employee
emp
)
{
return
employeeRepository
.
save
(
emp
);
public
Mono
<
EmployeeDTO
>
modify
(
EmployeeDTO
empDTO
)
{
log
.
info
(
"Calling repository save() method..."
);
return
employeeRepository
.
save
(
empDTO
);
}
/**
...
...
src/main/java/com/nisum/example/mongodb/service/IEmployeeService.java
View file @
c48e453e
package
com
.
nisum
.
example
.
mongodb
.
service
;
import
com.nisum.example.mongodb.model.Employee
;
import
com.nisum.example.mongodb.model.Employee
DTO
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
public
interface
IEmployeeService
{
Mono
<
Employee
>
insert
(
Employee
emp
);
Mono
<
Employee
>
fetchById
(
Integer
id
);
Flux
<
Employee
>
fetchByName
(
String
name
);
Flux
<
Employee
>
fetchAll
();
Mono
<
Employee
>
modify
(
Employee
emp
);
Mono
<
Employee
>
modifyById
(
Integer
id
);
Mono
<
Employee
DTO
>
insert
(
EmployeeDTO
emp
);
Mono
<
Employee
DTO
>
fetchById
(
Integer
id
);
Flux
<
Employee
DTO
>
fetchByName
(
String
name
);
Flux
<
Employee
DTO
>
fetchAll
();
Mono
<
Employee
DTO
>
modify
(
EmployeeDTO
emp
);
Mono
<
Employee
DTO
>
modifyById
(
Integer
id
);
Mono
<
Void
>
removeById
(
Integer
id
);
}
src/main/resources/application.properties
View file @
c48e453e
port
=
27017
dbname
=
testdb
spring.data.mongodb.database
=
testdb
spring.data.mongodb.port
=
27017
\ No newline at end of file
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