Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
White-Jotter/wj/src/main/java/com/gm/wj/controller/LibraryController.java /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
80 lines (70 sloc)
2.51 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.gm.wj.controller; | |
| import com.gm.wj.entity.Book; | |
| import com.gm.wj.result.Result; | |
| import com.gm.wj.result.ResultFactory; | |
| import com.gm.wj.service.BookService; | |
| import com.gm.wj.util.StringUtils; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.web.bind.annotation.*; | |
| import org.springframework.web.multipart.MultipartFile; | |
| import javax.validation.Valid; | |
| import java.io.File; | |
| import java.io.IOException; | |
| /** | |
| * Library controller. | |
| * | |
| * @author Evan | |
| * @date 2019/4 | |
| */ | |
| @RestController | |
| public class LibraryController { | |
| @Autowired | |
| BookService bookService; | |
| @GetMapping("/api/books") | |
| public Result listBooks() { | |
| return ResultFactory.buildSuccessResult(bookService.list()); | |
| } | |
| @PostMapping("/api/admin/content/books") | |
| public Result addOrUpdateBooks(@RequestBody @Valid Book book) { | |
| bookService.addOrUpdate(book); | |
| return ResultFactory.buildSuccessResult("修改成功"); | |
| } | |
| @PostMapping("/api/admin/content/books/delete") | |
| public Result deleteBook(@RequestBody @Valid Book book) { | |
| bookService.deleteById(book.getId()); | |
| return ResultFactory.buildSuccessResult("删除成功"); | |
| } | |
| @GetMapping("/api/search") | |
| public Result searchResult(@RequestParam("keywords") String keywords) { | |
| if ("".equals(keywords)) { | |
| return ResultFactory.buildSuccessResult(bookService.list()); | |
| } else { | |
| return ResultFactory.buildSuccessResult(bookService.Search(keywords)); | |
| } | |
| } | |
| @GetMapping("/api/categories/{cid}/books") | |
| public Result listByCategory(@PathVariable("cid") int cid) { | |
| if (0 != cid) { | |
| return ResultFactory.buildSuccessResult(bookService.listByCategory(cid)); | |
| } else { | |
| return ResultFactory.buildSuccessResult(bookService.list()); | |
| } | |
| } | |
| @PostMapping("/api/admin/content/books/covers") | |
| public String coversUpload(MultipartFile file) { | |
| String folder = "D:/workspace/img"; | |
| File imageFolder = new File(folder); | |
| File f = new File(imageFolder, StringUtils.getRandomString(6) + file.getOriginalFilename() | |
| .substring(file.getOriginalFilename().length() - 4)); | |
| if (!f.getParentFile().exists()) | |
| f.getParentFile().mkdirs(); | |
| try { | |
| file.transferTo(f); | |
| String imgURL = "http://localhost:8443/api/file/" + f.getName(); | |
| return imgURL; | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| return ""; | |
| } | |
| } | |
| } |