Skip to content

Commit

Permalink
fix the redpacket
Browse files Browse the repository at this point in the history
  • Loading branch information
liberhe committed Dec 18, 2023
1 parent 978ebe7 commit 534ec35
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/dl/officialsite/login/filter/LoginFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
}


String addressInHeader = request.getParameter("address");


//Must login
if(!HttpSessionUtils.isUserLogin(request.getSession())){
dumpError(response);
Expand All @@ -74,6 +77,11 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
//Since logon, put a temporary user data
SessionUserInfo sessionUserInfo = HttpSessionUtils.getMember(request.getSession());
UserPrincipleData userPrinciple = new UserPrincipleData();

if(!sessionUserInfo.getAddress().equals(addressInHeader)) {
dumpForbidden(response);
return;
}
userPrinciple.setAddress(sessionUserInfo.getAddress());
List<TeamMember> teams = loadTeams(sessionUserInfo.getAddress());
userPrinciple.setTeams(teams);
Expand Down Expand Up @@ -106,4 +114,13 @@ private void dumpError(HttpServletResponse response) throws IOException{
}


private void dumpForbidden(HttpServletResponse response) throws IOException{
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
ObjectMapper objectMapper = new ObjectMapper();
out.write( objectMapper.writeValueAsString( BaseResponse.failWithReason("2002", "not forbidden to query others")));
out.flush();
out.close();
}

}
4 changes: 3 additions & 1 deletion src/main/java/com/dl/officialsite/redpacket/RedPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ public class RedPacket {
@NotNull
private String chainId;

//0 uncompleted 1 completed
//0 uncompleted 1 completed 2 异常
private Integer status;

private Long amount;

//usdc or dai
private String token;
@Column(columnDefinition = "TEXT")

@Convert(converter = StringListConverter.class)
private List<String> claimedAddress;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public BaseResponse createRedPacket(@Valid @RequestBody RedPacket redPacket, @Re
}

@RequestMapping(value = "/query", method = RequestMethod.GET)
BaseResponse getMemberById(@RequestParam String id, @RequestParam String address) {
BaseResponse getRedpacketById(@RequestParam String id, @RequestParam String address) {

Optional<RedPacket> redPacket = redPacketRepository.findById(id);
if(!redPacket.isPresent()){
Expand All @@ -72,10 +72,10 @@ BaseResponse getRedPacketAll(@RequestParam String address,
@RequestMapping(value = "/query/user", method = RequestMethod.GET)
BaseResponse getRedPacketByAddress(@RequestParam String address, @RequestParam(required = false) Integer status) {
List<RedPacket> result;
if(status!= null) {
result = redPacketRepository.findByAddressAndStatus("%" + address + "%", status);
if(status == 0) {
result = redPacketRepository.findByUnclaimedPacket("%" + address + "%");
} else {
result = redPacketRepository.findByAddress("%" + address + "%");
result = redPacketRepository.findByClaimedPacket("%" + address + "%");

}
return BaseResponse.successWithData(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ public interface RedPacketRepository extends JpaRepository<RedPacket, String>,
List<RedPacket> findByAddressAndStatus(@Param("address") String address, @Param("status") Integer status);


@Query(value = "select * from red_packet where address_list like ?1 and claimed_address not like ?1 and status = ?2", nativeQuery = true)
List<RedPacket> findByUnclaimedPacket(@Param("address") String address, @Param("status") Integer status);
@Query(value = "select * from red_packet where claimed_address like ?1 ", nativeQuery = true)
List<RedPacket> findByClaimedPacket(@Param("address") String address);



@Query(value = "select * from red_packet where address_list like ?1 and claimed_address not like ?1 ", nativeQuery = true)
List<RedPacket> findByUnclaimedPacket(@Param("address") String address);


RedPacket findByIdAndStatus(@Param("id") String id, @Param("status") Integer status);
Expand Down
62 changes: 60 additions & 2 deletions src/main/java/com/dl/officialsite/redpacket/RedPacketService.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void updateRedpacketStatus() throws IOException {
String creationTimeGtValue = String.valueOf(currentTimeMillis / 1000 - 3600*24*90);

String graphQL = "\" {" +
" redpackets {" +
" redpackets (where: { creationTime_gt: "+ creationTimeGtValue + " }) {" +
" id " +
" hasRefundedOrAllClaimed " +
" claimers {" +
Expand All @@ -65,7 +65,7 @@ public void updateRedpacketStatus() throws IOException {

if (entity != null) {
String jsonResponse = EntityUtils.toString(entity);
log.info("response from the graph: "+ jsonResponse);
log.info("response from the graph: "+ jsonResponse.substring(0,30));
JsonObject jsonObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
JsonObject data = jsonObject.getAsJsonObject("data");
JsonArray redpacketsArray = data.getAsJsonArray("redpackets");
Expand Down Expand Up @@ -122,6 +122,64 @@ public void updateRedpacketStatus() throws IOException {
}
}
}





// @Scheduled(cron = "0 0/5 * * * ? ")
// public void syncNotSavingDBRedpacket() throws IOException {
// log.info("schedule task begin --------------------- ");
// HttpPost request = new HttpPost("http://api.studio.thegraph.com/proxy/55957/dapp-learning-redpacket/version/latest");
// request.setHeader("Content-Type", "application/json");
// // Define your GraphQL query
// long currentTimeMillis = System.currentTimeMillis();
// String creationTimeGtValue = String.valueOf(currentTimeMillis / 1000 - 3600*24*7);
//
// String graphQL = "\" {" +
// " redpackets (where: { creationTime_gt: "+ creationTimeGtValue + " }) {" +
// " id " +
// " hasRefundedOrAllClaimed " +
// " claimers {" +
// " claimer" +
// " }" +
// " }" +
// "}\"";
//
//
// String query = "{ \"query\": " +
// graphQL +
// " }";
//
// request.setEntity(new StringEntity(query));
// HttpResponse response = httpClient.execute(request);
// HttpEntity entity = response.getEntity();
//
// if (entity != null) {
// String jsonResponse = EntityUtils.toString(entity);
// JsonObject jsonObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
// JsonObject data = jsonObject.getAsJsonObject("data");
// JsonArray redpacketsArray = data.getAsJsonArray("redpackets");
//
// // log.info("redpacket array : " + redpacketsArray.get(0));
// List<RedPacket> redPacketList = redPacketRepository.findAll();
//
// for (int i = 0; i < redpacketsArray.size(); i++) {
//
// JsonObject redpacketObject = redpacketsArray.get(i).getAsJsonObject();
// String id = redpacketObject.get("id").getAsString();
// for (int j = 0; j < redPacketList.size(); j++) {
//
// RedPacket redPacket = redPacketList.get(j);
// if (redPacketList.get(j).getId().toLowerCase().equals(id.toLowerCase()))
// continue;
// todo
// //redPacketRepository.save(redPacket);
//
// }
// }
// }
// }
}


Expand Down

0 comments on commit 534ec35

Please sign in to comment.