Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
spring-webflux-reactive-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
Sarika Sama
spring-webflux-reactive-mongodb
Commits
45d46a2a
Commit
45d46a2a
authored
May 22, 2023
by
Sarika Sama
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
additional operations
parent
e00c5e0d
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
76 additions
and
25 deletions
+76
-25
ProductController.java
...om/nisum/webfluxmongodb/controller/ProductController.java
+17
-1
ProductDto.java
src/main/java/com/nisum/webfluxmongodb/dto/ProductDto.java
+2
-1
Product.java
src/main/java/com/nisum/webfluxmongodb/entity/Product.java
+2
-1
ProductRepository.java
...om/nisum/webfluxmongodb/repository/ProductRepository.java
+11
-1
ProductService.java
...java/com/nisum/webfluxmongodb/service/ProductService.java
+36
-8
AppUtils.java
src/main/java/com/nisum/webfluxmongodb/utils/AppUtils.java
+4
-2
apllication.yml
src/main/resources/apllication.yml
+0
-11
application.properties
src/main/resources/application.properties
+4
-0
No files found.
src/main/java/com/nisum/webfluxmongodb/controller/ProductController.java
View file @
45d46a2a
...
@@ -4,13 +4,17 @@ package com.nisum.webfluxmongodb.controller;
...
@@ -4,13 +4,17 @@ package com.nisum.webfluxmongodb.controller;
import
com.nisum.webfluxmongodb.dto.ProductDto
;
import
com.nisum.webfluxmongodb.dto.ProductDto
;
import
com.nisum.webfluxmongodb.service.ProductService
;
import
com.nisum.webfluxmongodb.service.ProductService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
reactor.core.publisher.Mono
;
import
static
org
.
apache
.
commons
.
lang3
.
StringUtils
.
isBlank
;
@RestController
@RestController
@RequestMapping
(
"/products"
)
@RequestMapping
(
"/products"
)
public
class
ProductController
{
public
class
ProductController
{
@Autowired
@Autowired
private
ProductService
service
;
private
ProductService
service
;
...
@@ -47,5 +51,17 @@ public class ProductController {
...
@@ -47,5 +51,17 @@ public class ProductController {
}
}
@GetMapping
(
"/search"
)
public
Mono
<
ProductDto
>
getSearchAllProducts
(
@PathVariable
(
name
=
"quantity"
)
int
qty
,
@PathVariable
(
name
=
"name"
)
String
name
,
@PathVariable
(
name
=
"price"
)
Double
price
)
{
if
(
null
!=
name
||
!
isBlank
(
name
)){
return
service
.
getProductsByName
(
name
);
}
else
if
(
qty
>
0
){
return
service
.
getProductByQuantity
(
qty
);
}
return
service
.
getProductByPrice
(
price
);
}
}
}
\ No newline at end of file
src/main/java/com/nisum/webfluxmongodb/dto/ProductDto.java
View file @
45d46a2a
...
@@ -8,7 +8,8 @@ import lombok.NoArgsConstructor;
...
@@ -8,7 +8,8 @@ import lombok.NoArgsConstructor;
@Data
@Data
@AllArgsConstructor
@AllArgsConstructor
@NoArgsConstructor
@NoArgsConstructor
public
class
ProductDto
{
public
class
ProductDto
{
private
String
id
;
private
String
id
;
private
String
name
;
private
String
name
;
...
...
src/main/java/com/nisum/webfluxmongodb/entity/Product.java
View file @
45d46a2a
...
@@ -10,7 +10,8 @@ import org.springframework.data.mongodb.core.mapping.Document;
...
@@ -10,7 +10,8 @@ import org.springframework.data.mongodb.core.mapping.Document;
@AllArgsConstructor
@AllArgsConstructor
@NoArgsConstructor
@NoArgsConstructor
@Document
(
collection
=
"products"
)
@Document
(
collection
=
"products"
)
public
class
Product
{
public
class
Product
{
@Id
@Id
private
String
id
;
private
String
id
;
private
String
name
;
private
String
name
;
...
...
src/main/java/com/nisum/webfluxmongodb/repository/ProductRepository.java
View file @
45d46a2a
...
@@ -6,8 +6,18 @@ import org.springframework.data.domain.Range;
...
@@ -6,8 +6,18 @@ import org.springframework.data.domain.Range;
import
org.springframework.data.mongodb.repository.ReactiveMongoRepository
;
import
org.springframework.data.mongodb.repository.ReactiveMongoRepository
;
import
org.springframework.stereotype.Repository
;
import
org.springframework.stereotype.Repository
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
@Repository
@Repository
public
interface
ProductRepository
extends
ReactiveMongoRepository
<
Product
,
String
>
{
public
interface
ProductRepository
extends
ReactiveMongoRepository
<
Product
,
String
>
{
Flux
<
ProductDto
>
findByPriceBetween
(
Range
<
Double
>
priceRange
);
Flux
<
ProductDto
>
findByPriceBetween
(
Range
<
Double
>
priceRange
);
Mono
<
ProductDto
>
findByName
(
String
name
);
Mono
<
ProductDto
>
findByPrice
(
double
price
);
Mono
<
ProductDto
>
findByQuantity
(
int
qty
);
}
}
src/main/java/com/nisum/webfluxmongodb/service/ProductService.java
View file @
45d46a2a
...
@@ -5,46 +5,74 @@ import com.nisum.webfluxmongodb.repository.ProductRepository;
...
@@ -5,46 +5,74 @@ import com.nisum.webfluxmongodb.repository.ProductRepository;
import
com.nisum.webfluxmongodb.utils.AppUtils
;
import
com.nisum.webfluxmongodb.utils.AppUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.domain.Range
;
import
org.springframework.data.domain.Range
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
reactor.core.CoreSubscriber
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
reactor.core.publisher.Mono
;
import
java.util.List
;
@Service
@Service
public
class
ProductService
{
public
class
ProductService
{
@Autowired
@Autowired
private
ProductRepository
repository
;
private
ProductRepository
repository
;
public
Flux
<
ProductDto
>
getProducts
(){
public
Flux
<
ProductDto
>
getProducts
()
{
return
repository
.
findAll
().
map
(
AppUtils:
:
entityToDto
);
return
repository
.
findAll
().
map
(
AppUtils:
:
entityToDto
);
}
}
public
Mono
<
ProductDto
>
getProduct
(
String
id
){
public
Mono
<
ProductDto
>
getProduct
(
String
id
)
{
return
repository
.
findById
(
id
).
map
(
AppUtils:
:
entityToDto
);
return
repository
.
findById
(
id
).
map
(
AppUtils:
:
entityToDto
);
}
}
public
Flux
<
ProductDto
>
getProductInRange
(
double
min
,
double
max
){
public
Flux
<
ProductDto
>
getProductInRange
(
double
min
,
double
max
)
{
return
repository
.
findByPriceBetween
(
Range
.
closed
(
min
,
max
));
return
repository
.
findByPriceBetween
(
Range
.
closed
(
min
,
max
));
}
}
public
Mono
<
ProductDto
>
saveProduct
(
Mono
<
ProductDto
>
productDtoMono
){
public
Mono
<
ProductDto
>
saveProduct
(
Mono
<
ProductDto
>
productDtoMono
)
{
System
.
out
.
println
(
"service method called ..."
);
System
.
out
.
println
(
"service method called ..."
);
return
productDtoMono
.
map
(
AppUtils:
:
dtoToEntity
)
return
productDtoMono
.
map
(
AppUtils:
:
dtoToEntity
)
.
flatMap
(
repository:
:
insert
)
.
flatMap
(
repository:
:
insert
)
.
map
(
AppUtils:
:
entityToDto
);
.
map
(
AppUtils:
:
entityToDto
);
}
}
public
Mono
<
ProductDto
>
updateProduct
(
Mono
<
ProductDto
>
productDtoMono
,
String
id
){
public
Mono
<
ProductDto
>
updateProduct
(
Mono
<
ProductDto
>
productDtoMono
,
String
id
)
{
return
repository
.
findById
(
id
)
return
repository
.
findById
(
id
)
.
flatMap
(
p
->
productDtoMono
.
map
(
AppUtils:
:
dtoToEntity
)
.
flatMap
(
p
->
productDtoMono
.
map
(
AppUtils:
:
dtoToEntity
)
.
doOnNext
(
e
->
e
.
setId
(
id
)))
.
doOnNext
(
e
->
e
.
setId
(
id
)))
.
flatMap
(
repository:
:
save
)
.
flatMap
(
repository:
:
save
)
.
map
(
AppUtils:
:
entityToDto
);
.
map
(
AppUtils:
:
entityToDto
);
}
}
public
Mono
<
Void
>
deleteProduct
(
String
id
){
public
Mono
<
Void
>
deleteProduct
(
String
id
)
{
return
repository
.
deleteById
(
id
);
return
repository
.
deleteById
(
id
);
}
}
public
Mono
<
ProductDto
>
getProductsByName
(
@RequestParam
String
name
){
return
(
Mono
<
ProductDto
>)
repository
.
findByName
(
name
);
}
public
Mono
<
ProductDto
>
getProductByPrice
(
@RequestParam
double
price
){
return
(
repository
.
findByPrice
(
price
));
}
public
Mono
<
ProductDto
>
getProductByQuantity
(
@RequestParam
int
qty
){
return
repository
.
findByQuantity
(
qty
)
;
// Mono<ProductDto> productDtoMono = repository.findByQuantity(qty);
// Mono<ProductDto> productDtoMono1=productDtoMono.map(productDto -> productDto.getQty());
// return productDtoMono1;
}
}
}
\ No newline at end of file
src/main/java/com/nisum/webfluxmongodb/utils/AppUtils.java
View file @
45d46a2a
...
@@ -7,13 +7,15 @@ import org.springframework.beans.BeanUtils;
...
@@ -7,13 +7,15 @@ import org.springframework.beans.BeanUtils;
public
class
AppUtils
{
public
class
AppUtils
{
public
static
ProductDto
entityToDto
(
Product
product
)
{
public
static
ProductDto
entityToDto
(
Product
product
)
{
ProductDto
productDto
=
new
ProductDto
();
ProductDto
productDto
=
new
ProductDto
();
BeanUtils
.
copyProperties
(
product
,
productDto
);
BeanUtils
.
copyProperties
(
product
,
productDto
);
return
productDto
;
return
productDto
;
}
}
public
static
Product
dtoToEntity
(
ProductDto
productDto
)
{
public
static
Product
dtoToEntity
(
ProductDto
productDto
)
{
Product
product
=
new
Product
();
Product
product
=
new
Product
();
BeanUtils
.
copyProperties
(
productDto
,
product
);
BeanUtils
.
copyProperties
(
productDto
,
product
);
return
product
;
return
product
;
...
...
src/main/resources/apllication.yml
deleted
100644 → 0
View file @
e00c5e0d
server
:
port
:
9290
spring
:
data
:
mongodb
:
database
:
prod_db
host
:
localhost
port
:
27017
src/main/resources/application.properties
0 → 100644
View file @
45d46a2a
server.port
=
8088
spring.data.mongodb.host
=
localhost
spring.data.mongodb.port
=
27017
spring.data.mongodb.database
=
prod_db
\ 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