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

Commit 5341127

Browse files
committed
* Update Models et Requests
+ CLI + Server check * Renamed Environment.kt
1 parent a81d65f commit 5341127

File tree

9 files changed

+111
-316
lines changed

9 files changed

+111
-316
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/.gradle/
2-
/.idea/
2+
/.idea/
3+
/.vscode/

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
22

33
plugins {
44
kotlin("jvm") version "1.7.21"
5+
kotlin("plugin.serialization") version "1.8.0"
56
application
67
}
78

@@ -15,10 +16,9 @@ repositories {
1516
dependencies {
1617
testImplementation(kotlin("test"))
1718

18-
1919
implementation("com.squareup.okhttp3:okhttp:4.10.0")
20-
implementation("com.moandjiezana.toml:toml4j:0.7.2")
2120
implementation("io.github.cdimascio:dotenv-kotlin:6.4.1")
21+
implementation("com.akuleshov7:ktoml-core:0.4.0")
2222
}
2323

2424
tasks.test {

src/main/kotlin/Environement.kt

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/main/kotlin/Environment.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Environment {
2+
companion object {
3+
val url by lazy { System.getenv("URL") ?: "http://127.0.0.1:8080" }
4+
}
5+
}

src/main/kotlin/Main.kt

Lines changed: 81 additions & 228 deletions
Original file line numberDiff line numberDiff line change
@@ -1,246 +1,99 @@
11
import models.BookModel
22
import models.ReaderModel
3-
import okhttp3.OkHttpClient
4-
import okhttp3.Request
3+
import okhttp3.*
54
import requests.BookRequest
65
import requests.ReaderRequest
7-
import java.io.IOException
86

9-
val client = OkHttpClient();
7+
val client = OkHttpClient()
108

11-
fun main(args: Array<String>) {
9+
fun main() {
1210

13-
newBook()
11+
if (checkServer())
12+
println("Connected to server")
13+
else {
14+
println("Server is not running")
15+
return
16+
}
1417

15-
newReader()
18+
while (true) {
19+
println("1. Add book")
20+
println("2. Add reader")
21+
println("3. Exit")
22+
println()
23+
print("Enter your choice: ")
1624

25+
when (readln().toIntOrNull()) {
26+
1 -> {
27+
val book = newBook()
28+
29+
val request = BookRequest()
30+
request.post(book, object : Callback {
31+
override fun onFailure(call: Call, e: java.io.IOException) {
32+
println("Failed to execute request")
33+
}
34+
35+
override fun onResponse(call: Call, response: Response) {
36+
if (response.code == 200)
37+
println("Book added successfully")
38+
else
39+
println("Failed to add book (Status code: ${response.code})")
40+
}
41+
})
42+
}
43+
2 -> {
44+
val reader = newReader()
45+
46+
val request = ReaderRequest()
47+
request.post(reader, object : Callback {
48+
override fun onFailure(call: Call, e: java.io.IOException) {
49+
println("Failed to execute request")
50+
}
51+
52+
override fun onResponse(call: Call, response: Response) {
53+
println("Reader added successfully")
54+
}
55+
})
56+
}
57+
3 -> {
58+
println("Exiting...")
59+
break
60+
}
61+
else -> {
62+
println("Invalid choice")
63+
}
64+
}
65+
}
1766
}
1867

19-
fun newBook() : BookModel {
20-
val title: String
21-
var author: String
22-
var year: Int
23-
var pages: Int
24-
var language: String
25-
var publisher: String
26-
var isbn: String
27-
var price: Double
28-
var format: String
29-
var genre: String
30-
var description: String
31-
var cover: String
32-
var url: String
33-
var id: Int
68+
fun checkServer(): Boolean {
69+
val request = Request.Builder()
70+
.url(Environment.url)
71+
.build()
3472

35-
println("Enter the title of the book")
36-
title = readLine()!!
37-
println("Enter the author of the book")
38-
author = readLine()!!
39-
println("Enter the year of the book")
40-
year = readLine()!!.toInt()
41-
println("Enter the number of pages of the book")
42-
pages = readLine()!!.toInt()
43-
println("Enter the language of the book")
44-
language = readLine()!!
45-
println("Enter the publisher of the book")
46-
publisher = readLine()!!
47-
println("Enter the ISBN of the book")
48-
isbn = readLine()!!
49-
println("Enter the price of the book")
50-
price = readLine()!!.toDouble()
51-
println("Enter the format of the book")
52-
format = readLine()!!
53-
println("Enter the genre of the book")
54-
genre = readLine()!!
55-
println("Enter the description of the book")
56-
description = readLine()!!
57-
println("Enter the cover of the book")
58-
cover = readLine()!!
59-
println("Enter the url of the book")
60-
url = readLine()!!
61-
println("Enter the id of the book")
62-
id = readLine()!!.toInt()
73+
return try {
74+
client.newCall(request).execute()
75+
true
76+
} catch (e: Exception) {
77+
false
78+
}
79+
}
80+
81+
fun newBook(): BookModel {
82+
println("Enter the title of the book:")
83+
val title: String = readln()
84+
println("Enter the author of the book:")
85+
val author: String = readln()
86+
println("Enter the language of the book:")
87+
val language: String = readln()
88+
println("Enter the genre of the book:")
89+
val genre: String = readln()
6390

64-
val book = BookModel(title, author, year, pages, language, publisher, isbn, price, format, genre, description, cover, url, id)
65-
return book
91+
return BookModel(title, author, language, genre)
6692
}
6793

6894
fun newReader(): ReaderModel {
69-
var id: Int
70-
var name: String
71-
var email: String
72-
var password: String
73-
var role: String
74-
var token: String
75-
var books: List<BookModel>
76-
var booksBorrowed: List<BookModel>
77-
var booksReserved: List<BookModel>
78-
var booksReturned: List<BookModel>
79-
var booksLost: List<BookModel>
80-
var booksDamaged: List<BookModel>
81-
var booksBorrowedHistory: List<BookModel>
82-
var booksReservedHistory: List<BookModel>
83-
var booksReturnedHistory: List<BookModel>
84-
var booksLostHistory: List<BookModel>
85-
var booksDamagedHistory: List<BookModel>
86-
var booksBorrowedHistoryCount: Int
87-
var booksReservedHistoryCount: Int
88-
var booksReturnedHistoryCount: Int
89-
var booksLostHistoryCount: Int
90-
var booksDamagedHistoryCount: Int
91-
var booksBorrowedCount: Int
92-
var booksReservedCount: Int
93-
var booksReturnedCount: Int
94-
var booksLostCount: Int
95-
var booksDamagedCount: Int
96-
var booksCount: Int
97-
var booksBorrowedHistoryPage: Int
98-
var booksReservedHistoryPage: Int
99-
var booksReturnedHistoryPage: Int
100-
var booksLostHistoryPage: Int
101-
var booksDamagedHistoryPage: Int
102-
var booksBorrowedPage: Int
103-
var booksReservedPage: Int
104-
var booksReturnedPage: Int
105-
var booksLostPage: Int
106-
var booksDamagedPage: Int
107-
var booksPage: Int
108-
var booksBorrowedHistoryPageCount: Int
109-
var booksReservedHistoryPageCount: Int
110-
var booksReturnedHistoryPageCount: Int
111-
var booksLostHistoryPageCount: Int
112-
113-
println("Enter the id of the reader")
114-
id = readLine()!!.toInt()
115-
println("Enter the name of the reader")
116-
name = readLine()!!
117-
println("Enter the email of the reader")
118-
email = readLine()!!
119-
println("Enter the password of the reader")
120-
password = readLine()!!
121-
println("Enter the role of the reader")
122-
role = readLine()!!
123-
println("Enter the token of the reader")
124-
token = readLine()!!
125-
println("Enter the books of the reader")
126-
books = readLine()!!.split(",").map { it.trim() }.map { BookModel(it, "", 0, 0, "", "", "", 0.0, "", "", "", "", "", 0) }
127-
println("Enter the booksBorrowed of the reader")
128-
booksBorrowed = readLine()!!.split(",").map { it.trim() }.map { BookModel(it, "", 0, 0, "", "", "", 0.0, "", "", "", "", "", 0) }
129-
println("Enter the booksReserved of the reader")
130-
booksReserved = readLine()!!.split(",").map { it.trim() }.map { BookModel(it, "", 0, 0, "", "", "", 0.0, "", "", "", "", "", 0) }
131-
println("Enter the booksReturned of the reader")
132-
booksReturned = readLine()!!.split(",").map { it.trim() }.map { BookModel(it, "", 0, 0, "", "", "", 0.0, "", "", "", "", "", 0) }
133-
println("Enter the booksLost of the reader")
134-
booksLost = readLine()!!.split(",").map { it.trim() }.map { BookModel(it, "", 0, 0, "", "", "", 0.0, "", "", "", "", "", 0) }
135-
println("Enter the booksDamaged of the reader")
136-
booksDamaged = readLine()!!.split(",").map { it.trim() }.map { BookModel(it, "", 0, 0, "", "", "", 0.0, "", "", "", "", "", 0) }
137-
println("Enter the booksBorrowedHistory of the reader")
138-
booksBorrowedHistory = readLine()!!.split(",").map { it.trim() }.map { BookModel(it, "", 0, 0, "", "", "", 0.0, "", "", "", "", "", 0) }
139-
println("Enter the booksReservedHistory of the reader")
140-
booksReservedHistory = readLine()!!.split(",").map { it.trim() }.map { BookModel(it, "", 0, 0, "", "", "", 0.0, "", "", "", "", "", 0) }
141-
println("Enter the booksReturnedHistory of the reader")
142-
booksReturnedHistory = readLine()!!.split(",").map { it.trim() }.map { BookModel(it, "", 0, 0, "", "", "", 0.0, "", "", "", "", "", 0) }
143-
println("Enter the booksLostHistory of the reader")
144-
booksLostHistory = readLine()!!.split(",").map { it.trim() }.map { BookModel(it, "", 0, 0, "", "", "", 0.0, "", "", "", "", "", 0) }
145-
println("Enter the booksDamagedHistory of the reader")
146-
booksDamagedHistory = readLine()!!.split(",").map { it.trim() }.map { BookModel(it, "", 0, 0, "", "", "", 0.0, "", "", "", "", "", 0) }
147-
println("Enter the booksBorrowedHistoryCount of the reader")
148-
booksBorrowedHistoryCount = readLine()!!.toInt()
149-
println("Enter the booksReservedHistoryCount of the reader")
150-
booksReservedHistoryCount = readLine()!!.toInt()
151-
println("Enter the booksReturnedHistoryCount of the reader")
152-
booksReturnedHistoryCount = readLine()!!.toInt()
153-
println("Enter the booksLostHistoryCount of the reader")
154-
booksLostHistoryCount = readLine()!!.toInt()
155-
println("Enter the booksDamagedHistoryCount of the reader")
156-
booksDamagedHistoryCount = readLine()!!.toInt()
157-
println("Enter the booksBorrowedCount of the reader")
158-
booksBorrowedCount = readLine()!!.toInt()
159-
println("Enter the booksReservedCount of the reader")
160-
booksReservedCount = readLine()!!.toInt()
161-
println("Enter the booksReturnedCount of the reader")
162-
booksReturnedCount = readLine()!!.toInt()
163-
println("Enter the booksLostCount of the reader")
164-
booksLostCount = readLine()!!.toInt()
165-
println("Enter the booksDamagedCount of the reader")
166-
booksDamagedCount = readLine()!!.toInt()
167-
println("Enter the booksCount of the reader")
168-
booksCount = readLine()!!.toInt()
169-
println("Enter the booksBorrowedHistoryPage of the reader")
170-
booksBorrowedHistoryPage = readLine()!!.toInt()
171-
println("Enter the booksReservedHistoryPage of the reader")
172-
booksReservedHistoryPage = readLine()!!.toInt()
173-
println("Enter the booksReturnedHistoryPage of the reader")
174-
booksReturnedHistoryPage = readLine()!!.toInt()
175-
println("Enter the booksLostHistoryPage of the reader")
176-
booksLostHistoryPage = readLine()!!.toInt()
177-
println("Enter the booksDamagedHistoryPage of the reader")
178-
booksDamagedHistoryPage = readLine()!!.toInt()
179-
println("Enter the booksBorrowedPage of the reader")
180-
booksBorrowedPage = readLine()!!.toInt()
181-
println("Enter the booksReservedPage of the reader")
182-
booksReservedPage = readLine()!!.toInt()
183-
println("Enter the booksReturnedPage of the reader")
184-
booksReturnedPage = readLine()!!.toInt()
185-
println("Enter the booksLostPage of the reader")
186-
booksLostPage = readLine()!!.toInt()
187-
println("Enter the booksDamagedPage of the reader")
188-
booksDamagedPage = readLine()!!.toInt()
189-
println("Enter the booksPage of the reader")
190-
booksPage = readLine()!!.toInt()
191-
println("Enter the booksBorrowedHistoryPageCount of the reader")
192-
booksBorrowedHistoryPageCount = readLine()!!.toInt()
193-
println("Enter the booksReservedHistoryPageCount of the reader")
194-
booksReservedHistoryPageCount = readLine()!!.toInt()
195-
println("Enter the booksReturnedHistoryPageCount of the reader")
196-
booksReturnedHistoryPageCount = readLine()!!.toInt()
197-
println("Enter the booksLostHistoryPageCount of the reader")
198-
booksLostHistoryPageCount = readLine()!!.toInt()
95+
println("Enter the name of the reader:")
96+
val name: String = readln()
19997

200-
val reader = ReaderModel(
201-
id,
202-
name,
203-
email,
204-
password,
205-
role,
206-
token,
207-
books,
208-
booksBorrowed,
209-
booksReserved,
210-
booksReturned,
211-
booksLost,
212-
booksDamaged,
213-
booksBorrowedHistory,
214-
booksReservedHistory,
215-
booksReturnedHistory,
216-
booksLostHistory,
217-
booksDamagedHistory,
218-
booksBorrowedHistoryCount,
219-
booksReservedHistoryCount,
220-
booksReturnedHistoryCount,
221-
booksLostHistoryCount,
222-
booksDamagedHistoryCount,
223-
booksBorrowedCount,
224-
booksReservedCount,
225-
booksReturnedCount,
226-
booksLostCount,
227-
booksDamagedCount,
228-
booksCount,
229-
booksBorrowedHistoryPage,
230-
booksReservedHistoryPage,
231-
booksReturnedHistoryPage,
232-
booksLostHistoryPage,
233-
booksDamagedHistoryPage,
234-
booksBorrowedPage,
235-
booksReservedPage,
236-
booksReturnedPage,
237-
booksLostPage,
238-
booksDamagedPage,
239-
booksPage,
240-
booksBorrowedHistoryPageCount,
241-
booksReservedHistoryPageCount,
242-
booksReturnedHistoryPageCount,
243-
booksLostHistoryPageCount
244-
)
245-
return reader
98+
return ReaderModel(name)
24699
}

0 commit comments

Comments
 (0)