diff --git a/resetdb.sh b/resetdb.sh old mode 100644 new mode 100755 index f8daefc5..75316cf4 --- a/resetdb.sh +++ b/resetdb.sh @@ -1,5 +1,7 @@ #!/bin/bash +set -eou pipefail + # This script is for use in local development only. It wipes the existing db, # creates a new empty one, runs the initial migration to create the schema, # and then imports actual db content on top of that. @@ -7,7 +9,8 @@ # TODO(opensource): We should adapt Asaf's seedfile command and then delete this. THIS_PATH=$(pwd) -BETA_PATH='/mnt/c/Users/bvisn/Developer/handmade/handmade-beta' +#BETA_PATH='/mnt/c/Users/bvisn/Developer/handmade/handmade-beta' +BETA_PATH='/Users/benvisness/Developer/handmade/handmade-beta' cd $BETA_PATH docker-compose down -v @@ -19,4 +22,5 @@ cd $THIS_PATH go run src/main.go migrate 2021-03-10T05:16:21Z cd $BETA_PATH -./scripts/db_import -d -n hmn_two -a ./dbdumps/hmn_pg_dump_2020-11-10 +#./scripts/db_import -d -n hmn_two -a ./dbdumps/hmn_pg_dump_2020-11-10 +./scripts/db_import -d -n hmn_two -a ./dbdumps/hmn_pg_dump_2021-04-25 diff --git a/src/models/category.go b/src/models/category.go index fd7bf91c..8bd4f7ec 100644 --- a/src/models/category.go +++ b/src/models/category.go @@ -4,6 +4,7 @@ import ( "context" "git.handmade.network/hmn/hmn/src/db" + "git.handmade.network/hmn/hmn/src/hmnurl" "github.com/jackc/pgx/v4/pgxpool" ) @@ -22,18 +23,23 @@ type Category struct { ID int `db:"id"` ParentID *int `db:"parent_id"` - ProjectID *int `db:"project_id"` + ProjectID *int `db:"project_id"` // TODO: Make not null - Slug *string `db:"slug"` - Name *string `db:"name"` - Blurb *string `db:"blurb"` + Slug *string `db:"slug"` // TODO: Make not null + Name *string `db:"name"` // TODO: Make not null + Blurb *string `db:"blurb"` // TODO: Make not null Kind CategoryType `db:"kind"` Color1 string `db:"color_1"` Color2 string `db:"color_2"` Depth int `db:"depth"` // TODO: What is this? } -func (c *Category) GetParents(ctx context.Context, conn *pgxpool.Pool) []Category { +/* +Gets the category and its parent categories, starting from the root and working toward the +category itself. Useful for breadcrumbs and the like. +*/ +func (c *Category) GetHierarchy(ctx context.Context, conn *pgxpool.Pool) []Category { + // TODO: Make this work for a whole set of categories at once. Should be doable. type breadcrumbRow struct { Cat Category `db:"cats"` } @@ -57,23 +63,58 @@ func (c *Category) GetParents(ctx context.Context, conn *pgxpool.Pool) []Categor panic(err) } + rowsSlice := rows.ToSlice() var result []Category - for _, irow := range rows.ToSlice()[1:] { - row := irow.(*breadcrumbRow) + for i := len(rowsSlice) - 1; i >= 0; i-- { + row := rowsSlice[i].(*breadcrumbRow) result = append(result, row.Cat) } return result } -// func GetCategoryUrls(cats ...*Category) map[int]string { +func GetCategoryUrls(ctx context.Context, conn *pgxpool.Pool, cats ...*Category) map[int]string { + var projectIds []int + for _, cat := range cats { + id := *cat.ProjectID -// } + alreadyInList := false + for _, otherId := range projectIds { + if otherId == id { + alreadyInList = true + break + } + } -// func makeCategoryUrl(cat *Category, subdomain string) string { -// switch cat.Kind { -// case CatTypeBlog: -// case CatTypeForum: -// } -// return hmnurl.ProjectUrl("/flooger", nil, subdomain) -// } + if !alreadyInList { + projectIds = append(projectIds, id) + } + } + + // TODO(inarray)!!!!! + + for _, cat := range cats { + hierarchy := makeCategoryUrl(cat.GetHierarchy(ctx, conn)) + } +} + +func makeCategoryUrl(cats []*Category, subdomain string) string { + path := "" + for i, cat := range cats { + if i == 0 { + switch cat.Kind { + case CatTypeBlog: + path += "/blogs" + case CatTypeForum: + path += "/forums" + // TODO: All cat types? + default: + return "" + } + } else { + path += "/" + *cat.Slug + } + } + + return hmnurl.ProjectUrl(path, nil, subdomain) +} diff --git a/src/templates/src/include/pagination.html b/src/templates/src/include/pagination.html index 0be6cf5d..edcaf0e0 100644 --- a/src/templates/src/include/pagination.html +++ b/src/templates/src/include/pagination.html @@ -1,3 +1,4 @@ +{{- /*gotype: git.handmade.network/hmn/hmn/src/templates.Pagination*/ -}}