diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserCollectDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserCollectDao.java new file mode 100644 index 0000000..5dfe209 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserCollectDao.java @@ -0,0 +1,11 @@ +package com.luoo.user.dao; + + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import com.luoo.user.pojo.UserCollect; + +public interface UserCollectDao extends JpaRepository, JpaSpecificationExecutor { + public UserCollect findByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType); + public long deleteByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType); +} diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/UserCollect.java b/luoo_user/src/main/java/com/luoo/user/pojo/UserCollect.java new file mode 100644 index 0000000..b09d5e2 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/pojo/UserCollect.java @@ -0,0 +1,60 @@ +package com.luoo.user.pojo; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +import lombok.Getter; +import lombok.Setter; + +import java.util.Date; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import com.fasterxml.jackson.annotation.JsonFormat; +import org.springframework.format.annotation.DateTimeFormat; + +import java.io.Serializable; + + +/** + * 用户收藏 + */ +@Getter +@Setter +@Entity +@Table(name="tb_user_collect") +public class UserCollect implements Serializable { + private static final long serialVersionUID = 1L; + + /** + * 收藏ID + */ + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private Integer collectId; + + /** + * 用户ID + */ + private String userId; + + /** + * 主体ID 期刊ID,歌曲ID + */ + private String objectId; + + /** + * 0:为歌曲,1:期刊 + */ + private Integer collectType; + + /** + * 收藏时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date collectTime; +} diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java b/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java index f5bab47..34426df 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java @@ -1,18 +1,45 @@ package com.luoo.user.service; +import java.util.Date; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import com.luoo.user.dao.UserCollectDao; +import com.luoo.user.dao.UserInfoDao; +import com.luoo.user.pojo.UserCollect; + +import api.StatusCode; +import enums.CollectTypeEnum; +import exception.BizException; + @Service +@Transactional public class UserCollectService { - + @Autowired + private UserCollectDao userCollectDao; public void saveCollect(String userId, String objectId, Integer collectType) { - + CollectTypeEnum collectTypeEnum = CollectTypeEnum.getByType(collectType); + if (null == collectTypeEnum) { + throw new BizException(StatusCode.VALIDATE_FAILED); + } + UserCollect dbCollect = userCollectDao.findByUserIdAndObjectIdAndCollectType(userId, objectId, collectType); + if (null!=dbCollect) { + return; + } + UserCollect userCollect = new UserCollect(); + userCollect.setUserId(userId); + userCollect.setCollectTime(new Date()); + userCollect.setObjectId(objectId); + userCollect.setCollectType(collectType); + userCollectDao.save(userCollect); } public void deleteUserCollectByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType) { - // TODO Auto-generated method stub - + userCollectDao.deleteByUserIdAndObjectIdAndCollectType(userId, objectId, collectType); } }