From 859ac51f1a506df57295f6914ff37076851d9786 Mon Sep 17 00:00:00 2001 From: Manuel Date: Wed, 11 Jan 2023 19:08:24 +0100 Subject: [PATCH] Formatting again using vfmt --- src/config.v | 12 ++++++------ src/database.v | 30 ++++++++++++++++++------------ src/main.v | 6 +++--- src/score.v | 4 ++-- src/web.v | 17 ++++++++++------- 5 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/config.v b/src/config.v index 0ed7e5a..6759a28 100644 --- a/src/config.v +++ b/src/config.v @@ -7,12 +7,12 @@ const ( ) struct Config { - host string - port int - token string - redirect bool + host string + port int + token string + redirect bool redirect_url string - db_path string + db_path string } fn (config Config) copy() Config { @@ -22,7 +22,7 @@ fn (config Config) copy() Config { fn load_config() Config { config := toml.parse_file(path) or { panic(err) } - return Config { + return Config{ host: config.value('host').string() port: config.value('port').int() token: config.value('token').string() diff --git a/src/database.v b/src/database.v index 6fe454d..9f56565 100644 --- a/src/database.v +++ b/src/database.v @@ -2,33 +2,39 @@ module main struct ScoreRes { pub mut: - id int [primary; sql: serial] - player string - score int - time i64 + id int [primary; sql: serial] + player string + score int + time i64 } fn (mut app App) create_tables() int { - return app.db.exec_none( - 'CREATE TABLE IF NOT EXISTS ScoreRes ( + return app.db.exec_none('CREATE TABLE IF NOT EXISTS ScoreRes ( id INTEGER PRIMARY KEY, player TEXT NOT NULL, score INTEGER NOT NULL, time SQLITE_INT64_TYPE NOT NULL - )' - ) + )') } 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() - 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 { - 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) { - sql app.db { delete from ScoreRes where id == score_id } + sql app.db { + delete from ScoreRes where id == score_id + } } diff --git a/src/main.v b/src/main.v index 6679625..bca46c5 100644 --- a/src/main.v +++ b/src/main.v @@ -8,8 +8,8 @@ import os struct App { vweb.Context pub mut: - db sqlite.DB - config shared Config + db sqlite.DB + config shared Config is_admin bool } @@ -41,7 +41,7 @@ fn main() { } mut host := '::' - if app_config.host != "" { + if app_config.host != '' { host = app_config.host } diff --git a/src/score.v b/src/score.v index 8df097e..925f797 100644 --- a/src/score.v +++ b/src/score.v @@ -1,6 +1,6 @@ module main struct Score { - player string - score int + player string + score int } diff --git a/src/web.v b/src/web.v index a1810e2..da9289b 100644 --- a/src/web.v +++ b/src/web.v @@ -5,7 +5,7 @@ import json import time struct Status { - status int + status int message string } @@ -31,20 +31,23 @@ pub fn (mut app App) score_list() vweb.Result { return app.json(scores) } -[post] -['/api/v1/score/submit'] +['/api/v1/score/submit'; post] pub fn (mut app App) score_submit() vweb.Result { if !app.auth() { 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 { app.set_status(400, '') 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) } @@ -58,6 +61,6 @@ fn (mut app App) auth() bool { rlock app.config { config_token = app.config.token } - + return config_token.len != 0 && token == config_token }