diff --git a/src/main/java/com/dl/officialsite/common/converter/IntegerListConverter.java b/src/main/java/com/dl/officialsite/common/converter/IntegerListConverter.java new file mode 100644 index 00000000..17393a40 --- /dev/null +++ b/src/main/java/com/dl/officialsite/common/converter/IntegerListConverter.java @@ -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, String> { + public static final String SPLIT_CHAR = ","; + + @Override + public String convertToDatabaseColumn(List intList) { + List result = new ArrayList<>(); + for(Integer i : intList) { + result.add(i.toString()); + } + return result.size() !=0 ? String.join(SPLIT_CHAR, result) : ""; + } + + @Override + public List convertToEntityAttribute(String string) { + + String [] list= string.split(SPLIT_CHAR); + List intList = new ArrayList<>(); + + for(String s : list) { + intList.add(Integer.valueOf(s)); + } + return intList; + } +} diff --git a/src/main/java/com/dl/officialsite/common/converter/LongListConverter.java b/src/main/java/com/dl/officialsite/common/converter/LongListConverter.java new file mode 100644 index 00000000..cbe302b6 --- /dev/null +++ b/src/main/java/com/dl/officialsite/common/converter/LongListConverter.java @@ -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, String> { + public static final String SPLIT_CHAR = ","; + + @Override + public String convertToDatabaseColumn(List intList) { + List result = new ArrayList<>(); + for(long i : intList) { + result.add(String.valueOf(i)); + } + return result.size() !=0 ? String.join(SPLIT_CHAR, result) : ""; + } + + @Override + public List convertToEntityAttribute(String string) { + + String [] list= string.split(SPLIT_CHAR); + List intList = new ArrayList<>(); + + for(String s : list) { + intList.add( Long.parseLong(s)); + } + return intList; + } +} diff --git a/src/main/java/com/dl/officialsite/reward/Reward.java b/src/main/java/com/dl/officialsite/reward/Reward.java index 39aa70df..feb029cb 100644 --- a/src/main/java/com/dl/officialsite/reward/Reward.java +++ b/src/main/java/com/dl/officialsite/reward/Reward.java @@ -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; @@ -49,10 +51,9 @@ public class Reward { @NotNull @Column(columnDefinition = "TEXT") - @Convert(converter = StringListConverter.class) + @Convert(converter = LongListConverter.class) private List sharingIds; - //the number of reward private Integer number; @@ -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; } diff --git a/src/main/java/com/dl/officialsite/reward/RewardController.java b/src/main/java/com/dl/officialsite/reward/RewardController.java index 1c5a3709..f45a7c0c 100644 --- a/src/main/java/com/dl/officialsite/reward/RewardController.java +++ b/src/main/java/com/dl/officialsite/reward/RewardController.java @@ -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); }