Get to a real stupid stopping point

inarray!!
This commit is contained in:
Ben Visness 2021-04-25 21:13:26 -05:00
parent e7ff342842
commit b13dd4bdb5
4 changed files with 65 additions and 19 deletions

8
resetdb.sh Normal file → Executable file
View File

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
set -eou pipefail
# This script is for use in local development only. It wipes the existing db, # 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, # creates a new empty one, runs the initial migration to create the schema,
# and then imports actual db content on top of that. # 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. # TODO(opensource): We should adapt Asaf's seedfile command and then delete this.
THIS_PATH=$(pwd) 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 cd $BETA_PATH
docker-compose down -v docker-compose down -v
@ -19,4 +22,5 @@ cd $THIS_PATH
go run src/main.go migrate 2021-03-10T05:16:21Z go run src/main.go migrate 2021-03-10T05:16:21Z
cd $BETA_PATH 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

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"git.handmade.network/hmn/hmn/src/db" "git.handmade.network/hmn/hmn/src/db"
"git.handmade.network/hmn/hmn/src/hmnurl"
"github.com/jackc/pgx/v4/pgxpool" "github.com/jackc/pgx/v4/pgxpool"
) )
@ -22,18 +23,23 @@ type Category struct {
ID int `db:"id"` ID int `db:"id"`
ParentID *int `db:"parent_id"` ParentID *int `db:"parent_id"`
ProjectID *int `db:"project_id"` ProjectID *int `db:"project_id"` // TODO: Make not null
Slug *string `db:"slug"` Slug *string `db:"slug"` // TODO: Make not null
Name *string `db:"name"` Name *string `db:"name"` // TODO: Make not null
Blurb *string `db:"blurb"` Blurb *string `db:"blurb"` // TODO: Make not null
Kind CategoryType `db:"kind"` Kind CategoryType `db:"kind"`
Color1 string `db:"color_1"` Color1 string `db:"color_1"`
Color2 string `db:"color_2"` Color2 string `db:"color_2"`
Depth int `db:"depth"` // TODO: What is this? 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 { type breadcrumbRow struct {
Cat Category `db:"cats"` Cat Category `db:"cats"`
} }
@ -57,23 +63,58 @@ func (c *Category) GetParents(ctx context.Context, conn *pgxpool.Pool) []Categor
panic(err) panic(err)
} }
rowsSlice := rows.ToSlice()
var result []Category var result []Category
for _, irow := range rows.ToSlice()[1:] { for i := len(rowsSlice) - 1; i >= 0; i-- {
row := irow.(*breadcrumbRow) row := rowsSlice[i].(*breadcrumbRow)
result = append(result, row.Cat) result = append(result, row.Cat)
} }
return result 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 { if !alreadyInList {
// switch cat.Kind { projectIds = append(projectIds, id)
// case CatTypeBlog: }
// case CatTypeForum: }
// }
// return hmnurl.ProjectUrl("/flooger", nil, subdomain) // 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)
}

View File

@ -1,3 +1,4 @@
{{- /*gotype: git.handmade.network/hmn/hmn/src/templates.Pagination*/ -}}
<div class="pagination"> <div class="pagination">
{{ if gt .Current 1 }} {{ if gt .Current 1 }}
<a class="button" href="{{ .PreviousUrl }}">Prev</a> <a class="button" href="{{ .PreviousUrl }}">Prev</a>

View File

@ -127,7 +127,7 @@ func Feed(c *RequestContext) ResponseData {
hasRead = true hasRead = true
} }
parents := postResult.Cat.GetParents(c.Context(), c.Conn) parents := postResult.Cat.GetHierarchy(c.Context(), c.Conn)
logging.Debug().Interface("parents", parents).Msg("") logging.Debug().Interface("parents", parents).Msg("")
var breadcrumbs []templates.Breadcrumb var breadcrumbs []templates.Breadcrumb