Skip to content

Commit

Permalink
update document
Browse files Browse the repository at this point in the history
  • Loading branch information
ntqdinh-axonivy committed Jul 30, 2024
1 parent 0218782 commit bc2429f
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ProductDetailModelAssembler(ProductModelAssembler productModelAssembler)

@Override
public ProductDetailModel toModel(Product product) {
return createModel(product, null);
return createModel(product, StringUtils.EMPTY);
}

public ProductDetailModel toModel(Product product, String tag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springdoc.core.converters.models.PageableAsQueryParam;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -59,8 +61,7 @@ public class FeedbackController {

private final PagedResourcesAssembler<Feedback> pagedResourcesAssembler;

public FeedbackController(FeedbackService feedbackService, JwtService jwtService,
FeedbackModelAssembler feedbackModelAssembler, PagedResourcesAssembler<Feedback> pagedResourcesAssembler) {
public FeedbackController(FeedbackService feedbackService, JwtService jwtService, FeedbackModelAssembler feedbackModelAssembler, PagedResourcesAssembler<Feedback> pagedResourcesAssembler) {
this.feedbackService = feedbackService;
this.jwtService = jwtService;
this.feedbackModelAssembler = feedbackModelAssembler;
Expand All @@ -69,8 +70,12 @@ public FeedbackController(FeedbackService feedbackService, JwtService jwtService

@GetMapping(PRODUCT_BY_ID)
@Operation(summary = "Find feedbacks by product id with lazy loading.", description = "Get all user feedback by product id (from meta.json) with lazy loading.")
public ResponseEntity<PagedModel<FeedbackModel>> findFeedbacks(@PathVariable(ID) @Parameter(description = "Product id (from meta.json)", example = "portal",in = ParameterIn.PATH)String productId,
Pageable pageable) {
@PageableAsQueryParam
public ResponseEntity<PagedModel<FeedbackModel>> findFeedbacks(@PathVariable(ID)
@Parameter(description = "Product id (from meta.json)", example = "portal", in = ParameterIn.PATH)
String productId,
@ParameterObject
Pageable pageable) {
Page<Feedback> results = feedbackService.findFeedbacks(productId, pageable);
if (results.isEmpty()) {
return generateEmptyPagedModel();
Expand All @@ -82,31 +87,40 @@ public ResponseEntity<PagedModel<FeedbackModel>> findFeedbacks(@PathVariable(ID)

@GetMapping(BY_ID)
@Operation(summary = "Find all feedbacks by product id", description = "Get all feedbacks by product id(from meta.json) which is used in mobile viewport.")
public ResponseEntity<FeedbackModel> findFeedback(@PathVariable(ID) @Parameter(description = "Product id (from meta.json)", example = "portal",in = ParameterIn.PATH)String id) {
public ResponseEntity<FeedbackModel> findFeedback(@PathVariable(ID)
@Parameter(description = "Product id (from meta.json)", example = "portal", in = ParameterIn.PATH)
String id) {
Feedback feedback = feedbackService.findFeedback(id);
return ResponseEntity.ok(feedbackModelAssembler.toModel(feedback));
}

@GetMapping()
@Operation(summary = "Find all feedbacks by user id and product id", description="Get current user feedback on target product.")
public ResponseEntity<FeedbackModel> findFeedbackByUserIdAndProductId(@RequestParam(USER_ID) @Parameter(name = "User Id",description = "Id of current user from DB", example = "1234",in = ParameterIn.QUERY)String userId,
@RequestParam("productId") @Parameter(name = "Product Id",description = "Product id (from meta.json)", example = "portal",in = ParameterIn.QUERY)String productId) {
@Operation(summary = "Find all feedbacks by user id and product id", description = "Get current user feedback on target product.")
public ResponseEntity<FeedbackModel> findFeedbackByUserIdAndProductId(@RequestParam(USER_ID)
@Parameter(description = "Id of current user from DB", example = "1234", in = ParameterIn.QUERY)
String userId,
@RequestParam("productId")
@Parameter(description = "Product id (from meta.json)", example = "portal", in = ParameterIn.QUERY)
String productId) {
Feedback feedback = feedbackService.findFeedbackByUserIdAndProductId(userId, productId);
return ResponseEntity.ok(feedbackModelAssembler.toModel(feedback));
}

@PostMapping
@Operation(summary = "Create user feedback", description = "Save user feedback of product with their token from Github account.")
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Example request body for feedback", required = true, content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = FeedbackModel.class)))
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Example request body for feedback", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = FeedbackModel.class)))
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully created user feedback"),
@ApiResponse(responseCode = "400", description = "Invalid input")
})
public ResponseEntity<Void> createFeedback(@RequestBody @Valid FeedbackModel feedback,
@RequestHeader(value = AUTHORIZATION) String authorizationHeader) {
@ApiResponse(responseCode = "201", description = "Successfully created user feedback"),
@ApiResponse(responseCode = "401", description = "Unauthorized request")})
public ResponseEntity<Void> createFeedback(@RequestBody
@Valid
FeedbackModel feedback,
@RequestHeader(value = AUTHORIZATION)
@Parameter(description = "JWT Bearer token", example = "Bearer 123456", in = ParameterIn.HEADER)
String bearerToken) {
String token = null;
if (authorizationHeader != null && authorizationHeader.startsWith(CommonConstants.BEARER)) {
token = authorizationHeader.substring(CommonConstants.BEARER.length()).trim(); // Remove "Bearer " prefix
if (bearerToken != null && bearerToken.startsWith(CommonConstants.BEARER)) {
token = bearerToken.substring(CommonConstants.BEARER.length()).trim(); // Remove "Bearer " prefix
}

// Validate the token
Expand All @@ -118,22 +132,23 @@ public ResponseEntity<Void> createFeedback(@RequestBody @Valid FeedbackModel fee
feedback.setUserId(claims.getSubject());
Feedback newFeedback = feedbackService.upsertFeedback(feedback);

URI location = ServletUriComponentsBuilder.fromCurrentRequest().path(BY_ID).buildAndExpand(newFeedback.getId())
.toUri();
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path(BY_ID).buildAndExpand(newFeedback.getId()).toUri();

return ResponseEntity.created(location).build();
}

@Operation(summary = "Find rating information of product by id",description = "Get overall rating of product from user.")
@Operation(summary = "Find rating information of product by its id.", description = "Get overall rating of product by its id.")
@GetMapping(PRODUCT_RATING_BY_ID)
public ResponseEntity<List<ProductRating>> getProductRating(@PathVariable(ID) @Parameter(description = "Product id (from meta.json)",example = "portal", in = ParameterIn.PATH)String productId) {
public ResponseEntity<List<ProductRating>> getProductRating(@PathVariable(ID)
@Parameter(description = "Product id (from meta.json)", example = "portal", in = ParameterIn.PATH)
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);
FeedbackModel.class);
return new ResponseEntity<>(emptyPagedModel, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
import java.util.Collections;
import java.util.Map;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -39,11 +45,15 @@ public OAuth2Controller(GitHubService gitHubService, JwtService jwtService, GitH
}

@PostMapping(GIT_HUB_LOGIN)
@Operation(description = "Get rating authentication token")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully login to GitHub provider", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Map.class))),
@ApiResponse(responseCode = "400", description = "Bad Request")})
@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = Oauth2AuthorizationCode.class)))
public ResponseEntity<Map<String, String>> gitHubLogin(@RequestBody Oauth2AuthorizationCode oauth2AuthorizationCode) {
String accessToken = EMPTY;
try {
GitHubAccessTokenResponse tokenResponse = gitHubService.getAccessToken(oauth2AuthorizationCode.getCode(),
gitHubProperty);
GitHubAccessTokenResponse tokenResponse = gitHubService.getAccessToken(oauth2AuthorizationCode.getCode(), gitHubProperty);
accessToken = tokenResponse.getAccessToken();
} catch (Exception e) {
return new ResponseEntity<>(Map.of(e.getClass().getName(), e.getMessage()), HttpStatus.BAD_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.time.StopWatch;
import org.springdoc.core.annotations.ParameterObject;
import org.springdoc.core.converters.models.PageableAsQueryParam;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -58,12 +60,22 @@ public ProductController(ProductService productService, GitHubService gitHubServ
}

@GetMapping()
@PageableAsQueryParam
@Operation(summary = "Find all products", description = "Be default system will finds product by type as 'all'")
public ResponseEntity<PagedModel<ProductModel>> findProducts(@RequestParam(name = TYPE) @Parameter(name = "Type", description = "Type of product.", in = ParameterIn.QUERY,
schema = @Schema(type = "string", allowableValues = {"all", "connectors", "utilities", "solutions", "demos"})) String type,
@RequestParam(required = false, name = KEYWORD) @Parameter(name = "Keyword", description = "Keyword that exist in product's name or short description.", example = "connector",in = ParameterIn.QUERY) String keyword,
@RequestParam(name = LANGUAGE) @Parameter(name = "Language", description = "Language of product short description.", in = ParameterIn.QUERY,
schema = @Schema(allowableValues = {"en", "de"})) String language, @Parameter(name = "Pagination", description = "Pagination configuration for result set") Pageable pageable) {
public ResponseEntity<PagedModel<ProductModel>> findProducts(@RequestParam(name = TYPE)
@Parameter(description = "Type of product.", in = ParameterIn.QUERY,
schema = @Schema(type = "string", allowableValues = {"all", "connectors", "utilities", "solutions", "demos"}))
String type,
@RequestParam(required = false, name = KEYWORD)
@Parameter(description = "Keyword that exist in product's name or short description.", example = "connector", in = ParameterIn.QUERY)
String keyword,
@RequestParam(name = LANGUAGE)
@Parameter(description = "Language of product short description.", in = ParameterIn.QUERY,
schema = @Schema(allowableValues = {"en", "de"}))
String language,
@ParameterObject
Pageable pageable) {

Page<Product> results = productService.findProducts(type, keyword, language, pageable);
if (results.isEmpty()) {
return generateEmptyPagedModel();
Expand All @@ -75,8 +87,10 @@ public ResponseEntity<PagedModel<ProductModel>> findProducts(@RequestParam(name

@PutMapping(SYNC)
@Operation(hidden = true)
public ResponseEntity<Message> syncProducts(@RequestHeader(value = AUTHORIZATION) String authorizationHeader,
@RequestParam(value = RESET_SYNC, required = false) Boolean resetSync) {
public ResponseEntity<Message> syncProducts(@RequestHeader(value = AUTHORIZATION)
String authorizationHeader,
@RequestParam(value = RESET_SYNC, required = false)
Boolean resetSync) {
String token = null;
if (authorizationHeader.startsWith(CommonConstants.BEARER)) {
token = authorizationHeader.substring(CommonConstants.BEARER.length()).trim(); // Remove "Bearer " prefix
Expand Down
Loading

0 comments on commit bc2429f

Please sign in to comment.