Skip to content

Commit

Permalink
add-test-endpoint-for-activity (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanmomo committed May 26, 2024
1 parent 2fcd68a commit 387733f
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.dl.officialsite.activity;

import com.dl.officialsite.activity.service.MemberTaskService;
import com.dl.officialsite.common.base.BaseResponse;
import com.dl.officialsite.common.exception.BizException;
import com.dl.officialsite.common.utils.HttpSessionUtils;
import com.dl.officialsite.config.ChainConfig;
import com.dl.officialsite.login.model.SessionUserInfo;
import com.dl.officialsite.member.Member;
import com.dl.officialsite.member.MemberRepository;
import com.dl.officialsite.member.MemberService;
import com.dl.officialsite.nft.constant.ContractNameEnum;
import com.dl.officialsite.nft.service.MemberNFTMintService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Optional;

import static com.dl.officialsite.common.enums.CodeEnums.PARAM_ERROR;

@RestController
@RequestMapping("/dev/activity/")
@Profile({"dev", "local"})
@Slf4j
public class ActivityTestController {
@Autowired
private MemberTaskService memberTaskService;
@Autowired
private MemberRepository memberRepository;
@Autowired
private MemberService memberService;
@Autowired
private MemberNFTMintService memberNFTMintService;
@Autowired
private ChainConfig chainConfig;

/**
* remove the user's activity data for re-testing
*/
@GetMapping("/clean")
public BaseResponse clean(@NotNull @RequestParam("chainId") String chainIdParam,
@RequestParam(required = false) String addressForTesting,
@RequestParam(required = false, defaultValue = "WarCraft") String contractName,
HttpSession session) {
String chainId = Arrays.stream(chainConfig.getIds()).filter(id -> StringUtils.equalsIgnoreCase(chainIdParam, id))
.findFirst()
.orElseThrow(() -> new BizException(PARAM_ERROR.getCode(), String.format("Chain id %s not exists", chainIdParam)));


SessionUserInfo sessionUserInfo = HttpSessionUtils.getMember(session);
final String address = sessionUserInfo != null ? sessionUserInfo.getAddress() : addressForTesting;

Optional<Member> memberOptional = memberRepository.findByAddress(address);
if (!memberOptional.isPresent()) {
BaseResponse.failWithReason("1001", "no user found");
}

this.memberTaskService.clean(address);

this.memberNFTMintService.clean(address, ContractNameEnum.fromValue(contractName), chainId);

this.memberService.cleanGitHubTgAndDiscordId(address);

return BaseResponse.success();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

public interface MemberTaskRecordRepository extends JpaRepository<MemberTaskRecord, Long>, JpaSpecificationExecutor<MemberTaskRecord> {

Expand All @@ -16,8 +19,14 @@ public interface MemberTaskRecordRepository extends JpaRepository<MemberTaskReco
@Query(value = "select * from member_task_record where address = :address and activity_name=:activityName and " +
"task_type=:taskType and target=:target", nativeQuery = true)
List<MemberTaskRecord> findByAddressAndActivityNameAndTaskTypeAndTarget(@Param("address") String address,
@Param("activityName") String activityName,
@Param("taskType") String taskType,
@Param("target") String target);
@Param("activityName") String activityName,
@Param("taskType") String taskType,
@Param("target") String target);

@Modifying
@Transactional
@Query(value = "delete from member_task_record where address = :address and activity_name=:activityName", nativeQuery = true)
Optional<Integer> deleteByAddressAndActivityName(@Param("address") String address,
@Param("activityName") String activityName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public List<MemberTaskStatus> fetchMemberTaskStatusList() {

return taskTypeEnumListMap.entrySet().stream().flatMap(entry ->
entry.getValue().stream().map(task -> new MemberTaskStatus(entry.getKey(), task.getName(), task.getTarget(),
task.getTargetUrl()))
task.getTargetUrl(), task.getDescription()))
).collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public class Task {
private String name;
private String target;
private String targetUrl;
private String description;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ public class MemberTaskStatus {
private String taskName;
private String target;
private String targetUrl;
private String description;

private boolean requiredAuthorization = true;
private boolean finished = false;

public MemberTaskStatus() {
}

public MemberTaskStatus(TaskTypeEnum taskType, String taskName, String target, String targetUrl) {
public MemberTaskStatus(TaskTypeEnum taskType, String taskName, String target, String targetUrl, String description) {
this.taskType = taskType;
this.taskName = taskName;
this.target = target;
this.targetUrl = targetUrl;
this.description = description;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public List<MemberTaskStatus> getMemberTasksStatusByAddress(String address, Opti
return memberTaskStatuses;
}

public void clean(String address) {
int removedCount = this.memberTaskRecordRepository.deleteByAddressAndActivityName(address, activityConfig.getName()).orElse(0);
log.info("Remove {} task records for address {}", removedCount, address);
}

private Optional<MemberTaskRecord> filter(List<MemberTaskRecord> memberTaskRecordList, TaskTypeEnum taskTypeEnum, String target) {
return memberTaskRecordList.stream()
.filter(record -> taskTypeEnum.equals(record.getTaskType()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -19,4 +20,10 @@ public interface MemberRepository extends JpaRepository<Member, Long>, JpaSpecif
Optional<Member> findByNickName(@Param("nickName") String nickName);

List<Member> findByIdIn(List<Long> ids);

@Modifying
@javax.transaction.Transactional
@Query(value = "update member set github_id=NULL, discord_id=NULL, telegram_user_id=NULL where address = :address", nativeQuery =
true)
void removeGitHubTgAndDiscordId(@Param("address") String address);
}
5 changes: 5 additions & 0 deletions src/main/java/com/dl/officialsite/member/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public MemberVo save(Member member) {

}

public void cleanGitHubTgAndDiscordId(String address) {
memberRepository.removeGitHubTgAndDiscordId(address);
log.info("Remove GitHub id, Telegram user id and Discord id to user: {}", address);
}

public MemberVo getMemberPrivacyInfo(String address) {
Optional<Member> member = memberRepository.findByAddress(address);
if (!member.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.util.Optional;

public interface MemberNFTMintRecordRepository
Expand All @@ -15,4 +17,12 @@ public interface MemberNFTMintRecordRepository
Optional<MemberNFTMintRecord> findByAddressAndContractNameAndChainId(@Param("address") String address,
@Param("contractName") String contractName,
@Param("chainId") String chainId);

@Modifying
@Transactional
@Query(value = "delete from member_nft_mint_record where address =:address and contract_name=:contractName and " +
"chain_id=:chainId", nativeQuery = true)
Optional<Integer> deleteByAddressAndContractNameAndChainId(@Param("address") String address,
@Param("contractName") String contractName,
@Param("chainId") String chainId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.dl.officialsite.nft.service;

import com.dl.officialsite.nft.bean.MemberNFTMintRecordRepository;
import com.dl.officialsite.nft.constant.ContractNameEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MemberNFTMintService {
@Autowired
private MemberNFTMintRecordRepository memberNFTMintRecordRepository;

public void clean(String address, ContractNameEnum contractName, String chainId) {
int removedCount = this.memberNFTMintRecordRepository.deleteByAddressAndContractNameAndChainId(address,
contractName.name(),
chainId).orElse(0);
log.info("Remove {} NFT mint record(s) for address {} and contract {}", removedCount, address, contractName.name());
}

}

0 comments on commit 387733f

Please sign in to comment.