Formatting again using vfmt
This commit is contained in:
parent
318b1d3dc8
commit
859ac51f1a
5 changed files with 39 additions and 30 deletions
12
src/config.v
12
src/config.v
|
@ -7,12 +7,12 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
struct Config {
|
struct Config {
|
||||||
host string
|
host string
|
||||||
port int
|
port int
|
||||||
token string
|
token string
|
||||||
redirect bool
|
redirect bool
|
||||||
redirect_url string
|
redirect_url string
|
||||||
db_path string
|
db_path string
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (config Config) copy() Config {
|
fn (config Config) copy() Config {
|
||||||
|
@ -22,7 +22,7 @@ fn (config Config) copy() Config {
|
||||||
fn load_config() Config {
|
fn load_config() Config {
|
||||||
config := toml.parse_file(path) or { panic(err) }
|
config := toml.parse_file(path) or { panic(err) }
|
||||||
|
|
||||||
return Config {
|
return Config{
|
||||||
host: config.value('host').string()
|
host: config.value('host').string()
|
||||||
port: config.value('port').int()
|
port: config.value('port').int()
|
||||||
token: config.value('token').string()
|
token: config.value('token').string()
|
||||||
|
|
|
@ -2,33 +2,39 @@ module main
|
||||||
|
|
||||||
struct ScoreRes {
|
struct ScoreRes {
|
||||||
pub mut:
|
pub mut:
|
||||||
id int [primary; sql: serial]
|
id int [primary; sql: serial]
|
||||||
player string
|
player string
|
||||||
score int
|
score int
|
||||||
time i64
|
time i64
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) create_tables() int {
|
fn (mut app App) create_tables() int {
|
||||||
return app.db.exec_none(
|
return app.db.exec_none('CREATE TABLE IF NOT EXISTS ScoreRes (
|
||||||
'CREATE TABLE IF NOT EXISTS ScoreRes (
|
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
player TEXT NOT NULL,
|
player TEXT NOT NULL,
|
||||||
score INTEGER NOT NULL,
|
score INTEGER NOT NULL,
|
||||||
time SQLITE_INT64_TYPE NOT NULL
|
time SQLITE_INT64_TYPE NOT NULL
|
||||||
)'
|
)')
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) insert_score(score ScoreRes) ScoreRes {
|
fn (mut app App) insert_score(score ScoreRes) ScoreRes {
|
||||||
sql app.db { insert score into ScoreRes }
|
sql app.db {
|
||||||
|
insert score into ScoreRes
|
||||||
|
}
|
||||||
last_row_id := app.db.last_insert_rowid()
|
last_row_id := app.db.last_insert_rowid()
|
||||||
return sql app.db { select from ScoreRes where id == last_row_id }
|
return sql app.db {
|
||||||
|
select from ScoreRes where id == last_row_id
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) get_scores() []ScoreRes {
|
fn (mut app App) get_scores() []ScoreRes {
|
||||||
return sql app.db { select from ScoreRes order by score desc }
|
return sql app.db {
|
||||||
|
select from ScoreRes order by score desc
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) delete_score(score_id int) {
|
fn (mut app App) delete_score(score_id int) {
|
||||||
sql app.db { delete from ScoreRes where id == score_id }
|
sql app.db {
|
||||||
|
delete from ScoreRes where id == score_id
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,8 @@ import os
|
||||||
struct App {
|
struct App {
|
||||||
vweb.Context
|
vweb.Context
|
||||||
pub mut:
|
pub mut:
|
||||||
db sqlite.DB
|
db sqlite.DB
|
||||||
config shared Config
|
config shared Config
|
||||||
is_admin bool
|
is_admin bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
mut host := '::'
|
mut host := '::'
|
||||||
if app_config.host != "" {
|
if app_config.host != '' {
|
||||||
host = app_config.host
|
host = app_config.host
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
module main
|
module main
|
||||||
|
|
||||||
struct Score {
|
struct Score {
|
||||||
player string
|
player string
|
||||||
score int
|
score int
|
||||||
}
|
}
|
||||||
|
|
13
src/web.v
13
src/web.v
|
@ -5,7 +5,7 @@ import json
|
||||||
import time
|
import time
|
||||||
|
|
||||||
struct Status {
|
struct Status {
|
||||||
status int
|
status int
|
||||||
message string
|
message string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,12 +31,11 @@ pub fn (mut app App) score_list() vweb.Result {
|
||||||
return app.json(scores)
|
return app.json(scores)
|
||||||
}
|
}
|
||||||
|
|
||||||
[post]
|
['/api/v1/score/submit'; post]
|
||||||
['/api/v1/score/submit']
|
|
||||||
pub fn (mut app App) score_submit() vweb.Result {
|
pub fn (mut app App) score_submit() vweb.Result {
|
||||||
if !app.auth() {
|
if !app.auth() {
|
||||||
app.set_status(401, '')
|
app.set_status(401, '')
|
||||||
return app.json(Status {401, 'OAuth token is missing'})
|
return app.json(Status{401, 'OAuth token is missing'})
|
||||||
}
|
}
|
||||||
|
|
||||||
body := json.decode(Score, app.req.data) or {
|
body := json.decode(Score, app.req.data) or {
|
||||||
|
@ -44,7 +43,11 @@ pub fn (mut app App) score_submit() vweb.Result {
|
||||||
return app.json(Status{400, 'Bad JSON object'})
|
return app.json(Status{400, 'Bad JSON object'})
|
||||||
}
|
}
|
||||||
|
|
||||||
score := app.insert_score(ScoreRes{player:body.player, score:body.score, time:time.now().unix_time()})
|
score := app.insert_score(ScoreRes{
|
||||||
|
player: body.player
|
||||||
|
score: body.score
|
||||||
|
time: time.now().unix_time()
|
||||||
|
})
|
||||||
|
|
||||||
return app.json(score)
|
return app.json(score)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue