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
1e87cd4f
Commit
1e87cd4f
authored
May 06, 2021
by
Ben Anderson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added the GET /api/promos/{id} route with validations
parent
b02db479
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
45 additions
and
3 deletions
+45
-3
PromotionsController.java
...um/ascend/promotions/controller/PromotionsController.java
+5
-3
PromotionNotFoundException.java
...cend/promotions/exception/PromotionNotFoundException.java
+7
-0
RestWebExceptionHandler.java
.../ascend/promotions/exception/RestWebExceptionHandler.java
+24
-0
PromotionRepository.java
...sum/ascend/promotions/repository/PromotionRepository.java
+3
-0
PromotionService.java
...com/nisum/ascend/promotions/service/PromotionService.java
+6
-0
No files found.
src/main/java/com/nisum/ascend/promotions/controller/PromotionsController.java
View file @
1e87cd4f
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.model.Promotion
;
import
com.nisum.ascend.promotions.repository.PromotionRepository
;
import
com.nisum.ascend.promotions.service.PromotionService
;
...
...
@@ -28,9 +29,10 @@ public class PromotionsController {
}
@GetMapping
(
"/{id}"
)
public
Mono
<
ResponseEntity
<
PromotionDto
>>
getPromotionById
(
@PathVariable
String
id
){
//TODO: get promo by ID
return
null
;
public
Mono
<
PromotionDto
>
getPromotionById
(
@PathVariable
String
id
){
return
promotionService
.
findPromoById
(
id
)
.
onErrorMap
(
throwable
->
new
PromotionNotFoundException
(
id
));
}
@PostMapping
()
...
...
src/main/java/com/nisum/ascend/promotions/exception/PromotionNotFoundException.java
0 → 100644
View file @
1e87cd4f
package
com
.
nisum
.
ascend
.
promotions
.
exception
;
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
0 → 100644
View file @
1e87cd4f
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
PromotionNotFoundException
)
{
exchange
.
getResponse
().
setStatusCode
(
HttpStatus
.
NOT_FOUND
);
// marks the response as complete and forbids writing to it
return
exchange
.
getResponse
().
setComplete
();
}
return
Mono
.
error
(
ex
);
}
}
\ No newline at end of file
src/main/java/com/nisum/ascend/promotions/repository/PromotionRepository.java
View file @
1e87cd4f
...
...
@@ -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 @
1e87cd4f
...
...
@@ -5,6 +5,7 @@ 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
{
...
...
@@ -16,6 +17,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
);
}
...
...
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