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
| package cn.idea360.i18n.web;
import cn.idea360.i18n.entity.Book; import cn.idea360.i18n.service.BookService; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import java.time.LocalDate; import java.util.List; import java.util.Optional;
@RestController @RequestMapping("/books") public class BookController {
@Resource private BookService bookService; @Resource private MessageSource messageSource;
@GetMapping public List<Book> findAll() { return bookService.findAll(); }
@GetMapping("/{id}") public Optional<Book> findById(@PathVariable Long id) { return bookService.findById(id); }
@ResponseStatus(HttpStatus.CREATED) @PostMapping public Book create(@RequestBody Book book) { String title = messageSource.getMessage("user.name", null, LocaleContextHolder.getLocale()); book.setTitle(title); return bookService.save(book); }
@PutMapping public Book update(@RequestBody Book book) { return bookService.save(book); }
@ResponseStatus(HttpStatus.NO_CONTENT) @DeleteMapping("/{id}") public void deleteById(@PathVariable Long id) { bookService.deleteById(id); }
@GetMapping("/find/title/{title}") public List<Book> findByTitle(@PathVariable String title) { return bookService.findByTitle(title); }
@GetMapping("/find/date-after/{date}") public List<Book> findByPublishedDateAfter( @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) { return bookService.findByPublishedDateAfter(date); }
}
|