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,25 +167,28 @@ 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) {
subdomainProjectRow, err := db.QueryOne(ctx, conn, models.Project{}, "SELECT $columns FROM handmade_project WHERE slug = $1", slug) if len(slug) > 0 && slug != models.HMNProjectSlug {
if err == nil { subdomainProjectRow, err := db.QueryOne(ctx, conn, models.Project{}, "SELECT $columns FROM handmade_project WHERE slug = $1", slug)
subdomainProject := subdomainProjectRow.(*models.Project) if err == nil {
return subdomainProject, nil subdomainProject := subdomainProjectRow.(*models.Project)
} else if !errors.Is(err, db.ErrNoMatchingRows) { return subdomainProject, nil
return nil, oops.New(err, "failed to get projects by slug") } else if !errors.Is(err, db.ErrNoMatchingRows) {
} return nil, oops.New(err, "failed to get projects by slug")
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) {
return nil, oops.New(nil, "default project didn't exist in the database")
} else { } else {
return nil, oops.New(err, "failed to get default project") 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) {
return nil, oops.New(nil, "default project didn't exist in the database")
} else {
return nil, oops.New(err, "failed to get default project")
}
}
defaultProject := defaultProjectRow.(*models.Project)
return defaultProject, nil
} }
defaultProject := defaultProjectRow.(*models.Project)
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
} }