-
Notifications
You must be signed in to change notification settings - Fork 859
/
LibraryController.java
84 lines (73 loc) · 2.51 KB
/
LibraryController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.gm.wj.controller;
import com.gm.wj.pojo.Book;
import com.gm.wj.pojo.Search;
import com.gm.wj.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;
@RestController
public class LibraryController {
@Autowired
BookService bookService;
@GetMapping("/api/books")
public List<Book> listBooks() {
return bookService.list();
}
@PostMapping("/api/admin/content/books")
public Book addOrUpdateBooks(@RequestBody Book book) {
bookService.addOrUpdate(book);
return book;
}
@PostMapping("/api/admin/content/books/delete")
public void deleteBook(@RequestBody Book book) {
bookService.deleteById(book.getId());
}
@PostMapping("/api/search")
public List<Book> searchResult(@RequestBody Search s) {
if ("".equals(s.getKeywords())) {
return bookService.list();
} else {
return bookService.Search(s.getKeywords());
}
}
@GetMapping("/api/categories/{cid}/books")
public List<Book> listByCategory(@PathVariable("cid") int cid) {
if (0 != cid) {
return bookService.listByCategory(cid);
} else {
return listBooks();
}
}
@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, 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 "";
}
}
public String getRandomString(int length) {
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
}