From 2fc25f1afefe34d4e196aab7b84f2a122045d154 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 19 May 2022 08:20:11 +0200 Subject: [PATCH] refactor: moved BuildLog to models --- src/client/logs.v | 2 +- src/console/logs/logs.v | 4 ++-- src/db/db.v | 2 +- src/db/git.v | 1 + src/db/logs.v | 27 +-------------------------- src/models/logs.v | 28 ++++++++++++++++++++++++++++ src/server/git.v | 2 +- src/server/logs.v | 3 ++- 8 files changed, 37 insertions(+), 32 deletions(-) create mode 100644 src/models/logs.v diff --git a/src/client/logs.v b/src/client/logs.v index cdacab9..3484f03 100644 --- a/src/client/logs.v +++ b/src/client/logs.v @@ -1,6 +1,6 @@ module client -import db { BuildLog } +import models { BuildLog } import net.http { Method } import response { Response } import time diff --git a/src/console/logs/logs.v b/src/console/logs/logs.v index 6400e80..1d0bb55 100644 --- a/src/console/logs/logs.v +++ b/src/console/logs/logs.v @@ -3,8 +3,8 @@ module logs import cli import env import client -import db import console +import models { BuildLog } struct Config { address string [required] @@ -67,7 +67,7 @@ pub fn cmd() cli.Command { } // print_log_list prints a list of logs. -fn print_log_list(logs []db.BuildLog) ? { +fn print_log_list(logs []BuildLog) ? { data := logs.map([it.id.str(), it.repo_id.str(), it.start_time.str(), it.exit_code.str()]) diff --git a/src/db/db.v b/src/db/db.v index a5756e6..57b437e 100644 --- a/src/db/db.v +++ b/src/db/db.v @@ -1,7 +1,7 @@ module db import sqlite -import models +import models { BuildLog, GitRepo } struct VieterDb { conn sqlite.DB diff --git a/src/db/git.v b/src/db/git.v index d8887ff..8cc493f 100644 --- a/src/db/git.v +++ b/src/db/git.v @@ -66,6 +66,7 @@ pub fn (db &VieterDb) update_git_repo(repo_id int, params map[string]string) { } } values_str := values.join(', ') + // I think this is actual SQL & not the ORM language query := 'update GitRepo set $values_str where id == $repo_id' db.conn.exec_none(query) diff --git a/src/db/logs.v b/src/db/logs.v index 817db78..129ec4e 100644 --- a/src/db/logs.v +++ b/src/db/logs.v @@ -1,31 +1,6 @@ module db -import time - -pub struct BuildLog { -pub: - id int [primary; sql: serial] - repo_id int [nonull] - start_time time.Time [nonull] - end_time time.Time [nonull] - arch string [nonull] - exit_code int [nonull] -} - -// str returns a string representation. -pub fn (bl &BuildLog) str() string { - mut parts := [ - 'id: $bl.id', - 'repo id: $bl.repo_id', - 'start time: $bl.start_time', - 'end time: $bl.end_time', - 'arch: $bl.arch', - 'exit code: $bl.exit_code', - ] - str := parts.join('\n') - - return str -} +import models { BuildLog } // get_build_logs returns all BuildLog's in the database. pub fn (db &VieterDb) get_build_logs() []BuildLog { diff --git a/src/models/logs.v b/src/models/logs.v new file mode 100644 index 0000000..173336f --- /dev/null +++ b/src/models/logs.v @@ -0,0 +1,28 @@ +module models + +import time + +pub struct BuildLog { +pub: + id int [primary; sql: serial] + repo_id int [nonull] + start_time time.Time [nonull] + end_time time.Time [nonull] + arch string [nonull] + exit_code int [nonull] +} + +// str returns a string representation. +pub fn (bl &BuildLog) str() string { + mut parts := [ + 'id: $bl.id', + 'repo id: $bl.repo_id', + 'start time: $bl.start_time', + 'end time: $bl.end_time', + 'arch: $bl.arch', + 'exit code: $bl.exit_code', + ] + str := parts.join('\n') + + return str +} diff --git a/src/server/git.v b/src/server/git.v index ec433c7..c1bc6f3 100644 --- a/src/server/git.v +++ b/src/server/git.v @@ -4,7 +4,7 @@ import web import net.http import response { new_data_response, new_response } import db -import models { GitRepoArch } +import models { GitRepo, GitRepoArch, GitRepoFilter } // get_repos returns the current list of repos. ['/api/repos'; get] diff --git a/src/server/logs.v b/src/server/logs.v index 21331e5..af0b081 100644 --- a/src/server/logs.v +++ b/src/server/logs.v @@ -8,6 +8,7 @@ import db import time import os import util +import models { BuildLog } // get_logs returns all build logs in the database. A 'repo' query param can // optionally be added to limit the list of build logs to that repository. @@ -97,7 +98,7 @@ fn (mut app App) post_log() web.Result { } // Store log in db - log := db.BuildLog{ + log := BuildLog{ repo_id: repo_id start_time: start_time end_time: end_time