@add(/api/books/getone)
- add new api get a book's info by id
This commit is contained in:
@@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.RestController
|
||||
* 图书接口(面向普通用户)
|
||||
* 路径前缀(待定):/api/books
|
||||
*
|
||||
* 计划接口:
|
||||
* 接口:
|
||||
* - 图书列表查询(搜索)
|
||||
* - 单本图书详情
|
||||
*/
|
||||
@@ -31,4 +31,14 @@ class BookController(private val bookService: BookService, private val ipExtract
|
||||
log.info("[Book] user agent: {}, ip={}", request.getHeader("User-Agent"), ipExtractor.getRealIp(request))
|
||||
return bookService.searchBook(query)
|
||||
}
|
||||
@GetMapping("/getone")
|
||||
fun getOneBook(
|
||||
@RequestAttribute(required = false) username: String?,
|
||||
request: HttpServletRequest,
|
||||
id: Long
|
||||
): Result<Any?> {
|
||||
log.info("[Book] getOne: user={}, id={}", username ?: "guest", id)
|
||||
log.info("[Book] user agent: {}, ip={}", request.getHeader("User-Agent"), ipExtractor.getRealIp(request))
|
||||
return bookService.getOneBook(id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,4 +13,10 @@ interface BookService {
|
||||
* @return 搜索结果列表,无匹配时返回 404
|
||||
*/
|
||||
fun searchBook(query: String): Result<Any?>
|
||||
/*
|
||||
* 根据 ID 查询单本图书
|
||||
* @param id 图书 ID,必须为正整数
|
||||
* @return 图书实体,不存在时返回 404
|
||||
*/
|
||||
fun getOneBook(id: Long): Result<Any?>
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import com.msksbr.bookmgr.template.Result
|
||||
|
||||
/*
|
||||
* 图书服务实现
|
||||
* 提供图书搜索功能,按书名或作者进行模糊匹配
|
||||
* 提供图书搜索和单本图书查询功能
|
||||
*/
|
||||
@Service
|
||||
class BookServiceImpl(private val bookMapper: BookMapper) : BookService {
|
||||
@@ -32,4 +32,21 @@ class BookServiceImpl(private val bookMapper: BookMapper) : BookService {
|
||||
log.info("[Book] search: found {} results for {}", result.size, query)
|
||||
return Result.success(result)
|
||||
}
|
||||
|
||||
override fun getOneBook(id: Long): Result<Any?> {
|
||||
if (id < 1) {
|
||||
log.warn("[Book] getOne: invalid id={}", id)
|
||||
return Result.error("Invalid book ID")
|
||||
}
|
||||
val result = bookMapper.selectOne(
|
||||
QueryWrapper<Book>()
|
||||
.eq("id", id)
|
||||
)
|
||||
if (result==null) {
|
||||
log.info("[Book] getOneBook: no results for {}", id)
|
||||
return Result.notFound("Book not found")
|
||||
}
|
||||
log.info("[Book] getOne: found {}, author={}", result.name, result.author)
|
||||
return Result.success(result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user