Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sorting for search result #101

Merged
merged 2 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.v
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn (mut app App) before_request() {
} or { [] }

app.most_downloaded_packages = sql app.db {
select from Package order by nr_downloads limit 10
select from Package order by downloads limit 10
} or { [] }

app.auth()
Expand Down
2 changes: 1 addition & 1 deletion migrate_mods.v
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn (mut app App) migrate_old_modules() ! {
name: mod.name
description: mod.description
url: mod.url
nr_downloads: mod.nr_downloads
ArtemkaKun marked this conversation as resolved.
Show resolved Hide resolved
downloads: mod.nr_downloads
vcs: mod.vcs
user_id: new_user2[0].id
}
Expand Down
11 changes: 5 additions & 6 deletions package.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@ struct Package {
description string
documentation string
url string
nr_downloads int
downloads int
vcs string = 'git'
user_id int
author User [fkey: 'id']
stars int
downloads int
is_flatten bool // No need to mention author of package, example `ui`
updated_at time.Time = time.now()
created_at time.Time = time.now()
}

fn (mut app App) find_all_packages() []Package {
pkgs := sql app.db {
select from Package order by nr_downloads desc
select from Package order by downloads desc
} or { [] }
println('all pkgs ${pkgs.len}')
return pkgs
Expand All @@ -34,7 +33,7 @@ fn (mut app App) find_all_packages() []Package {
fn (mut app App) find_all_packages_by_query(query string) []Package {
q := '%' + query + '%'
pkgs := sql app.db {
select from Package where name like q order by nr_downloads desc
select from Package where name like q
} or { [] }
println('found pkgs by query "${q}": ${pkgs.len}')
return pkgs
Expand All @@ -49,7 +48,7 @@ fn (mut app App) nr_packages_by_this_user(user_id int) int {

fn (mut app App) find_user_packages(user_id int) []Package {
mod := sql app.db {
select from Package where user_id == user_id order by nr_downloads desc
select from Package where user_id == user_id order by downloads desc
} or { [] }
return mod
}
Expand All @@ -67,7 +66,7 @@ fn (app &App) retrieve_package(name string) !Package {

fn (app &App) inc_nr_downloads(name string) {
sql app.db {
update Package set nr_downloads = nr_downloads + 1 where name == name
update Package set downloads = downloads + 1 where name == name
} or {}
}

Expand Down
28 changes: 27 additions & 1 deletion search.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@ import vweb
pub fn (mut app App) search() vweb.Result {
q := app.query['q']
title := if q == '' { 'All Packages' } else { 'Search Results' }
packages := app.find_all_packages_by_query(q)
unsorted_packages := app.find_all_packages_by_query(q)
packages := sort_packages(unsorted_packages) // NOTE: packages variable is used in search.html template

return $vweb.html()
}

fn sort_packages(unsorted_packages []Package) []Package {
mut packages := unsorted_packages.clone()

packages.sort_with_compare(compare_packages)

return packages
}

fn compare_packages(a &Package, b &Package) int {
if a.stars != b.stars {
return if a.stars > b.stars { -1 } else { 1 }
}

if a.downloads != b.downloads {
return if a.downloads > b.downloads { -1 } else { 1 }
}

if a.updated_at != b.updated_at {
return if a.updated_at > b.updated_at { -1 } else { 1 }
}

return a.name.compare(b.name)
}