Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
promotions-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
promotions-service
Commits
4796cd95
Commit
4796cd95
authored
May 06, 2021
by
Julius Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
post route completed
parent
b02db479
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
57 additions
and
4 deletions
+57
-4
PromotionsController.java
...um/ascend/promotions/controller/PromotionsController.java
+4
-4
PromotionAlreadyExistsException.java
...promotions/exception/PromotionAlreadyExistsException.java
+11
-0
RestWebExceptionHandler.java
.../ascend/promotions/exception/RestWebExceptionHandler.java
+23
-0
PromotionService.java
...com/nisum/ascend/promotions/service/PromotionService.java
+6
-0
PromotionsControllerTest.java
...scend/promotions/controller/PromotionsControllerTest.java
+13
-0
No files found.
src/main/java/com/nisum/ascend/promotions/controller/PromotionsController.java
View file @
4796cd95
package
com
.
nisum
.
ascend
.
promotions
.
controller
;
import
com.nisum.ascend.promotions.dto.PromotionDto
;
import
com.nisum.ascend.promotions.exception.PromotionAlreadyExistsException
;
import
com.nisum.ascend.promotions.model.Promotion
;
import
com.nisum.ascend.promotions.repository.PromotionRepository
;
import
com.nisum.ascend.promotions.service.PromotionService
;
...
...
@@ -33,10 +34,9 @@ public class PromotionsController {
return
null
;
}
@PostMapping
()
public
Mono
<
ResponseEntity
<
PromotionDto
>>
createPromotion
(
@RequestBody
Promotion
newPromotion
){
//TODO: create promotion
return
null
;
@PostMapping
(
""
)
public
Mono
<
PromotionDto
>
createPromotion
(
@RequestBody
Promotion
newPromotion
){
return
promotionService
.
createPromotion
(
newPromotion
).
onErrorMap
(
throwable
->
new
PromotionAlreadyExistsException
());
}
@PostMapping
(
"/bulkSearch"
)
...
...
src/main/java/com/nisum/ascend/promotions/exception/PromotionAlreadyExistsException.java
0 → 100644
View file @
4796cd95
package
com
.
nisum
.
ascend
.
promotions
.
exception
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
@ResponseStatus
(
value
=
HttpStatus
.
NOT_ACCEPTABLE
)
public
class
PromotionAlreadyExistsException
extends
RuntimeException
{
public
PromotionAlreadyExistsException
(){
super
(
"promotion already existed"
);
}
}
src/main/java/com/nisum/ascend/promotions/exception/RestWebExceptionHandler.java
0 → 100644
View file @
4796cd95
package
com
.
nisum
.
ascend
.
promotions
.
exception
;
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
PromotionAlreadyExistsException
)
{
exchange
.
getResponse
().
setStatusCode
(
HttpStatus
.
NOT_ACCEPTABLE
);
return
exchange
.
getResponse
().
setComplete
();
}
return
Mono
.
error
(
ex
);
}
}
src/main/java/com/nisum/ascend/promotions/service/PromotionService.java
View file @
4796cd95
package
com
.
nisum
.
ascend
.
promotions
.
service
;
import
com.nisum.ascend.promotions.dto.PromotionDto
;
import
com.nisum.ascend.promotions.model.Promotion
;
import
com.nisum.ascend.promotions.repository.PromotionRepository
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
@Service
public
class
PromotionService
{
...
...
@@ -19,4 +21,8 @@ public class PromotionService {
public
Flux
<
PromotionDto
>
findPromotionsByProductSku
(
String
sku
){
return
promotionRepository
.
findByProductSku
(
sku
).
map
(
PromotionDto:
:
generateDtoFromPromotion
);
}
public
Mono
<
PromotionDto
>
createPromotion
(
Promotion
promotion
){
return
promotionRepository
.
save
(
promotion
).
map
(
PromotionDto:
:
generateDtoFromPromotion
);
}
}
src/test/java/com/nisum/ascend/promotions/controller/PromotionsControllerTest.java
View file @
4796cd95
...
...
@@ -14,6 +14,7 @@ import org.springframework.test.annotation.DirtiesContext;
import
org.springframework.test.context.ActiveProfiles
;
import
org.springframework.test.context.junit.jupiter.SpringExtension
;
import
org.springframework.test.web.reactive.server.WebTestClient
;
import
reactor.core.publisher.Mono
;
import
java.util.List
;
...
...
@@ -48,4 +49,16 @@ class PromotionsControllerTest {
});
});
}
@Test
void
createPromotion
(){
Promotion
promotion
=
new
Promotion
(
"50OFF"
,
"SH1234"
,
(
float
)
0.5
,
5
);
webTestClient
.
post
().
uri
(
"/api/promos"
)
.
contentType
(
MediaType
.
valueOf
(
MediaType
.
APPLICATION_JSON_VALUE
))
.
body
(
Mono
.
just
(
promotion
),
Promotion
.
class
)
.
exchange
()
.
expectStatus
().
isOk
()
.
expectBody
()
.
jsonPath
(
"$.promotionId"
,
"50OFF"
);
}
}
\ 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