-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathBookController.java
More file actions
91 lines (75 loc) · 3.04 KB
/
BookController.java
File metadata and controls
91 lines (75 loc) · 3.04 KB
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
85
86
87
88
89
90
91
package io.github.talelin.latticy.controller.v1;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.dto.book.CreateOrUpdateBookDTO;
import io.github.talelin.latticy.model.BookDO;
import io.github.talelin.latticy.service.BookService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.Positive;
import java.util.List;
/**
* @author pedro@TaleLin
* @author Juzi@TaleLin
*/
@RestController
@RequestMapping("/v1/book")
@Validated
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("/{id}")
public BookDO getBook(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
BookDO book = bookService.getById(id);
if (book == null) {
throw new NotFoundException(10022);
}
return book;
}
@GetMapping("")
public List<BookDO> getBooks() {
return bookService.findAll();
}
@GetMapping("/search")
public List<BookDO> searchBook(@RequestParam(value = "q", required = false, defaultValue = "") String q) {
return bookService.getBookByKeyword("%" + q + "%");
}
@PostMapping("")
public CreatedVO createBook(@RequestBody @Validated CreateOrUpdateBookDTO validator) {
bookService.createBook(validator);
return new CreatedVO(12);
}
@PutMapping("/{id}")
public UpdatedVO updateBook(@PathVariable("id") @Positive(message = "{id.positive}") Integer id, @RequestBody @Validated CreateOrUpdateBookDTO validator) {
BookDO book = bookService.getById(id);
if (book == null) {
throw new NotFoundException(10022);
}
bookService.updateBook(book, validator);
return new UpdatedVO(13);
}
@DeleteMapping("/{id}")
@GroupRequired
@PermissionMeta(value = "删除图书", module = "图书")
public DeletedVO deleteBook(@PathVariable("id") @Positive(message = "{id.positive}") Integer id) {
BookDO book = bookService.getById(id);
if (book == null) {
throw new NotFoundException(10022);
}
bookService.deleteById(book.getId());
return new DeletedVO(14);
}
}