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
f3212ca7
Commit
f3212ca7
authored
May 10, 2021
by
Sumaiyya Burney
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'AFP-59' into 'dev'
Afp 59 See merge request
!4
parents
b7b0fe12
cdec8ee3
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
61 additions
and
5 deletions
+61
-5
PromotionsController.java
...um/ascend/promotions/controller/PromotionsController.java
+6
-3
PromotionNotFoundException.java
...cend/promotions/exception/PromotionNotFoundException.java
+11
-0
RestWebExceptionHandler.java
.../ascend/promotions/exception/RestWebExceptionHandler.java
+23
-0
PromotionRepository.java
...sum/ascend/promotions/repository/PromotionRepository.java
+3
-0
PromotionService.java
...com/nisum/ascend/promotions/service/PromotionService.java
+5
-0
PromotionsControllerTest.java
...scend/promotions/controller/PromotionsControllerTest.java
+13
-2
No files found.
src/main/java/com/nisum/ascend/promotions/controller/PromotionsController.java
View file @
f3212ca7
package
com
.
nisum
.
ascend
.
promotions
.
controller
;
import
com.nisum.ascend.promotions.dto.PromotionDto
;
import
com.nisum.ascend.promotions.exception.PromotionNotFoundException
;
import
com.nisum.ascend.promotions.exception.PromotionAlreadyExistsException
;
import
com.nisum.ascend.promotions.model.Promotion
;
import
com.nisum.ascend.promotions.repository.PromotionRepository
;
...
...
@@ -29,9 +30,11 @@ public class PromotionsController {
}
@GetMapping
(
"/{id}"
)
public
Mono
<
ResponseEntity
<
PromotionDto
>>
getPromotionById
(
@PathVariable
String
id
){
//TODO: get promo by ID
return
null
;
public
ResponseEntity
<
Mono
<
PromotionDto
>>
getPromotionById
(
@PathVariable
String
id
){
Mono
<
PromotionDto
>
responseData
=
promotionService
.
findPromoById
(
id
)
.
switchIfEmpty
(
Mono
.
error
(
new
PromotionNotFoundException
(
id
)));
return
ResponseEntity
.
ok
().
body
(
responseData
);
}
@PostMapping
(
""
)
...
...
src/main/java/com/nisum/ascend/promotions/exception/PromotionNotFoundException.java
0 → 100644
View file @
f3212ca7
package
com
.
nisum
.
ascend
.
promotions
.
exception
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
@ResponseStatus
(
value
=
HttpStatus
.
NOT_FOUND
)
public
class
PromotionNotFoundException
extends
RuntimeException
{
public
PromotionNotFoundException
(
String
id
)
{
super
(
"The promotion with the ID of "
+
id
+
" was not found."
);
}
}
src/main/java/com/nisum/ascend/promotions/exception/RestWebExceptionHandler.java
View file @
f3212ca7
package
com
.
nisum
.
ascend
.
promotions
.
exception
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.core.io.buffer.DataBuffer
;
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.Flux
;
import
reactor.core.publisher.Mono
;
import
springfox.documentation.spring.web.json.Json
;
import
java.nio.charset.StandardCharsets
;
@Component
@Order
(-
2
)
class
RestWebExceptionHandler
implements
WebExceptionHandler
{
@Override
public
Mono
<
Void
>
handle
(
ServerWebExchange
exchange
,
Throwable
ex
)
{
if
(
ex
instanceof
PromotionNotFoundException
)
{
exchange
.
getResponse
().
setStatusCode
(
HttpStatus
.
NOT_FOUND
);
DataBuffer
buffer
=
exchange
.
getResponse
().
bufferFactory
().
wrap
(
ex
.
getMessage
().
getBytes
(
StandardCharsets
.
UTF_8
));
// marks the response as complete and forbids writing to it
return
exchange
.
getResponse
().
writeWith
(
Flux
.
just
(
buffer
));
}
return
Mono
.
error
(
ex
);
}
}
@Component
@Order
(-
2
)
...
...
src/main/java/com/nisum/ascend/promotions/repository/PromotionRepository.java
View file @
f3212ca7
...
...
@@ -5,8 +5,11 @@ import com.nisum.ascend.promotions.model.Promotion;
import
org.springframework.data.mongodb.repository.ReactiveMongoRepository
;
import
org.springframework.stereotype.Repository
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
@Repository
public
interface
PromotionRepository
extends
ReactiveMongoRepository
<
Promotion
,
String
>
{
Flux
<
Promotion
>
findByProductSku
(
String
sku
);
Mono
<
Promotion
>
findByPromotionId
(
String
promoId
);
}
src/main/java/com/nisum/ascend/promotions/service/PromotionService.java
View file @
f3212ca7
...
...
@@ -18,6 +18,11 @@ public class PromotionService {
return
promotionRepository
.
findAll
().
map
(
PromotionDto:
:
generateDtoFromPromotion
);
}
public
Mono
<
PromotionDto
>
findPromoById
(
String
promoId
)
{
return
promotionRepository
.
findByPromotionId
(
promoId
)
.
map
(
PromotionDto:
:
generateDtoFromPromotion
);
}
public
Flux
<
PromotionDto
>
findPromotionsByProductSku
(
String
sku
){
return
promotionRepository
.
findByProductSku
(
sku
).
map
(
PromotionDto:
:
generateDtoFromPromotion
);
}
...
...
src/test/java/com/nisum/ascend/promotions/controller/PromotionsControllerTest.java
View file @
f3212ca7
package
com
.
nisum
.
ascend
.
promotions
.
controller
;
import
com.nisum.ascend.promotions.dto.PromotionDto
;
import
com.nisum.ascend.promotions.model.Promotion
;
import
lombok.extern.slf4j.Slf4j
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
...
...
@@ -29,16 +28,21 @@ import static org.junit.jupiter.api.Assertions.*;
class
PromotionsControllerTest
{
@Autowired
private
WebTestClient
webTestClient
;
@BeforeEach
void
setUp
()
{
}
WebTestClient
.
ResponseSpec
getPromotionWebClient
(
String
promoId
)
{
return
webTestClient
.
get
().
uri
(
"/api/promos/"
+
promoId
).
exchange
();
}
@Test
void
getAllPromotions
()
{
webTestClient
.
get
().
uri
(
"/api/promos"
).
exchange
()
.
expectStatus
().
isOk
()
.
expectHeader
().
contentType
(
MediaType
.
APPLICATION_JSON_VALUE
)
.
expectBodyList
(
PromotionDto
.
class
)
// .hasSize(2)
.
consumeWith
(
promo
->{
List
<
PromotionDto
>
promos
=
promo
.
getResponseBody
();
assert
promos
!=
null
;
...
...
@@ -51,6 +55,13 @@ class PromotionsControllerTest {
}
@Test
void
getPromotionById
()
{
getPromotionWebClient
(
"0003"
)
.
expectStatus
().
isOk
()
.
expectHeader
().
contentType
(
MediaType
.
APPLICATION_JSON_VALUE
)
.
expectBody
()
.
jsonPath
(
"promotionId"
,
"0003"
);
void
createPromotion
(){
Promotion
promotion
=
new
Promotion
(
"50OFF"
,
"SH1234"
,
(
float
)
0.5
,
5
);
webTestClient
.
post
().
uri
(
"/api/promos"
)
...
...
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