Skip to content

Commit 1846ced

Browse files
committed
List authors along with their books
1 parent 2c906a9 commit 1846ced

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

JPATableJoins/src/main/kotlin/com/hello/bookinventory/controller/BookController.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class BookController {
4646

4747
//curl http://localhost:8080/deleteBook
4848
// Deletes a random book from the book table
49+
// Will not delete the author from the author table as orphanRemoval = false
4950
@RequestMapping ("/deleteBook")
5051
fun deleteBook () : String {
5152
val returnTriple = bookService.deleteBook ()
@@ -64,4 +65,37 @@ class BookController {
6465
fun updateBook (): String {
6566
return bookService.updateBook()
6667
}
68+
69+
//curl http://localhost:8080/listAuthors
70+
@RequestMapping ("/listAuthors")
71+
fun listAuthors (): String {
72+
val authorList : List<Author> = bookService.findAllAuthors()
73+
var outputString : String = ""
74+
for (author in authorList)
75+
{
76+
val bookList : List<Book> = author.bookList
77+
78+
if (bookList.isNotEmpty()) {
79+
logger.info (bookList.toString())
80+
outputString += "[${author.name} wrote ${bookList.toString()}]\n "
81+
}
82+
}
83+
return outputString
84+
}
85+
86+
//curl http://localhost:8080/deleteAuthor
87+
// Deletes a random author from the author table
88+
// Will not delete the author's book from the book table as orphanRemoval = false
89+
/* @RequestMapping ("/deleteAuthor")
90+
fun deleteAuthor () : String {
91+
val returnTriple = bookService.deleteAuthor ()
92+
if (returnTriple.first == true)
93+
{
94+
return "Author # ${returnTriple.second} named ${returnTriple.third} was deleted"
95+
}
96+
else {
97+
return "Author could not be deleted."
98+
}
99+
}*/
100+
67101
}

JPATableJoins/src/main/kotlin/com/hello/bookinventory/model/Author.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ data class Author (
1010

1111
var name : String = "",
1212

13-
@OneToMany(cascade = arrayOf(CascadeType.ALL), mappedBy = "author", orphanRemoval = false)
13+
@OneToMany(mappedBy = "author", orphanRemoval = false)
1414
var bookList : List<Book> = mutableListOf()
1515
){
1616
override fun toString(): String {

JPATableJoins/src/main/kotlin/com/hello/bookinventory/service/BookService.kt

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,47 @@ class BookService {
9191
else {
9292
return ("Title was not updated")
9393
}
94+
}
9495

9596

97+
fun findAllAuthors () : List<Author> {
98+
val authors = authorRepository.findAll()
99+
for (author in authors){
100+
logger.info (author.name)
101+
}
102+
return authors
96103

97104
}
98105

99-
fun findAllAuthors () : List <Author> {
100-
return authorRepository.findAll()
101-
}
106+
/*fun deleteAuthor () : KotlinTriple<Boolean, Long, String> {
107+
val authorCount = authorRepository.count()
108+
val authorToDelete : Long = Random.nextLong(1, authorCount)
109+
logger.info ("Will attempt to delete author # $authorToDelete")
110+
if (authorRepository.existsById(authorToDelete))
111+
{
112+
val author = authorRepository.findById(authorToDelete)
113+
var name : String = ""
114+
if (author.isPresent())
115+
name = author.get().name
116+
logger.info ("Title of author to be deleted : ${name}")
117+
var status : Boolean = true
118+
val bookList = authorRepository.getBookListById(authorToDelete)
119+
if (bookList.isNotEmpty())
120+
logger.info ("Author has written ${bookList.size} books.")
121+
try {
122+
authorRepository.deleteById(authorToDelete)
123+
}
124+
catch (e: Exception)
125+
{
126+
logger.error("Unable to delete author # $authorToDelete, named ${name} .")
127+
logger.error ("Exception thrown. ${e.message}")
128+
status = false
129+
}
130+
if (status == true)
131+
return KotlinTriple(true, second = authorToDelete, third = name)
132+
}
133+
return KotlinTriple(false, second = 0L, third = "null")
134+
}*/
102135

103136
fun findBooksByAuthor () : List <Book> {
104137
TODO ()

0 commit comments

Comments
 (0)