parent
22dc74a257
commit
febe235802
@ -0,0 +1,78 @@ |
||||
package com.bupt.note.Controller; |
||||
|
||||
import com.bupt.note.Model.Collect; |
||||
import com.bupt.note.Model.Paper; |
||||
import com.bupt.note.Repository.CollectRepository; |
||||
import com.bupt.note.Repository.PaperRepository; |
||||
import com.bupt.note.ResponseData.ResponseData; |
||||
import com.bupt.note.ResponseData.ResponseDataUtil; |
||||
import com.bupt.note.dto.Page; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
@RestController |
||||
@RequestMapping(value = "/v1/api/collect") |
||||
public class CollectController { |
||||
|
||||
@Autowired |
||||
private CollectRepository collectRepository; |
||||
|
||||
@Autowired |
||||
private PaperRepository paperRepository; |
||||
|
||||
private Logger logger= LoggerFactory.getLogger(CollectController.class); |
||||
|
||||
@PostMapping("{id}") |
||||
public ResponseData collect(@PathVariable("id") Long paperId,@CookieValue("user") String username){ |
||||
if(collectRepository.existsByPaperIdAndUserName(paperId,username)){ |
||||
if(collectRepository.deleteByPaperIdAndUserName(paperId, username)>0){ |
||||
return ResponseDataUtil.buildSuccess("read.tip.cancel_collect_ok"); |
||||
}else{ |
||||
logger.error("取消收藏失败"); |
||||
return ResponseDataUtil.buildError("read.tip.cancel_collect_fail"); |
||||
} |
||||
}else{ |
||||
Collect collect=new Collect(); |
||||
collect.setPaperId(paperId); |
||||
collect.setUserName(username); |
||||
collect.setCreateTime(System.currentTimeMillis()); |
||||
collectRepository.save(collect); |
||||
if(collectRepository.existsById(collect.getId())){ |
||||
return ResponseDataUtil.buildSuccess("read.tip.collect_ok"); |
||||
}else{ |
||||
logger.error("收藏失败"); |
||||
return ResponseDataUtil.buildError("read.tip.collect_fail"); |
||||
} |
||||
} |
||||
} |
||||
|
||||
@GetMapping |
||||
public ResponseData list(@CookieValue("user") String username){ |
||||
try { |
||||
List<Collect> collects=collectRepository.findByUserName(username); |
||||
Map<Long,Collect> map=new HashMap<>(); |
||||
collects.forEach(collect -> { |
||||
map.put(collect.getPaperId(),collect); |
||||
}); |
||||
List<Paper> paperList= paperRepository.findAllById(map.keySet()); |
||||
paperList.forEach(paper -> { |
||||
paper.setHasCollect(true); |
||||
paper.setCollectTime(map.get(paper.getId()).getCreateTime()); |
||||
}); |
||||
Page<Paper> paperPage=new Page<>(); |
||||
paperPage.setData(paperList); |
||||
paperPage.setTotal((long) paperList.size()); |
||||
return ResponseDataUtil.buildSuccess(paperPage); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
logger.error(String.valueOf(e)); |
||||
return ResponseDataUtil.buildError(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,49 @@ |
||||
package com.bupt.note.Model; |
||||
|
||||
import javax.persistence.*; |
||||
|
||||
@Entity |
||||
@Table(name = "sys_collect") |
||||
public class Collect { |
||||
private Long id; |
||||
private Long paperId; |
||||
private String userName; |
||||
private Long createTime; |
||||
|
||||
@Id |
||||
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
public Long getId() { |
||||
return id; |
||||
} |
||||
|
||||
public void setId(Long id) { |
||||
this.id = id; |
||||
} |
||||
|
||||
@Column(nullable = false) |
||||
public Long getPaperId() { |
||||
return paperId; |
||||
} |
||||
|
||||
public void setPaperId(Long paperId) { |
||||
this.paperId = paperId; |
||||
} |
||||
|
||||
@Column(nullable = false) |
||||
public String getUserName() { |
||||
return userName; |
||||
} |
||||
|
||||
public void setUserName(String userName) { |
||||
this.userName = userName; |
||||
} |
||||
|
||||
@Column(nullable = false) |
||||
public Long getCreateTime() { |
||||
return createTime; |
||||
} |
||||
|
||||
public void setCreateTime(Long createTime) { |
||||
this.createTime = createTime; |
||||
} |
||||
} |
@ -0,0 +1,17 @@ |
||||
package com.bupt.note.Repository; |
||||
|
||||
import com.bupt.note.Model.Collect; |
||||
import org.springframework.data.jpa.repository.JpaRepository; |
||||
import org.springframework.data.jpa.repository.Modifying; |
||||
|
||||
import javax.transaction.Transactional; |
||||
import java.util.List; |
||||
|
||||
public interface CollectRepository extends JpaRepository<Collect, Long> { |
||||
boolean existsByPaperIdAndUserName(Long paperId, String userName); |
||||
@Modifying |
||||
@Transactional |
||||
Integer deleteByPaperIdAndUserName(Long paperId, String userName); |
||||
|
||||
List<Collect> findByUserName(String userName); |
||||
} |
@ -0,0 +1,5 @@ |
||||
package com.bupt.note.dto; |
||||
|
||||
public class CollectForm { |
||||
|
||||
} |
Loading…
Reference in new issue