-
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.
MARP-1094 Refactor product table (#227)
- Loading branch information
1 parent
a72ed0f
commit a0e4988
Showing
33 changed files
with
766 additions
and
457 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
function moveProductFieldsAndCleanUp() { | ||
const products = db.getCollection("Product").find({}, { | ||
_id: 1, | ||
installationCount: 1, | ||
synchronizedInstallationCount: 1, | ||
customOrder: 1 | ||
}).toArray(); | ||
|
||
if (products.length > 0) { | ||
db.getCollection("ProductMarketplaceData").insertMany(products); | ||
print("Fields successfully moved to ProductMarketplaceData."); | ||
} else { | ||
print("No fields to move."); | ||
} | ||
|
||
const result = db.getCollection("Product").updateMany( | ||
{}, | ||
{ $unset: { installationCount: "", synchronizedInstallationCount: "", customOrder: "" } } | ||
); | ||
print(`Fields removed from ${result.modifiedCount} documents in the Product collection.`); | ||
} | ||
|
||
moveProductFieldsAndCleanUp(); |
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
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
65 changes: 65 additions & 0 deletions
65
...service/src/main/java/com/axonivy/market/controller/ProductMarketplaceDataController.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,65 @@ | ||
package com.axonivy.market.controller; | ||
|
||
import com.axonivy.market.constants.GitHubConstants; | ||
import com.axonivy.market.enums.ErrorCode; | ||
import com.axonivy.market.github.service.GitHubService; | ||
import com.axonivy.market.model.Message; | ||
import com.axonivy.market.model.ProductCustomSortRequest; | ||
import com.axonivy.market.service.ProductMarketplaceDataService; | ||
import com.axonivy.market.util.AuthorizationUtils; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.enums.ParameterIn; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.Valid; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import static com.axonivy.market.constants.RequestMappingConstants.*; | ||
import static com.axonivy.market.constants.RequestParamConstants.DESIGNER_VERSION; | ||
import static com.axonivy.market.constants.RequestParamConstants.ID; | ||
import static org.springframework.http.HttpHeaders.AUTHORIZATION; | ||
|
||
@RestController | ||
@RequestMapping(PRODUCT_MARKETPLACE_DATA) | ||
@Tag(name = "Product Marketplace Data Controller", description = "API collection to get product marketplace data") | ||
@AllArgsConstructor | ||
public class ProductMarketplaceDataController { | ||
private final GitHubService gitHubService; | ||
private final ProductMarketplaceDataService productMarketplaceDataService; | ||
|
||
@PostMapping(CUSTOM_SORT) | ||
@Operation(hidden = true) | ||
public ResponseEntity<Message> createCustomSortProducts( | ||
@RequestHeader(value = AUTHORIZATION) String authorizationHeader, | ||
@RequestBody @Valid ProductCustomSortRequest productCustomSortRequest) { | ||
String token = AuthorizationUtils.getBearerToken(authorizationHeader); | ||
gitHubService.validateUserInOrganizationAndTeam(token, GitHubConstants.AXONIVY_MARKET_ORGANIZATION_NAME, | ||
GitHubConstants.AXONIVY_MARKET_TEAM_NAME); | ||
productMarketplaceDataService.addCustomSortProduct(productCustomSortRequest); | ||
var message = new Message(ErrorCode.SUCCESSFUL.getCode(), ErrorCode.SUCCESSFUL.getHelpText(), | ||
"Custom product sort order added successfully"); | ||
return new ResponseEntity<>(message, HttpStatus.OK); | ||
} | ||
|
||
@PutMapping(INSTALLATION_COUNT_BY_ID) | ||
@Operation(summary = "Update installation count of product", | ||
description = "By default, increase installation count when click download product files by users") | ||
public ResponseEntity<Integer> syncInstallationCount( | ||
@PathVariable(ID) @Parameter(description = "Product id (from meta.json)", example = "approval-decision-utils", | ||
in = ParameterIn.PATH) String productId, | ||
@RequestParam(name = DESIGNER_VERSION, required = false) @Parameter(in = ParameterIn.QUERY, | ||
example = "v10.0.20") String designerVersion) { | ||
int result = productMarketplaceDataService.updateInstallationCountForProduct(productId, designerVersion); | ||
return new ResponseEntity<>(result, HttpStatus.OK); | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
marketplace-service/src/main/java/com/axonivy/market/entity/ProductMarketplaceData.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,30 @@ | ||
package com.axonivy.market.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
import java.io.Serial; | ||
import java.io.Serializable; | ||
|
||
import static com.axonivy.market.constants.EntityConstants.PRODUCT_MARKETPLACE_DATA; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Document(PRODUCT_MARKETPLACE_DATA) | ||
public class ProductMarketplaceData implements Serializable { | ||
@Serial | ||
private static final long serialVersionUID = -8770801879877277456L; | ||
@Id | ||
private String id; | ||
private int installationCount; | ||
private Boolean synchronizedInstallationCount; | ||
private Integer customOrder; | ||
} |
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: 12 additions & 0 deletions
12
...e/src/main/java/com/axonivy/market/repository/CustomProductMarketplaceDataRepository.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,12 @@ | ||
package com.axonivy.market.repository; | ||
|
||
public interface CustomProductMarketplaceDataRepository { | ||
|
||
int updateInitialCount(String productId, int initialCount); | ||
|
||
int increaseInstallationCount(String productId); | ||
|
||
void increaseInstallationCountForProductByDesignerVersion(String productId, String designerVersion); | ||
|
||
void checkAndInitProductMarketplaceDataIfNotExist(String productId); | ||
} |
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
9 changes: 9 additions & 0 deletions
9
...service/src/main/java/com/axonivy/market/repository/ProductMarketplaceDataRepository.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,9 @@ | ||
package com.axonivy.market.repository; | ||
|
||
import com.axonivy.market.entity.ProductMarketplaceData; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ProductMarketplaceDataRepository extends MongoRepository<ProductMarketplaceData, String>, CustomProductMarketplaceDataRepository { | ||
} |
60 changes: 60 additions & 0 deletions
60
...n/java/com/axonivy/market/repository/impl/CustomProductMarketplaceDataRepositoryImpl.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,60 @@ | ||
package com.axonivy.market.repository.impl; | ||
|
||
import com.axonivy.market.constants.MongoDBConstants; | ||
import com.axonivy.market.entity.ProductDesignerInstallation; | ||
import com.axonivy.market.entity.ProductMarketplaceData; | ||
import com.axonivy.market.repository.CustomProductMarketplaceDataRepository; | ||
import com.axonivy.market.repository.CustomRepository; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import org.springframework.data.mongodb.core.FindAndModifyOptions; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.query.Criteria; | ||
import org.springframework.data.mongodb.core.query.Query; | ||
import org.springframework.data.mongodb.core.query.Update; | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
public class CustomProductMarketplaceDataRepositoryImpl extends CustomRepository implements CustomProductMarketplaceDataRepository { | ||
|
||
final MongoTemplate mongoTemplate; | ||
|
||
@Override | ||
public int updateInitialCount(String productId, int initialCount) { | ||
Update update = new Update().inc(MongoDBConstants.INSTALLATION_COUNT, initialCount).set( | ||
MongoDBConstants.SYNCHRONIZED_INSTALLATION_COUNT, true); | ||
ProductMarketplaceData updatedProductMarketplaceData = mongoTemplate.findAndModify(createQueryById(productId), | ||
update, FindAndModifyOptions.options().returnNew(true), ProductMarketplaceData.class); | ||
return updatedProductMarketplaceData != null ? updatedProductMarketplaceData.getInstallationCount() : 0; | ||
} | ||
|
||
@Override | ||
public int increaseInstallationCount(String productId) { | ||
Update update = new Update().inc(MongoDBConstants.INSTALLATION_COUNT, 1); | ||
ProductMarketplaceData updatedProduct = mongoTemplate.findAndModify(createQueryById(productId), update, | ||
FindAndModifyOptions.options().returnNew(true), ProductMarketplaceData.class); | ||
return updatedProduct != null ? updatedProduct.getInstallationCount() : 0; | ||
} | ||
|
||
@Override | ||
public void increaseInstallationCountForProductByDesignerVersion(String productId, String designerVersion) { | ||
Update update = new Update().inc(MongoDBConstants.INSTALLATION_COUNT, 1); | ||
mongoTemplate.upsert(createQueryByProductIdAndDesignerVersion(productId, designerVersion), | ||
update, ProductDesignerInstallation.class); | ||
} | ||
|
||
@Override | ||
public void checkAndInitProductMarketplaceDataIfNotExist(String productId) { | ||
Query query = new Query(Criteria.where(MongoDBConstants.ID).is(productId)); | ||
if (!mongoTemplate.exists(query, ProductMarketplaceData.class)) { | ||
ProductMarketplaceData productMarketplaceData = new ProductMarketplaceData(); | ||
productMarketplaceData.setId(productId); | ||
mongoTemplate.insert(productMarketplaceData); | ||
} | ||
} | ||
|
||
private Query createQueryByProductIdAndDesignerVersion(String productId, String designerVersion) { | ||
return new Query(Criteria.where(MongoDBConstants.PRODUCT_ID).is(productId) | ||
.andOperator(Criteria.where(MongoDBConstants.DESIGNER_VERSION).is(designerVersion))); | ||
} | ||
} |
Oops, something went wrong.