feat(server): migrated repo patch to sqlite

This commit is contained in:
Jef Roosens 2022-05-02 20:59:53 +02:00
parent 891a206116
commit 0a2488a4df
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
2 changed files with 67 additions and 16 deletions

View file

@ -1,6 +1,6 @@
module db
struct GitRepoArch {
pub struct GitRepoArch {
pub:
id int [primary; sql: serial]
repo_id int
@ -58,7 +58,7 @@ pub fn git_repo_from_params(params map[string]string) ?GitRepo {
pub fn (db &VieterDb) get_git_repos() []GitRepo {
res := sql db.conn {
select from GitRepo
select from GitRepo order by id
}
return res
@ -88,11 +88,54 @@ pub fn (db &VieterDb) add_git_repo(repo GitRepo) {
pub fn (db &VieterDb) delete_git_repo(repo_id int) {
sql db.conn {
delete from GitRepo where id == repo_id
delete from GitRepoArch where repo_id == repo_id
}
}
pub fn (db &VieterDb) update_git_repo(repo GitRepo) {
pub fn (db &VieterDb) update_git_repo(repo_id int, params map[string]string) {
/* sql db.conn { */
/* update GitRepo set repo */
/* } */
mut values := []string{}
$for field in GitRepo.fields {
if field.name in params {
// Any fields that are array types require their own update method
$if field.typ is string {
values << "${field.name} = '${params[field.name]}'"
/* r.$(field.name) = params[field.name] */
// This specific type check is needed for the compiler to ensure
// our types are correct
}
/* $else $if field.typ is []GitRepoArch { */
/* r.$(field.name) = params[field.name].split(',').map(GitRepoArch{ value: it }) */
/* } */
}
}
values_str := values.join(', ')
query := "update GitRepo set $values_str where id == $repo_id"
println(query)
db.conn.exec_none(query)
}
pub fn (db &VieterDb) update_git_repo_archs(repo_id int, archs []GitRepoArch) {
archs_with_id := archs.map(GitRepoArch{
...it
repo_id: repo_id
})
sql db.conn {
// Remove all old values
delete from GitRepoArch where repo_id == repo_id
// Insert all the new ones
/* insert archs_with_id into GitRepoArch */
}
for arch in archs_with_id {
sql db.conn {
insert arch into GitRepoArch
}
}
}