Skip to content

Commit c1e591b

Browse files
committed
hotfix: apis,domain,global-utils - Upsert API by refining find-or-create
1 parent 387105a commit c1e591b

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

apis/src/main/kotlin/org/yapp/apis/book/service/BookManagementService.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ class BookManagementService(
1010
private val bookDomainService: BookDomainService
1111
) {
1212
fun findOrCreateBook(request: BookCreateRequest): BookCreateResponse {
13-
val isbn = request.validIsbn()
14-
15-
val bookVO = bookDomainService.findByIsbn(isbn)
16-
return BookCreateResponse.from(bookVO)
13+
val bookInfoVO = bookDomainService.findOrCreate(
14+
request.validIsbn(),
15+
request.validTitle(),
16+
request.validAuthor(),
17+
request.validAuthor(),
18+
request.coverImageUrl,
19+
request.publicationYear,
20+
request.description
21+
)
22+
return BookCreateResponse.from(bookInfoVO)
1723
}
1824

1925
}

domain/src/main/kotlin/org/yapp/domain/book/BookDomainService.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,36 @@ class BookDomainService(
1515
return BookInfoVO.newInstance(book)
1616
}
1717

18+
fun findOrCreate(
19+
isbn: String,
20+
title: String,
21+
author: String,
22+
publisher: String,
23+
coverImageUrl: String,
24+
publicationYear: Int? = null,
25+
description: String? = null
26+
): BookInfoVO {
27+
return findByIsbnOrNull(isbn) ?: run {
28+
val newBook = Book.create(
29+
isbn = isbn,
30+
title = title,
31+
author = author,
32+
publisher = publisher,
33+
coverImageUrl = coverImageUrl,
34+
publicationYear = publicationYear,
35+
description = description
36+
)
37+
val savedBook = bookRepository.save(newBook)
38+
BookInfoVO.newInstance(savedBook)
39+
}
40+
}
41+
42+
private fun findByIsbnOrNull(isbn: String): BookInfoVO? {
43+
val book = bookRepository.findById(isbn)
44+
return book?.let { BookInfoVO.newInstance(it) }
45+
}
46+
47+
1848
fun save(
1949
isbn: String,
2050
title: String,

global-utils/src/main/kotlin/org/yapp/globalutils/validator/IsbnValidator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.yapp.globalutils.validator
22

33
object IsbnValidator {
4-
private val ISBN_REGEX = Regex("^(\\d{10}|\\d{13})$")
4+
private val ISBN_REGEX = Regex("^(.{10}|.{13})$")
55

66
fun isValidIsbn(isbn: String): Boolean {
77
return isbn.matches(ISBN_REGEX)

0 commit comments

Comments
 (0)