Redirect to homepage on bad slug

This commit is contained in:
Asaf Gartner 2021-05-25 16:26:12 +03:00
parent 9c19484333
commit e3257bb383
1 changed files with 22 additions and 16 deletions

View File

@ -167,14 +167,17 @@ func getBaseData(c *RequestContext) templates.BaseData {
} }
func FetchProjectBySlug(ctx context.Context, conn *pgxpool.Pool, slug string) (*models.Project, error) { func FetchProjectBySlug(ctx context.Context, conn *pgxpool.Pool, slug string) (*models.Project, error) {
if len(slug) > 0 && slug != models.HMNProjectSlug {
subdomainProjectRow, err := db.QueryOne(ctx, conn, models.Project{}, "SELECT $columns FROM handmade_project WHERE slug = $1", slug) subdomainProjectRow, err := db.QueryOne(ctx, conn, models.Project{}, "SELECT $columns FROM handmade_project WHERE slug = $1", slug)
if err == nil { if err == nil {
subdomainProject := subdomainProjectRow.(*models.Project) subdomainProject := subdomainProjectRow.(*models.Project)
return subdomainProject, nil return subdomainProject, nil
} else if !errors.Is(err, db.ErrNoMatchingRows) { } else if !errors.Is(err, db.ErrNoMatchingRows) {
return nil, oops.New(err, "failed to get projects by slug") return nil, oops.New(err, "failed to get projects by slug")
} else {
return nil, nil
} }
} else {
defaultProjectRow, err := db.QueryOne(ctx, conn, models.Project{}, "SELECT $columns FROM handmade_project WHERE id = $1", models.HMNProjectID) defaultProjectRow, err := db.QueryOne(ctx, conn, models.Project{}, "SELECT $columns FROM handmade_project WHERE id = $1", models.HMNProjectID)
if err != nil { if err != nil {
if errors.Is(err, db.ErrNoMatchingRows) { if errors.Is(err, db.ErrNoMatchingRows) {
@ -184,8 +187,8 @@ func FetchProjectBySlug(ctx context.Context, conn *pgxpool.Pool, slug string) (*
} }
} }
defaultProject := defaultProjectRow.(*models.Project) defaultProject := defaultProjectRow.(*models.Project)
return defaultProject, nil return defaultProject, nil
}
} }
func ProjectCSS(c *RequestContext) ResponseData { func ProjectCSS(c *RequestContext) ResponseData {
@ -262,6 +265,9 @@ func LoadCommonWebsiteData(c *RequestContext) (bool, ResponseData) {
if err != nil { if err != nil {
return false, ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to fetch current project")) return false, ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to fetch current project"))
} }
if dbProject == nil {
return false, c.Redirect(hmnurl.BuildHomepage(), http.StatusSeeOther)
}
c.CurrentProject = dbProject c.CurrentProject = dbProject
} }