Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
Spring-Webflux-Reactive-Mongodb-Curd-App
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-Webflux-Reactive-Mongodb-Curd-App
Commits
bd6704cc
Commit
bd6704cc
authored
Mar 18, 2021
by
Syed Javed Ali
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added ProductRepository and CustomProductRepository
parent
ae4e5b43
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
102 additions
and
33 deletions
+102
-33
ProductController.java
.../java/com/nisum/example/controller/ProductController.java
+15
-5
CustomProductRepository.java
...com/nisum/example/repository/CustomProductRepository.java
+11
-0
CustomProductRepositoryImpl.java
...nisum/example/repository/CustomProductRepositoryImpl.java
+42
-0
ProductRepository.java
.../java/com/nisum/example/repository/ProductRepository.java
+9
-0
IProductService.java
src/main/java/com/nisum/example/service/IProductService.java
+2
-1
ProductServiceImpl.java
...in/java/com/nisum/example/service/ProductServiceImpl.java
+23
-27
No files found.
src/main/java/com/nisum/example/controller/ProductController.java
View file @
bd6704cc
...
...
@@ -65,18 +65,28 @@ public class ProductController {
@DeleteMapping
(
value
=
"/product-delete/{id}"
,
produces
=
MediaType
.
APPLICATION_JSON_VALUE
)
@ResponseStatus
(
HttpStatus
.
OK
)
public
Mono
<
Boolean
>
deleteById
(
@PathVariable
String
id
){
public
Mono
<
Boolean
>
delete
Product
ById
(
@PathVariable
String
id
){
log
.
info
(
"Calling service removeById()..."
);
return
productService
.
removeById
(
id
)
.
log
();
}
@PutMapping
(
"/product-update-price
/{id}
"
)
@PutMapping
(
"/product-update-price"
)
@ResponseStatus
(
HttpStatus
.
OK
)
public
Mono
<
Product
>
modifyProductPrice
(
@PathVariable
String
id
,
@Valid
@RequestBody
Product
product
){
public
Mono
<
Product
>
modifyProductPrice
(
@RequestParam
(
"prodId"
)
String
id
,
@RequestParam
(
"prodPrice"
)
Double
prodPrice
){
log
.
info
(
"prodId:"
+
id
+
" "
+
"prodPrice "
+
prodPrice
);
log
.
info
(
"Calling service updateProductPrice()..."
);
return
productService
.
updateProductPrice
(
id
,
productMapper
.
toDto
(
product
))
return
productService
.
updateProductPrice
(
id
,
prodPrice
)
.
map
(
productMapper:
:
toModel
)
.
log
();
}
@GetMapping
(
"/product-get-name/{name}"
)
@ResponseStatus
(
HttpStatus
.
OK
)
public
Flux
<
Product
>
fetchProductByName
(
@PathVariable
(
"name"
)
String
prodName
){
log
.
info
(
"Calling service getProductByName()..."
);
return
productService
.
getProductByName
(
prodName
)
.
map
(
productMapper:
:
toModel
)
.
log
();
...
...
src/main/java/com/nisum/example/repository/CustomProductRepository.java
0 → 100644
View file @
bd6704cc
package
com
.
nisum
.
example
.
repository
;
import
com.nisum.example.dto.ProductDTO
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
public
interface
CustomProductRepository
{
Mono
<
ProductDTO
>
changeProductPrice
(
String
id
,
Double
newPrice
);
Mono
<
Boolean
>
clearProductById
(
String
id
);
Flux
<
ProductDTO
>
pickProductByName
(
String
prodName
);
}
src/main/java/com/nisum/example/repository/CustomProductRepositoryImpl.java
0 → 100644
View file @
bd6704cc
package
com
.
nisum
.
example
.
repository
;
import
com.nisum.example.dto.ProductDTO
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.mongodb.core.ReactiveMongoTemplate
;
import
org.springframework.data.mongodb.core.query.Criteria
;
import
org.springframework.data.mongodb.core.query.Query
;
import
org.springframework.data.mongodb.core.query.Update
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
@Slf4j
public
class
CustomProductRepositoryImpl
implements
CustomProductRepository
{
@Autowired
private
ReactiveMongoTemplate
reactiveMongoTemplate
;
//TODO result not as expected
@Override
public
Mono
<
ProductDTO
>
changeProductPrice
(
String
id
,
Double
newPrice
)
{
Query
query
=
new
Query
(
Criteria
.
where
(
"prodId"
).
is
(
id
));
Update
update
=
new
Update
().
set
(
"prodPrice"
,
newPrice
);
log
.
info
(
"Calling findandModify()...."
);
return
reactiveMongoTemplate
.
findAndModify
(
query
,
update
,
ProductDTO
.
class
);
}
@Override
public
Mono
<
Boolean
>
clearProductById
(
String
id
)
{
return
reactiveMongoTemplate
.
remove
(
Query
.
query
(
Criteria
.
where
(
"prodId"
).
is
(
id
)),
ProductDTO
.
class
)
.
flatMap
(
deleteResult
->
Mono
.
just
(
deleteResult
.
wasAcknowledged
()));
}
@Override
public
Flux
<
ProductDTO
>
pickProductByName
(
String
prodName
)
{
log
.
info
(
"Calling template find()..."
);
return
reactiveMongoTemplate
.
find
(
Query
.
query
(
Criteria
.
where
(
"prodName"
).
is
(
prodName
)),
ProductDTO
.
class
);
}
}
src/main/java/com/nisum/example/repository/ProductRepository.java
0 → 100644
View file @
bd6704cc
package
com
.
nisum
.
example
.
repository
;
import
com.nisum.example.dto.ProductDTO
;
import
org.springframework.data.mongodb.repository.ReactiveMongoRepository
;
import
org.springframework.stereotype.Repository
;
@Repository
public
interface
ProductRepository
extends
ReactiveMongoRepository
<
ProductDTO
,
String
>,
CustomProductRepository
{
}
src/main/java/com/nisum/example/service/IProductService.java
View file @
bd6704cc
...
...
@@ -11,5 +11,6 @@ public interface IProductService {
Flux
<
ProductDTO
>
getProducts
();
Mono
<
ProductDTO
>
updateProduct
(
ProductDTO
productDTO
);
Mono
<
Boolean
>
removeById
(
String
id
);
Mono
<
ProductDTO
>
updateProductPrice
(
String
id
,
ProductDTO
productDTO
);
Mono
<
ProductDTO
>
updateProductPrice
(
String
id
,
Double
prodPrice
);
Flux
<
ProductDTO
>
getProductByName
(
String
prodName
);
}
src/main/java/com/nisum/example/service/ProductServiceImpl.java
View file @
bd6704cc
package
com
.
nisum
.
example
.
service
;
import
com.nisum.example.dto.ProductDTO
;
import
com.nisum.example.repository.ProductRepository
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.mongodb.core.ReactiveMongoTemplate
;
import
org.springframework.data.mongodb.core.query.Criteria
;
import
org.springframework.data.mongodb.core.query.Query
;
import
org.springframework.data.mongodb.core.query.Update
;
import
org.springframework.stereotype.Service
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
...
...
@@ -21,6 +19,9 @@ public class ProductServiceImpl implements IProductService {
@Autowired
private
ReactiveMongoTemplate
reactiveMongoTemplate
;
@Autowired
private
ProductRepository
productRepository
;
/**
*Perform save operation of one product
* @param productDTO
...
...
@@ -28,8 +29,8 @@ public class ProductServiceImpl implements IProductService {
*/
@Override
public
Mono
<
ProductDTO
>
saveProduct
(
ProductDTO
productDTO
)
{
log
.
info
(
"Calling
template
save()..."
);
return
reactiveMongoTemplate
.
save
(
productDTO
);
log
.
info
(
"Calling
repository
save()..."
);
return
productRepository
.
save
(
productDTO
);
}
/**
...
...
@@ -39,42 +40,37 @@ public class ProductServiceImpl implements IProductService {
*/
@Override
public
Mono
<
ProductDTO
>
getProduct
(
String
id
)
{
log
.
info
(
"Calling
template
findById()..."
);
return
reactiveMongoTemplate
.
findById
(
id
,
ProductDTO
.
class
);
log
.
info
(
"Calling
repository
findById()..."
);
return
productRepository
.
findById
(
id
);
}
@Override
public
Flux
<
ProductDTO
>
getProducts
()
{
log
.
info
(
"Calling
template
findAll()..."
);
return
reactiveMongoTemplate
.
findAll
(
ProductDTO
.
class
);
log
.
info
(
"Calling
repository
findAll()..."
);
return
productRepository
.
findAll
(
);
}
@Override
public
Mono
<
ProductDTO
>
updateProduct
(
ProductDTO
productDTO
)
{
log
.
info
(
"Calling
template
save()..."
);
return
reactiveMongoTemplate
.
save
(
productDTO
);
log
.
info
(
"Calling
repository
save()..."
);
return
productRepository
.
save
(
productDTO
);
}
@Override
public
Mono
<
Boolean
>
removeById
(
String
id
)
{
log
.
info
(
"Calling template remove()..."
);
return
reactiveMongoTemplate
.
remove
(
Query
.
query
(
Criteria
.
where
(
"id"
).
is
(
id
)),
ProductDTO
.
class
)
.
flatMap
(
deleteResult
->
Mono
.
just
(
deleteResult
.
wasAcknowledged
()));
log
.
info
(
"Calling repository clearById()..."
);
return
productRepository
.
clearProductById
(
id
);
}
@Override
public
Mono
<
ProductDTO
>
updateProductPrice
(
String
id
,
Double
prodPrice
)
{
log
.
info
(
"Calling repository changePrice()..."
);
return
productRepository
.
changeProductPrice
(
id
,
prodPrice
);
}
@Override
public
Mono
<
ProductDTO
>
updateProductPrice
(
String
id
,
ProductDTO
productDTO
)
{
log
.
info
(
"Calling template findAndModify()..."
);
return
reactiveMongoTemplate
.
findById
(
id
,
ProductDTO
.
class
)
.
flatMap
(
productDto
->
reactiveMongoTemplate
.
findAndModify
(
Query
.
query
(
Criteria
.
where
(
"id"
).
is
(
id
)),
Update
.
update
(
"prodPrice"
,
productDTO
.
getProdPrice
()),
ProductDTO
.
class
)
.
flatMap
(
result
->
{
result
.
setProdPrice
(
productDto
.
getProdPrice
());
return
Mono
.
just
(
result
);
}));
public
Flux
<
ProductDTO
>
getProductByName
(
String
prodName
)
{
log
.
info
(
"Calling repository pickProductByName()..."
);
return
productRepository
.
pickProductByName
(
prodName
);
}
}
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