Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
inventory-service
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
Ascend
inventory-service
Commits
14dde8b3
Commit
14dde8b3
authored
May 06, 2021
by
Khai Yuan Liew
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[AFP-57] Work on PUT product endpoint
parent
d321aeed
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
21 deletions
+45
-21
ProductController.java
.../nisum/ascend/inventory/controller/ProductController.java
+7
-12
ProductService.java
...va/com/nisum/ascend/inventory/service/ProductService.java
+4
-5
ProductControllerTest.java
...um/ascend/inventory/controller/ProductControllerTest.java
+34
-4
No files found.
src/main/java/com/nisum/ascend/inventory/controller/ProductController.java
View file @
14dde8b3
...
...
@@ -6,17 +6,12 @@ import com.nisum.ascend.inventory.model.Product;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.*
;
import
com.nisum.ascend.inventory.service.ProductService
;
import
reactor.core.publisher.Mono
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
reactor.core.publisher.Flux
;
import
org.springframework.web.bind.annotation.PostMapping
;
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.reactive.function.server.ServerResponse
;
...
...
@@ -41,12 +36,12 @@ public class ProductController {
return
ResponseEntity
.
ok
(
productService
.
findAllProducts
());
}
//
@PutMapping("/{sku}")
//
public Mono<ResponseEntity<Product>> updateProduct(@PathVariable String sku, @RequestBody Product product){
// return productService.updateProductBySku(sku,user
)
//
.map(updatedProduct -> ResponseEntity.ok(updatedProduct))
//
.defaultIfEmpty(ResponseEntity.badRequest().build());
//
}
@PutMapping
(
"/{sku}"
)
public
Mono
<
ResponseEntity
<
Product
>>
updateProduct
(
@PathVariable
String
sku
,
@RequestBody
Product
product
){
return
productService
.
updateProductBySku
(
sku
,
product
)
.
map
(
updatedProduct
->
ResponseEntity
.
ok
(
updatedProduct
))
.
defaultIfEmpty
(
ResponseEntity
.
badRequest
().
build
());
}
@PostMapping
(
""
)
public
Mono
<
ProductDto
>
postProduct
(
@RequestBody
Product
product
)
{
...
...
src/main/java/com/nisum/ascend/inventory/service/ProductService.java
View file @
14dde8b3
...
...
@@ -35,18 +35,17 @@ public class ProductService {
.
map
(
ProductDto:
:
generateDtoFromProduct
);
}
public
Mono
<
Product
>
updateProduct
(
String
sku
,
Product
product
){
return
productRepository
.
findBy
Id
(
sku
)
public
Mono
<
Product
>
updateProduct
BySku
(
String
sku
,
Product
product
){
return
productRepository
.
findBy
Sku
(
sku
)
.
flatMap
(
dbProduct
->
{
dbProduct
.
setUpc
(
product
.
getUpc
());
dbProduct
.
setProductName
(
product
.
getProductName
());
dbProduct
.
setProductDescription
(
product
.
getProductDescription
());
dbProduct
.
setBrand
(
product
.
getBrand
());
dbProduct
.
setCategory
(
product
.
getCategory
());
dbProduct
.
setPrice
(
product
.
getPrice
());
dbProduct
.
setAvailableStock
(
product
.
getAvailableStock
());
dbProduct
.
setBlockedStock
(
product
.
getBlockedStock
());
dbProduct
.
setProductImageUrl
(
product
.
getProductImageUrl
());
dbProduct
.
setBrand
(
product
.
getBrand
());
dbProduct
.
setCategory
(
product
.
getCategory
());
return
productRepository
.
save
(
dbProduct
);
});
}
...
...
src/test/java/com/nisum/ascend/inventory/controller/ProductControllerTest.java
View file @
14dde8b3
...
...
@@ -81,7 +81,6 @@ class ProductControllerTest {
.
jsonPath
(
"$.sku"
,
sku
);
}
@Test
void
testFindAllProducts
(){
webTestClient
.
get
()
...
...
@@ -101,9 +100,6 @@ class ProductControllerTest {
});
}
@Test
void
postProduct
()
{
String
sku
=
"SH="
+
UUID
.
randomUUID
();
...
...
@@ -118,4 +114,38 @@ class ProductControllerTest {
.
expectBody
()
.
jsonPath
(
"$.sku"
,
sku
);
}
@Test
public
void
updateProduct
(){
double
newPrice
=
49.99
;
String
sku
=
"SH=1123"
;
Product
product
=
new
Product
(
sku
,
"3d2dsd9cm"
,
"Blue Sweater"
,
"A Nice red sweater"
,
(
float
)
newPrice
,
45
,
""
,
"SweatCo"
,
"Sweatshirts"
);
webTestClient
.
put
().
uri
(
"/api/products"
.
concat
(
"/{sku}"
),
sku
)
.
contentType
(
MediaType
.
valueOf
(
MediaType
.
APPLICATION_JSON_VALUE
))
.
accept
(
MediaType
.
valueOf
(
MediaType
.
APPLICATION_JSON_VALUE
))
.
body
(
Mono
.
just
(
product
),
Product
.
class
)
.
exchange
()
.
expectStatus
().
isOk
()
.
expectBody
()
.
jsonPath
(
"$.price"
).
isEqualTo
(
newPrice
);
}
@Test
public
void
updateProduct_notFound
(){
double
newPrice
=
49.99
;
String
sku
=
"SH=2021"
;
Product
product
=
new
Product
(
sku
,
"3d2dsd9cm"
,
"Blue Sweater"
,
"A Nice red sweater"
,
(
float
)
23.99
,
45
,
""
,
"SweatCo"
,
"Sweatshirts"
);
webTestClient
.
put
().
uri
(
"/api/products"
.
concat
(
"/{sku}"
),
sku
)
.
contentType
(
MediaType
.
valueOf
(
MediaType
.
APPLICATION_JSON_VALUE
))
.
accept
(
MediaType
.
valueOf
(
MediaType
.
APPLICATION_JSON_VALUE
))
.
body
(
Mono
.
just
(
product
),
Product
.
class
)
.
exchange
()
.
expectStatus
().
isBadRequest
();
}
}
\ 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