You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
3.8 KiB
113 lines
3.8 KiB
package com.gk.study.controller;
|
|
|
|
import com.gk.study.common.APIResponse;
|
|
import com.gk.study.common.ResponeCode;
|
|
import com.gk.study.entity.Thing;
|
|
import com.gk.study.permission.Access;
|
|
import com.gk.study.permission.AccessLevel;
|
|
import com.gk.study.service.ThingService;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
@RestController
|
|
@RequestMapping("/thing")
|
|
public class ThingController {
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(ThingController.class);
|
|
|
|
@Autowired
|
|
ThingService service;
|
|
|
|
@Value("${File.uploadPath}")
|
|
private String uploadPath;
|
|
|
|
@RequestMapping(value = "/list", method = RequestMethod.GET)
|
|
public APIResponse list(String keyword, String sort, String c, String tag){
|
|
List<Thing> list = service.getThingList(keyword, sort, c, tag);
|
|
|
|
return new APIResponse(ResponeCode.SUCCESS, "查询成功", list);
|
|
}
|
|
|
|
@RequestMapping(value = "/detail", method = RequestMethod.GET)
|
|
public APIResponse detail(String id){
|
|
Thing thing = service.getThingById(id);
|
|
|
|
return new APIResponse(ResponeCode.SUCCESS, "查询成功", thing);
|
|
}
|
|
|
|
@Access(level = AccessLevel.ADMIN)
|
|
@RequestMapping(value = "/create", method = RequestMethod.POST)
|
|
@Transactional
|
|
public APIResponse create(Thing thing) throws IOException {
|
|
String url = saveThing(thing);
|
|
if(!StringUtils.isEmpty(url)) {
|
|
thing.cover = url;
|
|
}
|
|
|
|
service.createThing(thing);
|
|
return new APIResponse(ResponeCode.SUCCESS, "创建成功");
|
|
}
|
|
|
|
@Access(level = AccessLevel.ADMIN)
|
|
@RequestMapping(value = "/delete", method = RequestMethod.POST)
|
|
public APIResponse delete(String ids){
|
|
System.out.println("ids===" + ids);
|
|
// 批量删除
|
|
String[] arr = ids.split(",");
|
|
for (String id : arr) {
|
|
service.deleteThing(id);
|
|
}
|
|
return new APIResponse(ResponeCode.SUCCESS, "删除成功");
|
|
}
|
|
|
|
@Access(level = AccessLevel.ADMIN)
|
|
@RequestMapping(value = "/update", method = RequestMethod.POST)
|
|
@Transactional
|
|
public APIResponse update(Thing thing) throws IOException {
|
|
System.out.println(thing);
|
|
String url = saveThing(thing);
|
|
if(!StringUtils.isEmpty(url)) {
|
|
thing.cover = url;
|
|
}
|
|
|
|
service.updateThing(thing);
|
|
return new APIResponse(ResponeCode.SUCCESS, "更新成功");
|
|
}
|
|
|
|
public String saveThing(Thing thing) throws IOException {
|
|
MultipartFile file = thing.getImageFile();
|
|
String newFileName = null;
|
|
if(file !=null && !file.isEmpty()) {
|
|
|
|
// 存文件
|
|
String oldFileName = file.getOriginalFilename();
|
|
String randomStr = UUID.randomUUID().toString();
|
|
newFileName = randomStr + oldFileName.substring(oldFileName.lastIndexOf("."));
|
|
String filePath = uploadPath + File.separator + "image" + File.separator + newFileName;
|
|
File destFile = new File(filePath);
|
|
if(!destFile.getParentFile().exists()){
|
|
destFile.getParentFile().mkdirs();
|
|
}
|
|
file.transferTo(destFile);
|
|
}
|
|
if(!StringUtils.isEmpty(newFileName)) {
|
|
thing.cover = newFileName;
|
|
}
|
|
return newFileName;
|
|
}
|
|
|
|
}
|