diff --git a/src/db/git.v b/src/db/git.v index fca46c6..29606e4 100644 --- a/src/db/git.v +++ b/src/db/git.v @@ -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 + } + } } diff --git a/src/server/git.v b/src/server/git.v index 6485eca..69cce81 100644 --- a/src/server/git.v +++ b/src/server/git.v @@ -134,28 +134,36 @@ fn (mut app App) delete_repo(id int) web.Result { // patch_repo updates a repo's data with the given query params. ['/api/repos/:id'; patch] -fn (mut app App) patch_repo(id string) web.Result { +fn (mut app App) patch_repo(id int) web.Result { if !app.is_authorized() { return app.json(http.Status.unauthorized, new_response('Unauthorized.')) } - mut repos := rlock app.git_mutex { - git.read_repos(app.conf.repos_file) or { - app.lerror('Failed to read repos file.') + app.db.update_git_repo(id, app.query) - return app.status(http.Status.internal_server_error) + if 'arch' in app.query { + arch_objs := app.query['arch'].split(',').map(db.GitRepoArch{value: it}) + + app.db.update_git_repo_archs(id, arch_objs) } - } - if id !in repos { - return app.not_found() - } +/* mut repos := rlock app.git_mutex { */ +/* git.read_repos(app.conf.repos_file) or { */ +/* app.lerror('Failed to read repos file.') */ - repos[id].patch_from_params(app.query) +/* return app.status(http.Status.internal_server_error) */ +/* } */ +/* } */ - lock app.git_mutex { - git.write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) } - } +/* if id !in repos { */ +/* return app.not_found() */ +/* } */ + +/* repos[id].patch_from_params(app.query) */ + +/* lock app.git_mutex { */ +/* git.write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) } */ +/* } */ return app.json(http.Status.ok, new_response('Repo updated successfully.')) }