-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.R
197 lines (163 loc) · 5.41 KB
/
api.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#* @filter checkAuth
function(req, res){
encoded_string <- req$HTTP_AUTHORIZATION
encoded_string <- gsub("Basic ", "",encoded_string)
decoded_string <- rawToChar(base64enc::base64decode(what = encoded_string))
username <- strsplit(decoded_string, ":")[[1]][1]
password <- strsplit(decoded_string, ":")[[1]][2]
conn <- RSQLite::dbConnect(RSQLite::SQLite(), "./data/sampledb.sqlite")
query <- paste0(
"
SELECT *
FROM users
WHERE username=? and password=?
"
)
checkUser <- DBI::dbGetQuery(conn, query, params = list(username, password))
checkUser <- nrow(checkUser)==0 # Unauthorized
if (checkUser){
res$status <- 401 # Unauthorized
return(list(error="Authentication required"))
} else {
plumber::forward()
}
}
#* @get /hello
#* @serializer html
function(){
"<html><h1>hello world</h1></html>"
}
#* Echo the parameter that was sent in
#* @param msg The message to echo back.
#* @get /echo
function(msg=""){
list(msg = paste0("The message is: '", msg, "'"))
}
#* @param price_min Minimum price (optional). If provided, only records with price greater than or equal to this value will be returned.
#* @param price_max Maximum price (optional). If provided, only records with price less than or equal to this value will be returned.
#* @param lat Latitude for geographical filtering (optional). If provided along with lon and radius, only records within the specified radius of this point will be returned.
#* @param lon Longitude for geographical filtering (optional). If provided along with lat and radius, only records within the specified radius of this point will be returned.
#* @param radius Radius for geographical filtering (optional). If provided along with lat and lon, only records within this distance (in KM units corresponding to lat/lon) of the specified point will be returned.
#* @get /centris
function(price_min, price_max, lat, lon, radius=1){
conn <- RSQLite::dbConnect(RSQLite::SQLite(), "./data/sampledb.sqlite")
if (!missing(price_min) & !missing(price_max)) {
query <- paste0(
"
SELECT *
FROM centris_ca
WHERE price BETWEEN {price_min} AND {price_max}
"
)
} else if (!missing(price_min)) {
query <- paste0(
"
SELECT *
FROM centris_ca
WHERE price >= {price_min}
"
)
} else if (!missing(price_max)) {
query <- paste0(
"
SELECT *
FROM centris_ca
WHERE price <= {price_max}
"
)
} else {
query <- paste0(
"
SELECT *
FROM centris_ca
"
)
}
data <- DBI::dbGetQuery(conn, stringr::str_glue(query))
DBI::dbDisconnect(conn)
if (!missing(lat) & !missing(lon)) {
lat <- as.numeric(lat)
lon <- as.numeric(lon)
radius <- as.numeric(radius)
data <- data |>
dplyr::mutate(dist = geo_distance(lat, lon, !!lat, !!lon)) |>
dplyr::filter(dist <= !!radius)
}
return(data)
}
#* @post /new_user
function(req, name, dob, gender, is_gamer) {
conn <- RSQLite::dbConnect(RSQLite::SQLite(), "./data/sampledb.sqlite")
if (missing(name)) name <- NA_character_
if (missing(dob)) dob <- NA_character_
if (missing(gender)) gender <- NA_character_
if (missing(is_gamer)) is_gamer <- NA_character_
query <- stringr::str_glue("INSERT INTO clients (name, dob, gender, is_gamer) VALUES ('{name}', '{dob}', '{gender}', '{is_gamer}')")
DBI::dbExecute(conn, query)
DBI::dbDisconnect(conn)
return(
list(
message = "User added successfully!"
)
)
}
#* @post /client/new
function(req, name, dob, gender, is_gamer) {
conn <- RSQLite::dbConnect(RSQLite::SQLite(), "./data/sampledb.sqlite")
if (missing(name)) name <- NA_character_
if (missing(dob)) dob <- NA_character_
if (missing(gender)) gender <- NA_character_
if (missing(is_gamer)) is_gamer <- NA_character_
query <- paste0(
"INSERT INTO clients (name, dob, gender, is_gamer) VALUES (?, ?, ?, ?)"
)
DBI::dbExecute(conn, query, params = list(name, dob, gender, is_gamer))
DBI::dbDisconnect(conn)
return(
list(
message = "User added successfully!"
)
)
}
#* @get /client/read
function(req) {
conn <- RSQLite::dbConnect(RSQLite::SQLite(), "./data/sampledb.sqlite")
data <- DBI::dbReadTable(conn, "clients")
DBI::dbDisconnect(conn)
return(data)
}
#* @put /client/update/<id>
function(id, name, dob, gender, is_gamer, res) {
conn <- RSQLite::dbConnect(RSQLite::SQLite(), "./data/sampledb.sqlite")
if (missing(name)) name <- NA_character_
if (missing(dob)) dob <- NA_character_
if (missing(gender)) gender <- NA_character_
if (missing(is_gamer)) is_gamer <- NA_character_
query <- paste0(
"UPDATE clients
SET name = ?, dob = ?, gender = ?, is_gamer = ?
WHERE id = ?"
)
result <- DBI::dbExecute(conn, query, params = list(name, dob, gender, is_gamer, as.integer(id)))
DBI::dbDisconnect(conn)
if (result > 0) {
res$status <- 200
return(list(message = "Client updated successfully"))
} else {
res$status <- 404
return(list(error = "Client not found"))
}
}
#* @delete /client/delete/<id>
function(id, res) {
conn <- RSQLite::dbConnect(RSQLite::SQLite(), "./data/sampledb.sqlite")
result <- dbExecute(con, "DELETE FROM clients WHERE id = ?", params = list(as.integer(id)))
DBI::dbDisconnect(con)
if (result > 0) {
res$status <- 200
return(list(message = "Client deleted successfully"))
} else {
res$status <- 404
return(list(error = "Client not found"))
}
}