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
7155b769
Commit
7155b769
authored
May 07, 2021
by
Ben Anderson
Browse files
Options
Browse Files
Download
Plain Diff
Resolved merge conflict with AFP-58
parents
823c3fdd
67ebf5fa
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
49 additions
and
12 deletions
+49
-12
ProductController.java
.../nisum/ascend/inventory/controller/ProductController.java
+13
-2
ProductRepository.java
.../nisum/ascend/inventory/repository/ProductRepository.java
+1
-1
ProductService.java
...va/com/nisum/ascend/inventory/service/ProductService.java
+9
-9
ProductControllerTest.java
...um/ascend/inventory/controller/ProductControllerTest.java
+26
-0
No files found.
src/main/java/com/nisum/ascend/inventory/controller/ProductController.java
View file @
7155b769
...
...
@@ -8,15 +8,19 @@ import org.springframework.http.HttpStatus;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
com.nisum.ascend.inventory.service.ProductService
;
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
reactor.core.publisher.Mono
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
reactor.core.publisher.Flux
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.reactive.function.server.ServerResponse
;
import
reactor.core.publisher.Mono
;
import
com.nisum.ascend.inventory.service.ProductService
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
@CrossOrigin
(
origins
=
"*"
)
@RestController
...
...
@@ -25,6 +29,13 @@ public class ProductController {
@Autowired
ProductService
productService
;
@DeleteMapping
(
value
=
"/{sku}"
)
public
Mono
<
ResponseEntity
<
Void
>>
deleteProduct
(
@PathVariable
String
sku
)
{
return
productService
.
removeProductBySku
(
sku
)
.
map
(
res
->
ResponseEntity
.
ok
().<
Void
>
build
())
.
defaultIfEmpty
(
ResponseEntity
.
notFound
().
build
());
}
@GetMapping
(
"/{sku}"
)
public
ResponseEntity
<
Mono
<
ProductDto
>>
getProductBySku
(
@PathVariable
String
sku
)
{
Mono
<
ProductDto
>
monoProd
=
productService
.
getProductBySku
(
sku
);
...
...
src/main/java/com/nisum/ascend/inventory/repository/ProductRepository.java
View file @
7155b769
package
com
.
nisum
.
ascend
.
inventory
.
repository
;
import
com.nisum.ascend.inventory.dto.ProductDto
;
import
com.nisum.ascend.inventory.model.Product
;
import
org.springframework.data.mongodb.repository.Query
;
import
org.springframework.data.mongodb.repository.ReactiveMongoRepository
;
...
...
@@ -9,6 +10,5 @@ import reactor.core.publisher.Mono;
@Repository
public
interface
ProductRepository
extends
ReactiveMongoRepository
<
Product
,
String
>
{
Mono
<
Product
>
findBySku
(
String
sku
);
}
src/main/java/com/nisum/ascend/inventory/service/ProductService.java
View file @
7155b769
package
com
.
nisum
.
ascend
.
inventory
.
service
;
import
com.nisum.ascend.inventory.dto.ProductDto
;
import
com.nisum.ascend.inventory.exception.ResourceNotFoundException
;
import
com.nisum.ascend.inventory.model.Product
;
...
...
@@ -9,21 +8,21 @@ import org.springframework.stereotype.Service;
import
reactor.core.publisher.Flux
;
import
com.nisum.ascend.inventory.repository.ProductRepository
;
import
reactor.core.publisher.Mono
;
import
static
com
.
nisum
.
ascend
.
inventory
.
dto
.
ProductDto
.
generateDtoFromProduct
;
import
com.nisum.ascend.inventory.exception.ResourceAlreadyExistsException
;
import
com.nisum.ascend.inventory.model.Product
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.reactive.function.server.ServerResponse
;
import
reactor.core.publisher.Mono
;
import
com.nisum.ascend.inventory.repository.ProductRepository
;
@Service
public
class
ProductService
{
@Autowired
ProductRepository
productRepository
;
public
Mono
<
Product
>
removeProductBySku
(
String
sku
)
{
return
productRepository
.
findBySku
(
sku
)
.
flatMap
(
existingProduct
->
productRepository
.
delete
(
existingProduct
)
.
then
(
Mono
.
just
(
existingProduct
)))
.
switchIfEmpty
(
Mono
.
error
(
new
ResourceNotFoundException
(
HttpStatus
.
NOT_FOUND
,
"Product Not Found"
)));
}
public
Mono
<
ProductDto
>
getProductBySku
(
String
sku
){
return
productRepository
.
findBySku
(
sku
)
.
map
(
existingProduct
->
generateDtoFromProduct
(
existingProduct
))
...
...
@@ -54,4 +53,5 @@ public class ProductService {
return
productRepository
.
save
(
product
)
.
map
(
ProductDto:
:
generateDtoFromProduct
);
}
}
src/test/java/com/nisum/ascend/inventory/controller/ProductControllerTest.java
View file @
7155b769
...
...
@@ -100,6 +100,32 @@ class ProductControllerTest {
});
}
/*
If you want to do this test, make sure to insert the following in MongoDB Atlas:
,
"sku": "SH=1123",
"upc":"3d2dsd9cm",
"productName":"Blue Sweater",
"productDescription":"A Nice red sweater",
"price":23.99,
"availableStock":45,
"blockedStock":0,
"productImageUrl":"",
"brand":"SweatCo",
"category":"Sweatshirts"
*/
@Test
public
void
testDeleteProduct
(){
String
sku
=
"SH=1123"
;
webTestClient
.
delete
().
uri
(
"/api/products"
.
concat
(
"/{sku}"
),
sku
)
.
accept
(
MediaType
.
valueOf
(
MediaType
.
APPLICATION_JSON_VALUE
))
.
exchange
()
.
expectStatus
().
isOk
()
.
expectBody
(
Void
.
class
);
System
.
out
.
println
(
"Deletion Successful. Ending test..."
);
}
@Test
void
postProduct
()
{
String
sku
=
"SH="
+
UUID
.
randomUUID
();
...
...
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