| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package com.izouma.nineth.web;
- import com.izouma.nineth.domain.Collection;
- import com.izouma.nineth.dto.CollectionDTO;
- import com.izouma.nineth.dto.CreateBlindBox;
- import com.izouma.nineth.service.CollectionService;
- import com.izouma.nineth.dto.PageQuery;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.CollectionRepo;
- import com.izouma.nineth.service.LikeService;
- import com.izouma.nineth.utils.ObjUtils;
- import com.izouma.nineth.utils.SecurityUtils;
- import com.izouma.nineth.utils.excel.ExcelUtils;
- import io.swagger.annotations.ApiOperation;
- import lombok.AllArgsConstructor;
- import org.springframework.data.domain.Page;
- import org.springframework.security.access.prepost.PreAuthorize;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.List;
- @RestController
- @RequestMapping("/collection")
- @AllArgsConstructor
- public class CollectionController extends BaseController {
- private CollectionService collectionService;
- private CollectionRepo collectionRepo;
- private LikeService likeService;
- //@PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/save")
- public Collection save(@RequestBody Collection record) {
- Collection orig = collectionRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
- ObjUtils.merge(orig, record);
- return collectionRepo.save(orig);
- }
- @PostMapping("/create")
- public Collection create(@RequestBody Collection record) {
- return collectionService.create(record);
- }
- //@PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/all")
- public Page<CollectionDTO> all(@RequestBody PageQuery pageQuery) {
- return collectionService.toDTO(collectionService.all(pageQuery));
- }
- @GetMapping("/get/{id}")
- public CollectionDTO get(@PathVariable Long id) {
- return collectionService.toDTO(collectionRepo.findById(id).orElseThrow(new BusinessException("无记录")), true);
- }
- @PostMapping("/del/{id}")
- public void del(@PathVariable Long id) {
- collectionRepo.softDelete(id);
- }
- @GetMapping("/excel")
- @ResponseBody
- public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
- List<Collection> data = collectionService.all(pageQuery).getContent();
- ExcelUtils.export(response, data);
- }
- @GetMapping("/{id}/like")
- @ApiOperation("点赞")
- public void like(@PathVariable Long id) {
- likeService.like(SecurityUtils.getAuthenticatedUser().getId(), id);
- }
- @GetMapping("/{id}/unlike")
- @ApiOperation("取消点赞")
- public void unlike(@PathVariable Long id) {
- likeService.unlike(SecurityUtils.getAuthenticatedUser().getId(), id);
- }
- @GetMapping("/myLikes")
- @ApiOperation("我收藏的")
- public List<CollectionDTO> myLikes() {
- return collectionService.toDTO(collectionRepo.userLikes(SecurityUtils.getAuthenticatedUser().getId()));
- }
- @PostMapping("/createBlindBox")
- public Collection createBlindBox(@RequestBody CreateBlindBox createBlindBox) {
- return collectionService.createBlindBox(createBlindBox);
- }
- }
|