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
be80c860
Commit
be80c860
authored
May 06, 2021
by
Ben Anderson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented error handling for unique key violations
parent
06946133
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
46 additions
and
7 deletions
+46
-7
ProductController.java
.../nisum/ascend/inventory/controller/ProductController.java
+5
-4
ProductDto.java
src/main/java/com/nisum/ascend/inventory/dto/ProductDto.java
+1
-0
ResourceAlreadyExistsException.java
...d/inventory/exception/ResourceAlreadyExistsException.java
+12
-0
RestWebExceptionHandler.java
...m/ascend/inventory/exception/RestWebExceptionHandler.java
+25
-0
ProductService.java
...va/com/nisum/ascend/inventory/service/ProductService.java
+3
-3
No files found.
src/main/java/com/nisum/ascend/inventory/controller/ProductController.java
View file @
be80c860
package
com
.
nisum
.
ascend
.
inventory
.
controller
;
import
com.nisum.ascend.inventory.dto.ProductDto
;
import
com.nisum.ascend.inventory.exception.ResourceAlreadyExistsException
;
import
com.nisum.ascend.inventory.model.Product
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
...
...
@@ -9,6 +10,7 @@ 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
;
import
reactor.core.publisher.Mono
;
import
com.nisum.ascend.inventory.service.ProductService
;
...
...
@@ -19,8 +21,7 @@ public class ProductController {
ProductService
productService
;
@PostMapping
(
""
)
public
ResponseEntity
<
Mono
<
ProductDto
>>
postProduct
(
@RequestBody
Product
product
)
{
Mono
<
ProductDto
>
newProduct
=
productService
.
createProduct
(
product
);
return
new
ResponseEntity
<>(
newProduct
,
HttpStatus
.
ACCEPTED
);
public
Mono
<
ProductDto
>
postProduct
(
@RequestBody
Product
product
)
{
return
productService
.
createProduct
(
product
).
onErrorMap
(
throwable
->
new
ResourceAlreadyExistsException
());
}
}
\ No newline at end of file
src/main/java/com/nisum/ascend/inventory/dto/ProductDto.java
View file @
be80c860
...
...
@@ -18,6 +18,7 @@ public class ProductDto {
private
int
stock
;
private
PromotionDto
promotion
;
public
ProductDto
()
{}
public
static
ProductDto
generateDtoFromProduct
(
Product
product
)
{
// TODO: Fetch the product's promotion from the Promotions com.nisum.ascend.inventory.service
...
...
src/main/java/com/nisum/ascend/inventory/exception/ResourceAlreadyExistsException.java
0 → 100644
View file @
be80c860
package
com
.
nisum
.
ascend
.
inventory
.
exception
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
@ResponseStatus
(
value
=
HttpStatus
.
NOT_ACCEPTABLE
)
public
class
ResourceAlreadyExistsException
extends
RuntimeException
{
public
ResourceAlreadyExistsException
()
{
super
(
"A resource with the provided SKU already exists"
);
}
}
src/main/java/com/nisum/ascend/inventory/exception/RestWebExceptionHandler.java
0 → 100644
View file @
be80c860
package
com
.
nisum
.
ascend
.
inventory
.
exception
;
import
com.mongodb.DuplicateKeyException
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.server.ServerWebExchange
;
import
org.springframework.web.server.WebExceptionHandler
;
import
reactor.core.publisher.Mono
;
@Component
@Order
(-
2
)
class
RestWebExceptionHandler
implements
WebExceptionHandler
{
@Override
public
Mono
<
Void
>
handle
(
ServerWebExchange
exchange
,
Throwable
ex
)
{
if
(
ex
instanceof
ResourceAlreadyExistsException
)
{
exchange
.
getResponse
().
setStatusCode
(
HttpStatus
.
NOT_ACCEPTABLE
);
// marks the response as complete and forbids writing to it
return
exchange
.
getResponse
().
setComplete
();
}
return
Mono
.
error
(
ex
);
}
}
src/main/java/com/nisum/ascend/inventory/service/ProductService.java
View file @
be80c860
package
com
.
nisum
.
ascend
.
inventory
.
service
;
import
com.nisum.ascend.inventory.dto.ProductDto
;
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
;
...
...
@@ -13,9 +15,7 @@ public class ProductService {
ProductRepository
productRepository
;
public
Mono
<
ProductDto
>
createProduct
(
Product
product
)
{
return
productRepository
.
save
(
product
)
.
map
(
ProductDto:
:
generateDtoFromProduct
)
.
map
(
ProductDto:
:
generateDtoFromProduct
);
}
}
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