Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit ac11469

Browse files
committed
+ Disconnect gracefully menu option
+ Comment
1 parent 63f7432 commit ac11469

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed

src/main/kotlin/Environment.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Environment is a class that contains variables that are used in the application
3+
*/
14
class Environment {
25
companion object {
36
val url by lazy { System.getenv("URL") ?: "http://127.0.0.1:8080" }

src/main/kotlin/Main.kt

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,46 @@ import requests.ReaderRequest
66

77
val client = OkHttpClient()
88

9+
/**
10+
* Entry point of the program
11+
*/
912
fun main() {
1013

14+
// Check if the server is running
1115
if (checkServer())
1216
println("Connected to server")
1317
else {
1418
println("Server is not running")
15-
return
19+
return // Exit application
1620
}
1721

22+
// Loop until the user wants to exit
1823
while (true) {
24+
// Display choices
1925
println("1. Add book")
2026
println("2. Add reader")
21-
println("3. Exit")
27+
println("3. Disconnect ¯\\_(ツ)_/¯")
28+
println("4. Exit")
2229
println()
2330
print("Enter your choice: ")
2431

32+
// Read user input
2533
when (readln().toIntOrNull()) {
34+
35+
// Add book
2636
1 -> {
37+
// Ask user for book information
2738
val book = newBook()
2839

40+
// Send request to server
2941
val request = BookRequest()
3042
request.post(book, object : Callback {
43+
// If the request fails
3144
override fun onFailure(call: Call, e: java.io.IOException) {
3245
println("Failed to execute request")
3346
}
3447

48+
// If the request succeeds
3549
override fun onResponse(call: Call, response: Response) {
3650
if (response.isSuccessful)
3751
println("Book added successfully")
@@ -40,15 +54,21 @@ fun main() {
4054
}
4155
})
4256
}
57+
58+
// Add reader
4359
2 -> {
60+
// Ask user for reader information
4461
val reader = newReader()
4562

63+
// Send request to server
4664
val request = ReaderRequest()
4765
request.post(reader, object : Callback {
66+
// If the request fails
4867
override fun onFailure(call: Call, e: java.io.IOException) {
4968
println("Failed to execute request")
5069
}
5170

71+
// If the request succeeds
5272
override fun onResponse(call: Call, response: Response) {
5373
if(response.isSuccessful)
5474
println("Reader added successfully")
@@ -57,7 +77,21 @@ fun main() {
5777
}
5878
})
5979
}
80+
81+
// Disconnect (¯\_(ツ)_/¯)
82+
// In case this wasn't obvious enough, this is a joke
6083
3 -> {
84+
println("Disconnecting gracefully...")
85+
val request = Request.Builder()
86+
.url(Environment.url + "/gracefully/disconnect")
87+
.build()
88+
89+
client.newCall(request).execute()
90+
throw Exception("Disconnected gracefully!")
91+
}
92+
93+
// Exit application
94+
4 -> {
6195
println("Exiting...")
6296
break
6397
}
@@ -68,6 +102,10 @@ fun main() {
68102
}
69103
}
70104

105+
/**
106+
* Check if the server is running
107+
* @return true if the server is running, false otherwise
108+
*/
71109
fun checkServer(): Boolean {
72110
val request = Request.Builder()
73111
.url(Environment.url)
@@ -81,6 +119,10 @@ fun checkServer(): Boolean {
81119
}
82120
}
83121

122+
/**
123+
* Ask user for book information
124+
* @return The book
125+
*/
84126
fun newBook(): BookModel {
85127
println("Enter the title of the book:")
86128
val title: String = readln()
@@ -94,6 +136,10 @@ fun newBook(): BookModel {
94136
return BookModel(title, author, language, genre)
95137
}
96138

139+
/**
140+
* Ask user for reader information
141+
* @return The reader
142+
*/
97143
fun newReader(): ReaderModel {
98144
println("Enter the name of the reader:")
99145
val name: String = readln()

src/main/kotlin/models/BookModel.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ package models
22

33
import kotlinx.serialization.Serializable
44

5+
/**
6+
* BookModel is a class that contains the information of a book
7+
* @param title The title of the book
8+
* @param author The author of the book
9+
* @param language The language of the book
10+
* @param genre The genre of the book
11+
*/
512
@Serializable
613
data class BookModel (
714
val title: String,

src/main/kotlin/models/ReaderModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package models
22

33
import kotlinx.serialization.Serializable
44

5+
/**
6+
* ReaderModel is a class that contains the information of a reader
7+
* @param name The name of the reader
8+
*/
59
@Serializable
610
data class ReaderModel (
711
var name: String,

src/main/kotlin/requests/BookRequest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ import okhttp3.*
88
import okhttp3.MediaType.Companion.toMediaTypeOrNull
99
import okhttp3.RequestBody.Companion.toRequestBody
1010

11+
/**
12+
* BookRequest is a class that contains functions to send book request to the server
13+
*/
1114
class BookRequest {
15+
/**
16+
* Send a POST request to the server to add a book
17+
* @param book The book to add
18+
* @param callback The callback to execute when the request is done
19+
*/
1220
fun post(book: BookModel, callback: Callback) {
1321
val toml = Toml.encodeToString(book)
1422

src/main/kotlin/requests/ReaderRequest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ import okhttp3.*
88
import okhttp3.MediaType.Companion.toMediaTypeOrNull
99
import okhttp3.RequestBody.Companion.toRequestBody
1010

11+
/**
12+
* ReaderRequest is a class that contains functions to send reader request to the server
13+
*/
1114
class ReaderRequest {
15+
/**
16+
* Send a POST request to the server to add a reader
17+
* @param reader The reader to add
18+
* @param callback The callback to execute when the request is done
19+
*/
1220
fun post(reader: ReaderModel, callback: Callback) {
1321
val toml = Toml.encodeToString(reader)
1422

0 commit comments

Comments
 (0)