forked from vieter-v/vieter
feat(server): migrated repo patch to sqlite
parent
891a206116
commit
0a2488a4df
49
src/db/git.v
49
src/db/git.v
|
@ -1,6 +1,6 @@
|
||||||
module db
|
module db
|
||||||
|
|
||||||
struct GitRepoArch {
|
pub struct GitRepoArch {
|
||||||
pub:
|
pub:
|
||||||
id int [primary; sql: serial]
|
id int [primary; sql: serial]
|
||||||
repo_id int
|
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 {
|
pub fn (db &VieterDb) get_git_repos() []GitRepo {
|
||||||
res := sql db.conn {
|
res := sql db.conn {
|
||||||
select from GitRepo
|
select from GitRepo order by id
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
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) {
|
pub fn (db &VieterDb) delete_git_repo(repo_id int) {
|
||||||
sql db.conn {
|
sql db.conn {
|
||||||
delete from GitRepo where id == repo_id
|
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 { */
|
/* sql db.conn { */
|
||||||
/* update GitRepo set repo */
|
/* 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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
// patch_repo updates a repo's data with the given query params.
|
||||||
['/api/repos/:id'; patch]
|
['/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() {
|
if !app.is_authorized() {
|
||||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||||
}
|
}
|
||||||
|
|
||||||
mut repos := rlock app.git_mutex {
|
app.db.update_git_repo(id, app.query)
|
||||||
git.read_repos(app.conf.repos_file) or {
|
|
||||||
app.lerror('Failed to read repos file.')
|
|
||||||
|
|
||||||
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 {
|
/* mut repos := rlock app.git_mutex { */
|
||||||
return app.not_found()
|
/* 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 {
|
/* if id !in repos { */
|
||||||
git.write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) }
|
/* 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.'))
|
return app.json(http.Status.ok, new_response('Repo updated successfully.'))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue