-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/MARP-473-follow-up-for-marp-264-i…
…nstallation-frequency-providing-data
- Loading branch information
Showing
68 changed files
with
1,491 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
marketplace-service/src/main/java/com/axonivy/market/assembler/FeedbackModelAssembler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.axonivy.market.assembler; | ||
|
||
import com.axonivy.market.controller.FeedbackController; | ||
import com.axonivy.market.entity.Feedback; | ||
import com.axonivy.market.entity.User; | ||
import com.axonivy.market.exceptions.model.NotFoundException; | ||
import com.axonivy.market.model.FeedbackModel; | ||
import com.axonivy.market.service.UserService; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.hateoas.server.mvc.RepresentationModelAssemblerSupport; | ||
import org.springframework.stereotype.Component; | ||
|
||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; | ||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; | ||
|
||
@Log4j2 | ||
@Component | ||
public class FeedbackModelAssembler extends RepresentationModelAssemblerSupport<Feedback, FeedbackModel> { | ||
|
||
private final UserService userService; | ||
|
||
public FeedbackModelAssembler(UserService userService) { | ||
super(Feedback.class, FeedbackModel.class); | ||
this.userService = userService; | ||
} | ||
|
||
@Override | ||
public FeedbackModel toModel(Feedback feedback) { | ||
FeedbackModel resource = new FeedbackModel(); | ||
resource.add(linkTo(methodOn(FeedbackController.class).findFeedback(feedback.getId())) | ||
.withSelfRel()); | ||
return createResource(resource, feedback); | ||
} | ||
|
||
private FeedbackModel createResource(FeedbackModel model, Feedback feedback) { | ||
User user; | ||
try { | ||
user = userService.findUser(feedback.getUserId()); | ||
} | ||
catch (NotFoundException e) { | ||
log.warn(e.getMessage()); | ||
user = new User(); | ||
} | ||
model.setId(feedback.getId()); | ||
model.setUsername(StringUtils.isBlank(user.getName()) ? user.getUsername() : user.getName()); | ||
model.setUserAvatarUrl(user.getAvatarUrl()); | ||
model.setUserProvider(user.getProvider()); | ||
model.setProductId(feedback.getProductId()); | ||
model.setContent(feedback.getContent()); | ||
model.setRating(feedback.getRating()); | ||
model.setCreatedAt(feedback.getCreatedAt()); | ||
model.setUpdatedAt(feedback.getUpdatedAt()); | ||
return model; | ||
} | ||
|
||
} |
11 changes: 5 additions & 6 deletions
11
marketplace-service/src/main/java/com/axonivy/market/assembler/ProductModelAssembler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
marketplace-service/src/main/java/com/axonivy/market/config/MarketApiDocumentConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 3 additions & 5 deletions
8
marketplace-service/src/main/java/com/axonivy/market/config/MarketHeaderInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 6 additions & 5 deletions
11
marketplace-service/src/main/java/com/axonivy/market/config/MongoConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 5 additions & 7 deletions
12
marketplace-service/src/main/java/com/axonivy/market/controller/AppController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
marketplace-service/src/main/java/com/axonivy/market/controller/FeedbackController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.axonivy.market.controller; | ||
|
||
import com.axonivy.market.assembler.FeedbackModelAssembler; | ||
import com.axonivy.market.entity.Feedback; | ||
import com.axonivy.market.model.FeedbackModel; | ||
import com.axonivy.market.model.ProductRating; | ||
import com.axonivy.market.service.FeedbackService; | ||
import com.axonivy.market.service.JwtService; | ||
import io.jsonwebtoken.Claims; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import jakarta.validation.Valid; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.web.PagedResourcesAssembler; | ||
import org.springframework.hateoas.PagedModel; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
import static com.axonivy.market.constants.RequestMappingConstants.FEEDBACK; | ||
|
||
@RestController | ||
@RequestMapping(FEEDBACK) | ||
public class FeedbackController { | ||
|
||
private final FeedbackService feedbackService; | ||
private final JwtService jwtService; | ||
private final FeedbackModelAssembler feedbackModelAssembler; | ||
|
||
private final PagedResourcesAssembler<Feedback> pagedResourcesAssembler; | ||
|
||
public FeedbackController(FeedbackService feedbackService, JwtService jwtService, FeedbackModelAssembler feedbackModelAssembler, PagedResourcesAssembler<Feedback> pagedResourcesAssembler) { | ||
this.feedbackService = feedbackService; | ||
this.jwtService = jwtService; | ||
this.feedbackModelAssembler = feedbackModelAssembler; | ||
this.pagedResourcesAssembler = pagedResourcesAssembler; | ||
} | ||
|
||
@Operation(summary = "Find all feedbacks by product id") | ||
@GetMapping("/product/{productId}") | ||
public ResponseEntity<PagedModel<FeedbackModel>> findFeedbacks(@PathVariable("productId") String productId, Pageable pageable) { | ||
Page<Feedback> results = feedbackService.findFeedbacks(productId, pageable); | ||
if (results.isEmpty()) { | ||
return generateEmptyPagedModel(); | ||
} | ||
var responseContent = new PageImpl<>(results.getContent(), pageable, results.getTotalElements()); | ||
var pageResources = pagedResourcesAssembler.toModel(responseContent, feedbackModelAssembler); | ||
return new ResponseEntity<>(pageResources, HttpStatus.OK); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<FeedbackModel> findFeedback(@PathVariable("id") String id) { | ||
Feedback feedback = feedbackService.findFeedback(id); | ||
return ResponseEntity.ok(feedbackModelAssembler.toModel(feedback)); | ||
} | ||
|
||
@Operation(summary = "Find all feedbacks by user id and product id") | ||
@GetMapping() | ||
public ResponseEntity<FeedbackModel> findFeedbackByUserIdAndProductId( | ||
@RequestParam String userId, | ||
@RequestParam String productId) { | ||
Feedback feedback = feedbackService.findFeedbackByUserIdAndProductId(userId, productId); | ||
return ResponseEntity.ok(feedbackModelAssembler.toModel(feedback)); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<Void> createFeedback(@RequestBody @Valid Feedback feedback, @RequestHeader(value = "Authorization", required = false) String authorizationHeader) { | ||
String token = null; | ||
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) { | ||
token = authorizationHeader.substring(7); // Remove "Bearer " prefix | ||
} | ||
|
||
// Validate the token | ||
if (token == null || !jwtService.validateToken(token)) { | ||
return ResponseEntity.status(401).build(); // Unauthorized if token is missing or invalid | ||
} | ||
|
||
Claims claims = jwtService.getClaimsFromToken(token); | ||
feedback.setUserId(claims.getSubject()); | ||
Feedback newFeedback = feedbackService.upsertFeedback(feedback); | ||
|
||
URI location = ServletUriComponentsBuilder.fromCurrentRequest() | ||
.path("/{id}") | ||
.buildAndExpand(newFeedback.getId()) | ||
.toUri(); | ||
|
||
return ResponseEntity.created(location).build(); | ||
} | ||
|
||
@Operation(summary = "Find rating information of product by id") | ||
@GetMapping("/product/{productId}/rating") | ||
public ResponseEntity<List<ProductRating>> getProductRating(@PathVariable("productId") String productId) { | ||
return ResponseEntity.ok(feedbackService.getProductRatingById(productId)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private ResponseEntity<PagedModel<FeedbackModel>> generateEmptyPagedModel() { | ||
var emptyPagedModel = (PagedModel<FeedbackModel>) pagedResourcesAssembler | ||
.toEmptyModel(Page.empty(), FeedbackModel.class); | ||
return new ResponseEntity<>(emptyPagedModel, HttpStatus.OK); | ||
} | ||
} |
Oops, something went wrong.