From 4b4ccdfaf6e46b492626e5480f70227fa754de0c Mon Sep 17 00:00:00 2001 From: Yader Velasquez Date: Fri, 9 Feb 2024 18:12:10 -0600 Subject: [PATCH] Remove PullOrgSlug from short and links --- cmd/links/main.go | 4 +- core/routes.go | 4 +- list/routes.go | 93 +++++++++++++++++++++--------- short/routes.go | 47 ++++++++++----- templates/link_short_create.html | 2 +- templates/link_short_list.html | 8 +-- templates/listing_create.html | 6 +- templates/listing_link_create.html | 6 +- templates/listing_link_list.html | 8 +-- templates/listing_list.html | 10 ++-- 10 files changed, 122 insertions(+), 66 deletions(-) diff --git a/cmd/links/main.go b/cmd/links/main.go index 9ecb71c..6e7dd6a 100644 --- a/cmd/links/main.go +++ b/cmd/links/main.go @@ -254,9 +254,9 @@ func run() error { slack.NewService(slackService) mattermostService := e.Group("/mattermost") mattermost.NewService(mattermostService) - shortService := e.Group("/short") + shortService := e.Group("/:slug/short") short.NewService(shortService) - listService := e.Group("/list") + listService := e.Group("/:slug/list") list.NewService(listService) adminService := e.Group("/admin") admin.NewService(adminService) diff --git a/core/routes.go b/core/routes.go index 20b14c6..6ee1df2 100644 --- a/core/routes.go +++ b/core/routes.go @@ -1809,9 +1809,9 @@ func (s *Service) QRManageDetail(c echo.Context) error { var backURL string if qrCode.CodeType == models.CodeTypeList && qrCode.ListID.Valid { - backURL = c.Echo().Reverse("list:listing_qrcode_list", qrCode.ListID.Int64) + backURL = c.Echo().Reverse("list:listing_qrcode_list", org.Slug, qrCode.ListID.Int64) } else if qrCode.CodeType == models.CodeTypeShort && qrCode.ShortID.Valid { - backURL = c.Echo().Reverse("short:link_short_qrcode_list", qrCode.ShortID.Int64) + backURL = c.Echo().Reverse("short:link_short_qrcode_list", org.Slug, qrCode.ShortID.Int64) } gmap := gobwebs.Map{ diff --git a/list/routes.go b/list/routes.go index 28cae6a..6fe2e1d 100644 --- a/list/routes.go +++ b/list/routes.go @@ -64,7 +64,10 @@ func (s *Service) ListingLinksUpdate(c echo.Context) error { gctx := c.(*server.Context) user := gctx.User.(*models.User) - orgSlug := links.PullOrgSlug(c) + orgSlug := c.Param("slug") + if orgSlug == "" { + return echo.NotFoundHandler(c) + } opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"o.slug": orgSlug}, @@ -138,7 +141,8 @@ func (s *Service) ListingLinksUpdate(c echo.Context) error { "form": form, "listingLinkID": listingLink.ID, "navFlag": "listing", - "back": c.Echo().Reverse(s.RouteName("listing_links"), listing.ID), + "back": c.Echo().Reverse(s.RouteName("listing_links"), org.Slug, listing.ID), + "org": org, } req := c.Request() if req.Method == http.MethodPost { @@ -186,7 +190,7 @@ func (s *Service) ListingLinksUpdate(c echo.Context) error { messages.Success(c, lt.Translate("A link was successfully created.")) return c.Redirect(http.StatusMovedPermanently, - c.Echo().Reverse(s.RouteName("listing_links"), listing.ID)) + c.Echo().Reverse(s.RouteName("listing_links"), org.Slug, listing.ID)) } return gctx.Render(http.StatusOK, "listing_link_create.html", gmap) @@ -201,6 +205,10 @@ func (s *Service) ListingLinksDelete(c echo.Context) error { if err != nil { return echo.NotFoundHandler(c) } + orgSlug := c.Param("slug") + if orgSlug == "" { + return echo.NotFoundHandler(c) + } gctx := c.(*server.Context) lt := localizer.GetSessionLocalizer(c) pd := localizer.NewPageData(lt.Translate("Delete List")) @@ -232,16 +240,15 @@ func (s *Service) ListingLinksDelete(c echo.Context) error { messages.Error(c, lt.Translate("Something went wrong. The link could not be deleted.")) redirect := c.Request().Header.Get("Referer") if redirect == "" { - redirect = c.Echo().Reverse(s.RouteName("listing_links"), id) + redirect = c.Echo().Reverse(s.RouteName("listing_links"), orgSlug, id) } return c.Redirect(http.StatusMovedPermanently, redirect) } messages.Success(c, lt.Translate("Link successfully deleted")) return c.Redirect(http.StatusMovedPermanently, - c.Echo().Reverse(s.RouteName("listing_links"), id)) + c.Echo().Reverse(s.RouteName("listing_links"), orgSlug, id)) } - orgSlug := links.PullOrgSlug(c) opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"o.slug": orgSlug}, @@ -300,8 +307,8 @@ func (s *Service) ListingLinksDelete(c echo.Context) error { pd.Data["message"] = lt.Translate("Do you really whant to delete this link") gmap := gobwebs.Map{ "pd": pd, - "url": c.Echo().Reverse(s.RouteName("listing_link_delete"), listing.ID, listingLink.ID), - "back": c.Echo().Reverse(s.RouteName("listing_links"), listing.ID), + "url": c.Echo().Reverse(s.RouteName("listing_link_delete"), org.Slug, listing.ID, listingLink.ID), + "back": c.Echo().Reverse(s.RouteName("listing_links"), org.Slug, listing.ID), } return gctx.Render(http.StatusOK, "element_delete.html", gmap) @@ -315,7 +322,10 @@ func (s *Service) ListingLinksCreate(c echo.Context) error { gctx := c.(*server.Context) user := gctx.User.(*models.User) - orgSlug := links.PullOrgSlug(c) + orgSlug := c.Param("slug") + if orgSlug == "" { + return echo.NotFoundHandler(c) + } opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"o.slug": orgSlug}, @@ -367,6 +377,7 @@ func (s *Service) ListingLinksCreate(c echo.Context) error { "listing": listing, "form": form, "navFlag": "listing", + "org": org, } req := c.Request() if req.Method == http.MethodPost { @@ -414,7 +425,7 @@ func (s *Service) ListingLinksCreate(c echo.Context) error { messages.Success(c, lt.Translate("A link was successfully created.")) return c.Redirect(http.StatusMovedPermanently, - c.Echo().Reverse(s.RouteName("listing_links"), listing.ID)) + c.Echo().Reverse(s.RouteName("listing_links"), org.Slug, listing.ID)) } return gctx.Render(http.StatusOK, "listing_link_create.html", gmap) @@ -428,7 +439,10 @@ func (s *Service) ListingLinksManage(c echo.Context) error { gctx := c.(*server.Context) user := gctx.User.(*models.User) - orgSlug := links.PullOrgSlug(c) + orgSlug := c.Param("slug") + if orgSlug == "" { + return echo.NotFoundHandler(c) + } opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"o.slug": orgSlug}, @@ -543,6 +557,7 @@ func (s *Service) ListingLinksManage(c echo.Context) error { "listingLinks": result.Listing.Links, "listing": listing, "navFlag": "listing", + "org": org, } if result.Listing.PageInfo.HasPrevPage { gmap["prevURL"] = links.GetPaginationParams("prev", "", "", result.Listing.PageInfo.Cursor) @@ -562,7 +577,10 @@ func (s *Service) ListingUpdate(c echo.Context) error { gctx := c.(*server.Context) user := gctx.User.(*models.User) - orgSlug := links.PullOrgSlug(c) + orgSlug := c.Param("slug") + if orgSlug == "" { + return echo.NotFoundHandler(c) + } opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"o.slug": orgSlug}, @@ -728,7 +746,9 @@ func (s *Service) ListingUpdate(c echo.Context) error { return err } messages.Success(c, lt.Translate("Listing successfully updated.")) - return c.Redirect(http.StatusMovedPermanently, c.Echo().Reverse(s.RouteName("listing_list"))) + return c.Redirect( + http.StatusMovedPermanently, + c.Echo().Reverse(s.RouteName("listing_list"), org.Slug)) } form := &ListingForm{ @@ -758,7 +778,10 @@ func (s *Service) ListingCreate(c echo.Context) error { gctx := c.(*server.Context) user := gctx.User.(*models.User) - orgSlug := links.PullOrgSlug(c) + orgSlug := c.Param("slug") + if orgSlug == "" { + return echo.NotFoundHandler(c) + } opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"o.slug": orgSlug}, @@ -898,7 +921,9 @@ func (s *Service) ListingCreate(c echo.Context) error { } messages.Success(c, lt.Translate("A list was successfully created.")) - return c.Redirect(http.StatusMovedPermanently, c.Echo().Reverse(s.RouteName("listing_list"))) + return c.Redirect( + http.StatusMovedPermanently, + c.Echo().Reverse(s.RouteName("listing_list"), org.Slug)) } // NOTE should we include BillinStatusOpenSource? if links.BillingEnabled(gctx.Server.Config) && org.IsRestricted([]int{models.BillingStatusFree}) { @@ -921,7 +946,10 @@ func (s *Service) ListingList(c echo.Context) error { gctx := c.(*server.Context) user := gctx.User.(*models.User) - orgSlug := links.PullOrgSlug(c) + orgSlug := c.Param("slug") + if orgSlug == "" { + return echo.NotFoundHandler(c) + } opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"o.slug": orgSlug}, @@ -1042,6 +1070,10 @@ func (s *Service) ListingDelete(c echo.Context) error { if err != nil { return echo.NotFoundHandler(c) } + orgSlug := c.Param("slug") + if orgSlug == "" { + return echo.NotFoundHandler(c) + } gctx := c.(*server.Context) lt := localizer.GetSessionLocalizer(c) pd := localizer.NewPageData(lt.Translate("Delete List")) @@ -1072,19 +1104,18 @@ func (s *Service) ListingDelete(c echo.Context) error { messages.Error(c, lt.Translate("Something went wrong. The link could not be deleted.")) redirect := c.Request().Header.Get("Referer") if redirect == "" { - redirect = c.Echo().Reverse(s.RouteName("listing_list")) + redirect = c.Echo().Reverse(s.RouteName("listing_list"), orgSlug) } return c.Redirect(http.StatusMovedPermanently, redirect) } messages.Success(c, lt.Translate("List successfully deleted")) return c.Redirect(http.StatusMovedPermanently, - c.Echo().Reverse(s.RouteName("listing_list"))) + c.Echo().Reverse(s.RouteName("listing_list"), orgSlug)) } // NOTE: All these validations already happen in the resolver for the POST request user := gctx.User.(*models.User) - orgSlug := links.PullOrgSlug(c) opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"o.slug": orgSlug}, @@ -1128,8 +1159,8 @@ func (s *Service) ListingDelete(c echo.Context) error { pd.Data["message"] = lt.Translate("Do you really whant to delete this link") gmap := gobwebs.Map{ "pd": pd, - "url": c.Echo().Reverse(s.RouteName("listing_delete"), listing.ID), - "back": c.Echo().Reverse(s.RouteName("listing_list")), + "url": c.Echo().Reverse(s.RouteName("listing_delete"), org.Slug, listing.ID), + "back": c.Echo().Reverse(s.RouteName("listing_list"), org.Slug), } return gctx.Render(http.StatusOK, "element_delete.html", gmap) } @@ -1141,7 +1172,10 @@ func (s *Service) ListingQRCodeCreate(c echo.Context) error { } gctx := c.(*server.Context) user := gctx.User.(*models.User) - orgSlug := links.PullOrgSlug(c) + orgSlug := c.Param("slug") + if orgSlug == "" { + return echo.NotFoundHandler(c) + } opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"o.slug": orgSlug}, @@ -1188,7 +1222,7 @@ func (s *Service) ListingQRCodeCreate(c echo.Context) error { gmap := gobwebs.Map{ "pd": pd, "listing": listing, - "backURL": c.Echo().Reverse(s.RouteName("listing_qrcode_list"), listing.ID), + "backURL": c.Echo().Reverse(s.RouteName("listing_qrcode_list"), orgSlug, listing.ID), "navFlag": "listing", } form := &ListingQRForm{} @@ -1251,7 +1285,7 @@ func (s *Service) ListingQRCodeCreate(c echo.Context) error { } messages.Success(c, lt.Translate("QR Code succesfully created")) gmap["qrCode"] = result.QRCode - gmap["backURL"] = c.Echo().Reverse(s.RouteName("listing_qrcode_list"), listing.ID) + gmap["backURL"] = c.Echo().Reverse(s.RouteName("listing_qrcode_list"), org.Slug, listing.ID) } return gctx.Render(http.StatusOK, "qrcode_detail.html", gmap) } @@ -1263,7 +1297,10 @@ func (s *Service) ListingQRCodeList(c echo.Context) error { } gctx := c.(*server.Context) user := gctx.User.(*models.User) - orgSlug := links.PullOrgSlug(c) + orgSlug := c.Param("slug") + if orgSlug == "" { + return echo.NotFoundHandler(c) + } opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"o.slug": orgSlug}, @@ -1346,9 +1383,9 @@ func (s *Service) ListingQRCodeList(c echo.Context) error { "object": listing, "qrList": result.QRList, "showAddBtn": len(result.QRList) < 5, - "addURL": c.Echo().Reverse(s.RouteName("listing_qrcode_create"), listing.ID), - "nextDeleteURL": c.Echo().Reverse(s.RouteName("listing_qrcode_list"), listing.ID), - "back": c.Echo().Reverse(s.RouteName("listing_list")), + "addURL": c.Echo().Reverse(s.RouteName("listing_qrcode_create"), org.Slug, listing.ID), + "nextDeleteURL": c.Echo().Reverse(s.RouteName("listing_qrcode_list"), org.Slug, listing.ID), + "back": c.Echo().Reverse(s.RouteName("listing_list"), org.Slug), "navFlag": "listing", } return gctx.Render(http.StatusOK, "qrcodes_list.html", gmap) diff --git a/short/routes.go b/short/routes.go index 27a7673..8976385 100644 --- a/short/routes.go +++ b/short/routes.go @@ -47,7 +47,10 @@ func (s *Service) RegisterRoutes() { func (s *Service) LinkShortList(c echo.Context) error { gctx := c.(*server.Context) user := gctx.User.(*models.User) - slug := links.PullOrgSlug(c) + slug := c.Param("slug") + if slug == "" { + return echo.NotFoundHandler(c) + } opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"o.slug": slug}, @@ -168,10 +171,13 @@ func (s *Service) LinkShortList(c echo.Context) error { func (s *Service) LinkShortCreate(c echo.Context) error { gctx := c.(*server.Context) user := gctx.User.(*models.User) + slug := c.Param("slug") + if slug == "" { + return echo.NotFoundHandler(c) + } - orgSlug := links.PullOrgSlug(c) opts := &database.FilterOptions{ - Filter: sq.Expr("o.slug = ?", orgSlug), + Filter: sq.Expr("o.slug = ?", slug), } orgs, err := models.GetOrganizations(c.Request().Context(), opts) if err != nil { @@ -301,7 +307,10 @@ func (s *Service) LinkShortUpdate(c echo.Context) error { return echo.NotFoundHandler(c) } - orgSlug := links.PullOrgSlug(c) + orgSlug := c.Param("slug") + if orgSlug == "" { + return echo.NotFoundHandler(c) + } opts := &database.FilterOptions{ Filter: sq.Expr("o.slug = ?", orgSlug), } @@ -465,6 +474,10 @@ func (s *Service) LinkShortDelete(c echo.Context) error { if err != nil { return echo.NotFoundHandler(c) } + orgSlug := c.Param("slug") + if orgSlug == "" { + return echo.NotFoundHandler(c) + } gctx := c.(*server.Context) lt := localizer.GetSessionLocalizer(c) pd := localizer.NewPageData(lt.Translate("Delete Short Link")) @@ -495,14 +508,14 @@ func (s *Service) LinkShortDelete(c echo.Context) error { messages.Error(c, lt.Translate("Something went wrong. The link could not be deleted.")) redirect := c.Request().Header.Get("Referer") if redirect == "" { - redirect = c.Echo().Reverse(s.RouteName("link_short_list")) + redirect = c.Echo().Reverse(s.RouteName("link_short_list"), orgSlug) } return c.Redirect(http.StatusMovedPermanently, redirect) } messages.Success(c, lt.Translate("Short Link successfully deleted")) return c.Redirect(http.StatusMovedPermanently, - c.Echo().Reverse(s.RouteName("link_short_list"))) + c.Echo().Reverse(s.RouteName("link_short_list"), orgSlug)) } // NOTE: All these validations already happen in the resolver for the POST request @@ -529,8 +542,8 @@ func (s *Service) LinkShortDelete(c echo.Context) error { pd.Data["message"] = lt.Translate("Do you really whant to delete this link") gmap := gobwebs.Map{ "pd": pd, - "url": c.Echo().Reverse(s.RouteName("link_short_delete"), linkShort.ID), - "back": c.Echo().Reverse(s.RouteName("link_short_list")), + "url": c.Echo().Reverse(s.RouteName("link_short_delete"), orgSlug, linkShort.ID), + "back": c.Echo().Reverse(s.RouteName("link_short_list"), orgSlug), } return gctx.Render(http.StatusOK, "element_delete.html", gmap) } @@ -637,7 +650,10 @@ func (s *Service) LinkShortQRCodeList(c echo.Context) error { } gctx := c.(*server.Context) user := gctx.User.(*models.User) - orgSlug := links.PullOrgSlug(c) + orgSlug := c.Param("slug") + if orgSlug == "" { + return echo.NotFoundHandler(c) + } opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"o.slug": orgSlug}, @@ -720,10 +736,10 @@ func (s *Service) LinkShortQRCodeList(c echo.Context) error { "object": short, "qrList": result.QRList, "showAddBtn": len(result.QRList) < 5, - "addURL": c.Echo().Reverse(s.RouteName("link_short_qrcode_create"), short.ID), - "nextDeleteURL": c.Echo().Reverse(s.RouteName("link_short_qrcode_list"), short.ID), + "addURL": c.Echo().Reverse(s.RouteName("link_short_qrcode_create"), orgSlug, short.ID), + "nextDeleteURL": c.Echo().Reverse(s.RouteName("link_short_qrcode_list"), orgSlug, short.ID), "navFlag": "short", - "back": c.Echo().Reverse(s.RouteName("link_short_list")), + "back": c.Echo().Reverse(s.RouteName("link_short_list"), orgSlug), } return gctx.Render(http.StatusOK, "qrcodes_list.html", gmap) } @@ -735,7 +751,10 @@ func (s *Service) LinkShortQRCodeCreate(c echo.Context) error { } gctx := c.(*server.Context) user := gctx.User.(*models.User) - orgSlug := links.PullOrgSlug(c) + orgSlug := c.Param("slug") + if orgSlug == "" { + return echo.NotFoundHandler(c) + } opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"o.slug": orgSlug}, @@ -782,7 +801,7 @@ func (s *Service) LinkShortQRCodeCreate(c echo.Context) error { gmap := gobwebs.Map{ "pd": pd, "short": short, - "backURL": c.Echo().Reverse(s.RouteName("link_short_qrcode_list"), short.ID), + "backURL": c.Echo().Reverse(s.RouteName("link_short_qrcode_list"), orgSlug, short.ID), "navFlag": "short", } form := &ShortQRForm{} diff --git a/templates/link_short_create.html b/templates/link_short_create.html index 6d741ed..7af4ed6 100644 --- a/templates/link_short_create.html +++ b/templates/link_short_create.html @@ -5,7 +5,7 @@
-