1
1
package com.example.plugins
2
2
3
+ import com.example.exceptions.DbElementNotFoundException
3
4
import com.example.models.Article
4
5
import com.example.service.ArticleService
5
6
import io.ktor.http.*
6
7
import io.ktor.server.application.*
8
+ import io.ktor.server.config.yaml.*
7
9
import io.ktor.server.request.*
8
10
import io.ktor.server.response.*
9
11
import io.ktor.server.routing.*
10
- import kotlinx.coroutines.*
11
12
import java.sql.*
12
13
13
- fun Application.configureRouting () {
14
- val dbConnection: Connection = connectToPostgres(embedded = false )
14
+ fun Application.configureRouting (dbConnection : Connection ) {
15
15
val articleService = ArticleService (dbConnection)
16
16
routing {
17
17
// Create new Article
@@ -22,60 +22,48 @@ fun Application.configureRouting() {
22
22
}
23
23
// Read an Article
24
24
get(" /articles/{id}" ) {
25
- val id = call.parameters[" id" ]?.toInt() ? : throw IllegalArgumentException (" Invalid ID" )
25
+ val id = call.parameters[" id" ]?.toInt() ? : throw DbElementNotFoundException (" Invalid article ID" )
26
26
try {
27
27
val article = articleService.read(id)
28
28
call.respond(HttpStatusCode .OK , article)
29
- } catch (e : Exception ) {
29
+ } catch (cause : DbElementNotFoundException ) {
30
30
call.respond(HttpStatusCode .NotFound )
31
31
}
32
32
}
33
33
// Update an Article
34
34
put(" /articles/{id}" ) {
35
- val id = call.parameters[" id" ]?.toInt() ? : throw IllegalArgumentException (" Invalid ID" )
36
- val article = call.receive<Article >()
37
- articleService.update(id, article)
38
- call.respond(HttpStatusCode .OK )
35
+ val id = call.parameters[" id" ]?.toInt() ? : throw DbElementNotFoundException (" Invalid article ID" )
36
+ try {
37
+ val article = call.receive<Article >()
38
+ articleService.update(id, article)
39
+ call.respond(HttpStatusCode .OK )
40
+ } catch (cause: DbElementNotFoundException ) {
41
+ call.respond(HttpStatusCode .NotFound )
42
+ }
39
43
}
40
44
// Delete an Article
41
45
delete(" /articles/{id}" ) {
42
- val id = call.parameters[" id" ]?.toInt() ? : throw IllegalArgumentException (" Invalid ID" )
43
- articleService.delete(id)
44
- call.respond(HttpStatusCode .OK )
46
+ val id = call.parameters[" id" ]?.toInt() ? : throw DbElementNotFoundException (" Invalid article ID" )
47
+ try {
48
+ articleService.delete(id)
49
+ call.respond(HttpStatusCode .OK )
50
+ } catch (cause: DbElementNotFoundException ) {
51
+ call.respond(HttpStatusCode .NotFound )
52
+ }
45
53
}
46
54
}
47
55
}
48
56
49
- /* *
50
- * Makes a connection to a Postgres database.
51
- *
52
- * In order to connect to your running Postgres process,
53
- * please specify the following parameters in your configuration file:
54
- * - postgres.url -- Url of your running database process.
55
- * - postgres.user -- Username for database connection
56
- * - postgres.password -- Password for database connection
57
- *
58
- * If you don't have a database process running yet, you may need to [download]((https://www.postgresql.org/download/))
59
- * and install Postgres and follow the instructions [here](https://postgresapp.com/).
60
- * Then, you would be edit your url, which is usually "jdbc:postgresql://host:port/database", as well as
61
- * user and password values.
62
- *
63
- *
64
- * @param embedded -- if [true] defaults to an embedded database for tests that runs locally in the same process.
65
- * In this case you don't have to provide any parameters in configuration file, and you don't have to run a process.
66
- *
67
- * @return [Connection] that represent connection to the database. Please, don't forget to close this connection when
68
- * your application shuts down by calling [Connection.close]
69
- * */
70
57
fun Application.connectToPostgres (embedded : Boolean ): Connection {
71
58
Class .forName(" org.postgresql.Driver" )
72
59
if (embedded) {
73
60
return DriverManager .getConnection(" jdbc:postgresql://localhost/test;DB_CLOSE_DELAY=-1" , " root" , " " )
74
61
} else {
75
- val url = environment.config.property(" postgres.url" ).getString()
76
- val user = environment.config.property(" postgres.user" ).getString()
77
- val password = environment.config.property(" postgres.password" ).getString()
78
-
62
+ val configs = YamlConfig (" postgres.yaml" )
63
+ val url = " jdbc:postgresql://localhost:5432/" +
64
+ configs?.property(" services.postgres.environment.POSTGRES_DB" )?.getString()
65
+ val user = configs?.property(" services.postgres.environment.POSTGRES_USER" )?.getString()
66
+ val password = configs?.property(" services.postgres.environment.POSTGRES_PASSWORD" )?.getString()
79
67
return DriverManager .getConnection(url, user, password)
80
68
}
81
69
}
0 commit comments