Skip to content

Commit

Permalink
add orm errors handling (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
walking devel authored Apr 12, 2023
1 parent 99f3033 commit 1a1eb58
Show file tree
Hide file tree
Showing 28 changed files with 469 additions and 350 deletions.
8 changes: 4 additions & 4 deletions src/activity_service.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module main

import time

fn (app App) add_activity(user_id int, name string) {
fn (app App) add_activity(user_id int, name string) ! {
activity := Activity{
user_id: user_id
name: name
Expand All @@ -11,19 +11,19 @@ fn (app App) add_activity(user_id int, name string) {

sql app.db {
insert activity into Activity
}
}!
}

fn (mut app App) find_activities(user_id int) []Activity {
return sql app.db {
select from Activity where user_id == user_id order by created_at desc
}
} or { []Activity{} }
}

fn (mut app App) has_activity(user_id int, name string) bool {
activity_count := sql app.db {
select count from Activity where user_id == user_id && name == name
}
} or { 0 }

return activity_count > 0
}
4 changes: 2 additions & 2 deletions src/admin_routes.v
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn (mut app App) handle_admin_update_settings(oauth_client_id string, oauth_
return app.redirect_to_index()
}

app.update_gitly_settings(oauth_client_id, oauth_client_secret)
app.update_gitly_settings(oauth_client_id, oauth_client_secret) or { app.info(err.str()) }

return app.redirect('/admin')
}
Expand All @@ -36,7 +36,7 @@ pub fn (mut app App) handle_admin_edit_user(user_id string) vweb.Result {
is_blocked := 'is-blocked' in app.form
is_admin := 'is-admin' in app.form

app.edit_user(user_id.int(), clear_session, is_blocked, is_admin)
app.edit_user(user_id.int(), clear_session, is_blocked, is_admin) or { app.info(err.str()) }

return app.redirect('/admin')
}
Expand Down
32 changes: 16 additions & 16 deletions src/admin_service.v
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
module main

pub fn (mut app App) edit_user(user_id int, delete_tokens bool, is_blocked bool, is_admin bool) {
pub fn (mut app App) edit_user(user_id int, delete_tokens bool, is_blocked bool, is_admin bool) ! {
if is_admin {
app.add_admin(user_id)
app.add_admin(user_id)!
} else {
app.remove_admin(user_id)
app.remove_admin(user_id)!
}

if is_blocked {
app.block_user(user_id)
app.block_user(user_id)!
} else {
app.unblock_user(user_id)
app.unblock_user(user_id)!
}

if delete_tokens {
app.delete_tokens(user_id)
app.delete_tokens(user_id)!
}
}

pub fn (mut app App) block_user(user_id int) {
app.set_user_block_status(user_id, true)
pub fn (mut app App) block_user(user_id int) ! {
app.set_user_block_status(user_id, true)!
}

pub fn (mut app App) unblock_user(user_id int) {
app.set_user_block_status(user_id, false)
pub fn (mut app App) unblock_user(user_id int) ! {
app.set_user_block_status(user_id, false)!
}

pub fn (mut app App) add_admin(user_id int) {
app.set_user_admin_status(user_id, true)
pub fn (mut app App) add_admin(user_id int) ! {
app.set_user_admin_status(user_id, true)!
}

pub fn (mut app App) remove_admin(user_id int) {
app.set_user_admin_status(user_id, false)
pub fn (mut app App) remove_admin(user_id int) ! {
app.set_user_admin_status(user_id, false)!
}

pub fn (mut app App) update_gitly_settings(oauth_client_id string, oauth_client_secret string) {
app.update_settings(oauth_client_id, oauth_client_secret)
pub fn (mut app App) update_gitly_settings(oauth_client_id string, oauth_client_secret string) ! {
app.update_settings(oauth_client_id, oauth_client_secret)!

app.load_settings()
}
Expand Down
51 changes: 32 additions & 19 deletions src/branch_service.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module main
import time
import git

fn (mut app App) fetch_branches(repo Repo) {
fn (mut app App) fetch_branches(repo Repo) ! {
branches_output := repo.git('branch -a')

for branch_output in branches_output.split_into_lines() {
branch_name := git.parse_git_branch_output(branch_output)

app.fetch_branch(repo, branch_name)
app.fetch_branch(repo, branch_name)!
}
}

fn (mut app App) fetch_branch(repo Repo, branch_name string) {
fn (mut app App) fetch_branch(repo Repo, branch_name string) ! {
last_commit_hash := repo.get_last_branch_commit_hash(branch_name)

branch_data := repo.git('log ${branch_name} -1 --pretty="%aE${log_field_separator}%cD" ${last_commit_hash}')
Expand All @@ -33,16 +33,17 @@ fn (mut app App) fetch_branch(repo Repo, branch_name string) {
}

app.create_branch_or_update(repo.id, branch_name, user.username, last_commit_hash,
int(committed_at.unix))
int(committed_at.unix))!
}

fn (mut app App) create_branch_or_update(repository_id int, branch_name string, author string, hash string, date int) {
branch := sql app.db {
fn (mut app App) create_branch_or_update(repository_id int, branch_name string, author string, hash string, date int) ! {
branches := sql app.db {
select from Branch where repo_id == repository_id && name == branch_name limit 1
}
} or { []Branch{} }

if branch.id != 0 {
app.update_branch(branch.id, author, hash, date)
if branches.len != 0 {
branch := branches.first()
app.update_branch(branch.id, author, hash, date)!

return
}
Expand All @@ -57,51 +58,63 @@ fn (mut app App) create_branch_or_update(repository_id int, branch_name string,

sql app.db {
insert new_branch into Branch
}
}!
}

fn (mut app App) update_branch(branch_id int, author string, hash string, date int) {
fn (mut app App) update_branch(branch_id int, author string, hash string, date int) ! {
sql app.db {
update Branch set author = author, hash = hash, date = date where id == branch_id
}
}!
}

fn (mut app App) find_repo_branch_by_name(repo_id int, name string) Branch {
return sql app.db {
branches := sql app.db {
select from Branch where name == name && repo_id == repo_id limit 1
} or { []Branch{} }

if branches.len == 0 {
return Branch{}
}

return branches.first()
}

fn (mut app App) find_repo_branch_by_id(repo_id int, id int) Branch {
return sql app.db {
branches := sql app.db {
select from Branch where id == id && repo_id == repo_id limit 1
} or { []Branch{} }

if branches.len == 0 {
return Branch{}
}

return branches.first()
}

fn (app App) get_all_repo_branches(repo_id int) []Branch {
return sql app.db {
select from Branch where repo_id == repo_id order by date desc
}
} or { []Branch{} }
}

fn (mut app App) get_count_repo_branches(repo_id int) int {
return sql app.db {
select count from Branch where repo_id == repo_id
}
} or { 0 }
}

fn (mut app App) contains_repo_branch(repo_id int, name string) bool {
count := sql app.db {
select count from Branch where repo_id == repo_id && name == name
}
} or { 0 }

return count == 1
}

fn (mut app App) delete_repo_branches(repo_id int) {
fn (mut app App) delete_repo_branches(repo_id int) ! {
sql app.db {
delete from Branch where repo_id == repo_id
}
}!
}

fn (branch Branch) relative() string {
Expand Down
4 changes: 2 additions & 2 deletions src/cli.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module main

import os

pub fn (mut app App) command_fetcher() {
pub fn (mut app App) command_fetcher() ! {
for {
line := os.get_line()

Expand All @@ -16,7 +16,7 @@ pub fn (mut app App) command_fetcher() {
'adduser' {
if args.len > 4 {
app.register_user(args[1], args[2], args[3], args[4..], false,
false)
false)!
println('Added user ${args[1]}')
} else {
error('Not enough arguments (3 required but only ${args.len} given)')
Expand Down
8 changes: 6 additions & 2 deletions src/comment_routes.v
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ pub fn (mut app App) handle_add_comment(username string, repo_name string) vweb.
return app.issue(username, repo_name, issue_id)
}

app.add_issue_comment(app.user.id, issue_id.int(), text)
app.add_issue_comment(app.user.id, issue_id.int(), text) or {
app.error('There was an error while inserting the comment')

return app.issue(username, repo_name, issue_id)
}

// TODO: count comments
app.increment_issue_comments(issue_id.int())
app.increment_issue_comments(issue_id.int()) or { app.info(err.str()) }

return app.redirect('/${username}/${repo_name}/issue/${issue_id}')
}
8 changes: 4 additions & 4 deletions src/comment_service.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module main

import time

fn (mut app App) add_issue_comment(author_id int, issue_id int, text string) {
fn (mut app App) add_issue_comment(author_id int, issue_id int, text string) ! {
comment := Comment{
author_id: author_id
issue_id: issue_id
Expand All @@ -12,13 +12,13 @@ fn (mut app App) add_issue_comment(author_id int, issue_id int, text string) {

sql app.db {
insert comment into Comment
}
}!
}

fn (mut app App) get_all_issue_comments(issue_id int) []Comment {
mut comments := sql app.db {
comments := sql app.db {
select from Comment where issue_id == issue_id
}
} or { []Comment{} }

return comments
}
Expand Down
24 changes: 15 additions & 9 deletions src/commit_service.v
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ fn (commit Commit) get_changes(repo Repo) []Change {
return changes
}

fn (mut app App) add_commit_if_not_exist(repo_id int, branch_id int, last_hash string, author string, author_id int, message string, date int) {
commit := sql app.db {
fn (mut app App) add_commit_if_not_exist(repo_id int, branch_id int, last_hash string, author string, author_id int, message string, date int) ! {
commits := sql app.db {
select from Commit where repo_id == repo_id && branch_id == branch_id && hash == last_hash limit 1
}
} or { []Commit{} }

if commit.id > 0 {
if commits.len > 0 {
return
}

Expand All @@ -81,33 +81,39 @@ fn (mut app App) add_commit_if_not_exist(repo_id int, branch_id int, last_hash s

sql app.db {
insert new_commit into Commit
}
}!
}

fn (mut app App) find_repo_commits_as_page(repo_id int, branch_id int, offset int) []Commit {
return sql app.db {
select from Commit where repo_id == repo_id && branch_id == branch_id order by created_at desc limit 35 offset offset
}
} or { []Commit{} }
}

fn (mut app App) get_repo_commit_count(repo_id int, branch_id int) int {
return sql app.db {
select count from Commit where repo_id == repo_id && branch_id == branch_id
}
} or { 0 }
}

fn (mut app App) find_repo_commit_by_hash(repo_id int, hash string) Commit {
commits := sql app.db {
select from Commit where repo_id == repo_id && hash == hash
}
} or { []Commit{} }
if commits.len == 1 {
return commits[0]
}
return Commit{}
}

fn (mut app App) find_repo_last_commit(repo_id int, branch_id int) Commit {
return sql app.db {
commits := sql app.db {
select from Commit where repo_id == repo_id && branch_id == branch_id order by created_at desc limit 1
} or { []Commit{} }

if commits.len == 0 {
return Commit{}
}

return commits.first()
}
Loading

0 comments on commit 1a1eb58

Please sign in to comment.