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) {
if len(slug) > 0 && slug != models.HMNProjectSlug {
subdomainProjectRow, err := db.QueryOne(ctx, conn, models.Project{}, "SELECT $columns FROM handmade_project WHERE slug = $1", slug)
if err == nil {
subdomainProject := subdomainProjectRow.(*models.Project)
return subdomainProject, nil
} else if !errors.Is(err, db.ErrNoMatchingRows) {
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)
if err != nil {
if errors.Is(err, db.ErrNoMatchingRows) {
@ -184,9 +187,9 @@ func FetchProjectBySlug(ctx context.Context, conn *pgxpool.Pool, slug string) (*
}
}
defaultProject := defaultProjectRow.(*models.Project)
return defaultProject, nil
}
}
func ProjectCSS(c *RequestContext) ResponseData {
color := c.URL().Query().Get("color")
@ -262,6 +265,9 @@ func LoadCommonWebsiteData(c *RequestContext) (bool, ResponseData) {
if err != nil {
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
}