add asset id redirecting

This commit is contained in:
Bradley Nelson 2022-12-31 05:34:38 +00:00
parent 58d6f9a28c
commit 0d3cbbc016
No known key found for this signature in database
GPG key ID: EB209B0420B6230F
5 changed files with 86 additions and 0 deletions

4
.gitignore vendored
View file

@ -45,3 +45,7 @@ node_modules
.output .output
.env .env
dist dist
.pnpm-store
devData
backend/app/api/app

View file

@ -0,0 +1,45 @@
package assetIds
import (
"net/http"
"strconv"
"strings"
"github.com/rs/zerolog/log"
"github.com/go-chi/chi/v5"
"github.com/hay-kot/homebox/backend/internal/data/repo"
"github.com/hay-kot/homebox/backend/pkgs/server"
)
func HandleAssetRedirect(repos *repo.AllRepos) server.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
// Get the asset ID from the URL
assetIdParam := chi.URLParam(r, "id")
assetIdParam = strings.ReplaceAll(assetIdParam, "-", "") // Remove dashes
// Convert the asset ID to an int64
assetId, err := strconv.ParseInt(assetIdParam, 10, 64)
if err != nil {
return err
}
// Get the asset from the database
itemIds, err := repos.Items.GetIDsByAssetID(r.Context(), repo.AssetID(assetId));
if err != nil {
return err
}
// check if we got more than one item
if len(itemIds) > 1 {
log.Err(err).Msg("More than one item found for asset ID")
return server.Respond(w, http.StatusInternalServerError, "More than one item found for asset ID")
}
// check if we got any items
if len(itemIds) == 0 {
log.Err(err).Msg("No items found for asset ID")
return server.Respond(w, http.StatusNotFound, "No items found for asset ID")
}
http.Redirect(w, r, "/item/" + itemIds[0].String(), http.StatusSeeOther)
return nil
}
}

View file

@ -10,6 +10,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"github.com/hay-kot/homebox/backend/app/api/handlers/assetIds"
"github.com/hay-kot/homebox/backend/app/api/handlers/debughandlers" "github.com/hay-kot/homebox/backend/app/api/handlers/debughandlers"
v1 "github.com/hay-kot/homebox/backend/app/api/handlers/v1" v1 "github.com/hay-kot/homebox/backend/app/api/handlers/v1"
_ "github.com/hay-kot/homebox/backend/app/api/static/docs" _ "github.com/hay-kot/homebox/backend/app/api/static/docs"
@ -123,6 +124,8 @@ func (a *app) mountRoutes(repos *repo.AllRepos) {
a.mwAuthToken, a.mwRoles(RoleModeOr, authroles.RoleUser.String(), authroles.RoleAttachments.String()), a.mwAuthToken, a.mwRoles(RoleModeOr, authroles.RoleUser.String(), authroles.RoleAttachments.String()),
) )
a.server.Get("/a/{id}", assetIds.HandleAssetRedirect(a.repos))
a.server.NotFound(notFoundHandler()) a.server.NotFound(notFoundHandler())
} }

View file

@ -283,6 +283,10 @@ func (e *ItemsRepository) GetOneByGroup(ctx context.Context, gid, id uuid.UUID)
return e.getOne(ctx, item.ID(id), item.HasGroupWith(group.ID(gid))) return e.getOne(ctx, item.ID(id), item.HasGroupWith(group.ID(gid)))
} }
func (e *ItemsRepository) GetIDsByAssetID(ctx context.Context, assetID AssetID) ([]uuid.UUID, error) {
return e.db.Item.Query().Where(item.AssetID(int(assetID))).Order(ent.Desc(item.FieldCreatedAt)).IDs(ctx)
}
// QueryByGroup returns a list of items that belong to a specific group based on the provided query. // QueryByGroup returns a list of items that belong to a specific group based on the provided query.
func (e *ItemsRepository) QueryByGroup(ctx context.Context, gid uuid.UUID, q ItemQuery) (PaginationResult[ItemSummary], error) { func (e *ItemsRepository) QueryByGroup(ctx context.Context, gid uuid.UUID, q ItemQuery) (PaginationResult[ItemSummary], error) {
qb := e.db.Item.Query().Where( qb := e.db.Item.Query().Where(

View file

@ -107,6 +107,36 @@ func TestItemsRepository_GetAll(t *testing.T) {
} }
} }
func TestItemsRepository_GetIDsByAssetID(t *testing.T) {
useItems(t, 4)
items, err := tRepos.Items.GetIDsByAssetID(context.Background(), 0)
assert.NoError(t, err)
assert.Equal(t, 4, len(items), "Two items are returned when there are many with the same asset Id")
item1 := useItems(t, 1)[0]
item1Update := ItemUpdate{
ID: item1.ID,
AssetID: 1,
Name: "note-important",
Description: "This is a note",
LocationID: item1.Location.ID,
}
_, err = tRepos.Items.UpdateByGroup(context.Background(), tGroup.ID, item1Update)
assert.NoError(t, err)
items, err = tRepos.Items.GetIDsByAssetID(context.Background(), 1)
assert.NoError(t, err)
assert.Equal(t, 1, len(items), "One item is returned when there is only one with the same asset Id")
assert.Equal(t, item1.ID, items[0], "The item returned is the one with the same asset Id")
}
func TestItemsRepository_Create(t *testing.T) { func TestItemsRepository_Create(t *testing.T) {
location, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory()) location, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
assert.NoError(t, err) assert.NoError(t, err)