Skip to content

Commit

Permalink
fix the reward save
Browse files Browse the repository at this point in the history
  • Loading branch information
liberhe committed Dec 22, 2023
1 parent 6ef1018 commit 4ff3445
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.dl.officialsite.common.converter;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.util.Collections.emptyList;

@Converter
public class IntegerListConverter implements AttributeConverter<List<Integer>, String> {
public static final String SPLIT_CHAR = ",";

@Override
public String convertToDatabaseColumn(List<Integer> intList) {
List<String> result = new ArrayList<>();
for(Integer i : intList) {
result.add(i.toString());
}
return result.size() !=0 ? String.join(SPLIT_CHAR, result) : "";
}

@Override
public List<Integer> convertToEntityAttribute(String string) {

String [] list= string.split(SPLIT_CHAR);
List<Integer> intList = new ArrayList<>();

for(String s : list) {
intList.add(Integer.valueOf(s));
}
return intList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.dl.officialsite.common.converter;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.ArrayList;
import java.util.List;

import static java.lang.String.valueOf;
import static java.util.Collections.emptyList;

@Converter
public class LongListConverter implements AttributeConverter<List<Long>, String> {
public static final String SPLIT_CHAR = ",";

@Override
public String convertToDatabaseColumn(List<Long> intList) {
List<String> result = new ArrayList<>();
for(long i : intList) {
result.add(String.valueOf(i));
}
return result.size() !=0 ? String.join(SPLIT_CHAR, result) : "";
}

@Override
public List<Long> convertToEntityAttribute(String string) {

String [] list= string.split(SPLIT_CHAR);
List<Long> intList = new ArrayList<>();

for(String s : list) {
intList.add( Long.parseLong(s));
}
return intList;
}
}
14 changes: 8 additions & 6 deletions src/main/java/com/dl/officialsite/reward/Reward.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.dl.officialsite.reward;


import com.dl.officialsite.common.converter.IntegerListConverter;
import com.dl.officialsite.common.converter.LongListConverter;
import com.dl.officialsite.common.converter.StringListConverter;
import lombok.*;
import org.hibernate.annotations.DynamicUpdate;
Expand Down Expand Up @@ -49,10 +51,9 @@ public class Reward {

@NotNull
@Column(columnDefinition = "TEXT")
@Convert(converter = StringListConverter.class)
@Convert(converter = LongListConverter.class)
private List<Long> sharingIds;


//the number of reward
private Integer number;

Expand All @@ -64,12 +65,13 @@ public class Reward {
@Column(updatable = false)
private Long createTime;

private Long totalAmount;

private Integer term;


@NotNull
private Long expireTime;

private Long totalAmount;

@NotNull
private Integer term;

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public class RewardController {


@PostMapping("/create")
public BaseResponse createRedPacket(@Valid @RequestBody Reward redPacket, @RequestParam String address) {
redPacket.setCreator(address);
Reward redPacket1 = redPacketRepository.save(redPacket);
public BaseResponse createRedPacket(@Valid @RequestBody Reward reward, @RequestParam String address) {
reward.setCreator(address);
Reward redPacket1 = redPacketRepository.save(reward);
return BaseResponse.successWithData(redPacket1);

}
Expand Down

0 comments on commit 4ff3445

Please sign in to comment.