-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplumber.R
165 lines (132 loc) · 3.29 KB
/
plumber.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
library(DBI)
library(zeallot)
library(pool)
library(jsonlite)
`%||%` <- function(l, r) { if (is.null(l)) r else l }
create_table <- function(con) {
pool <- poolCheckout(con)
rs <- dbSendQuery(
pool,
'CREATE TABLE IF NOT EXISTS todos (
id SERIAL PRIMARY KEY,
title text,
completed boolean DEFAULT FALSE,
"order" integer,
url text
);'
)
dbClearResult(rs)
poolReturn(pool)
}
create_todo <- function(con, req, title, order = NULL, completed = FALSE) {
todo_order <- order %||% NA_integer_
df <- dbGetQuery(con,
'INSERT INTO todos ("title", "order", "completed") VALUES ($1, $2, $3) RETURNING *',
params = list(title, todo_order, completed)
)
url <- glue::glue("{req$rook.url_scheme}://{req$HTTP_HOST}/{df$id}")
out <- dbGetQuery(
con,
'UPDATE todos set "url"=$1 WHERE id=$2 RETURNING *',
params = list(url, df$id)
)
unbox(out)
}
get_todo <- function(con, id) {
out <- dbGetQuery(
con,
"SELECT * FROM TODOS WHERE id = $1",
params = list(id)
)
unbox(out)
}
get_todos <- function(con) {
dbGetQuery(con, "SELECT * FROM todos")
}
update_todo <- function(con, id, title = NULL, order = NULL, completed = NULL) {
c(old_title, old_completed, old_order) %<-%
dbGetQuery(
con,
'SELECT title, completed, "order" FROM todos WHERE id = $1',
params = list(id)
)
new_title <- title %||% old_title
new_completed <- completed %||% old_completed
new_order <- order %||% old_order
out <- dbGetQuery(
con,
'UPDATE todos set "title"=$1, "order"=$2, "completed"=$3 WHERE id=$4 RETURNING *',
params = list(new_title, new_order, new_completed, id)
)
unbox(out)
}
delete_todo <- function(con, id) {
dbGetQuery(
con,
"DELETE FROM todos where id = $1",
params = list(id)
)
}
delete_todos <- function(con) {
pool <- poolCheckout(con)
rs <- dbSendQuery(pool, "DELETE FROM todos")
dbClearResult(rs)
poolReturn(pool)
}
create_table(con)
#' @filter cors
cors <- function(req, res) {
res$setHeader("Access-Control-Allow-Origin", "*")
if (req$REQUEST_METHOD == "OPTIONS") {
res$setHeader("Access-Control-Allow-Methods","*")
res$setHeader("Access-Control-Allow-Headers", req$HTTP_ACCESS_CONTROL_REQUEST_HEADERS)
res$status <- 200
return(list())
} else {
plumber::forward()
}
}
#' @get /
#' @post /
#' @delete /
function(req, res, title, order = NULL, completed) {
method <- req$REQUEST_METHOD
if (method == "POST") {
out <- create_todo(con, req, title, order)
res$status <- 201
return(out)
}
if (method == "DELETE") {
delete_todos(con)
res$status <- 204
return(list())
}
if (method == "GET") {
out <- get_todos(con)
res$status <- 200
return(out)
}
msg <- "something went wrong."
res$status <- 500
list(error = unbox(msg))
}
#' @get /<id:int>
#' @patch /<id:int>
#' @delete /<id:int>
function(req, res, id, title = NULL, order = NULL, completed = NULL) {
method <- req$REQUEST_METHOD
if (method == "PATCH") {
return(update_todo(con, id, title, order, completed))
}
if (method == "DELETE") {
out <- delete_todo(con, id)
res$status <- 204
return(out)
}
if (method == "GET") {
return(get_todo(con, id))
}
msg <- "something went wrong."
res$status <- 500
list(error = unbox(msg))
}