Skip to content

Commit

Permalink
Make favorites unique, and add timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteStash committed Jan 9, 2025
1 parent ba7cc98 commit f54c444
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
5 changes: 3 additions & 2 deletions pkg/api/resolver_mutation_studio.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,12 @@ func (r *mutationResolver) FavoriteStudio(ctx context.Context, id uuid.UUID, fav
return fmt.Errorf("studio is deleted, unable to make favorite")
}

studioFavorite := models.StudioFavorite{StudioID: id, UserID: user.ID}
if favorite {
err := jqb.AddStudioFavorite(models.StudioFavorite{StudioID: id, UserID: user.ID})
err := jqb.AddStudioFavorite(studioFavorite)
return err
}
return jqb.DestroyStudioFavorite(models.StudioFavorite{StudioID: id, UserID: user.ID})
return jqb.DestroyStudioFavorite(studioFavorite)
})
return err == nil, err
}
2 changes: 1 addition & 1 deletion pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/jmoiron/sqlx"
)

var appSchemaVersion uint = 46
var appSchemaVersion uint = 47

var databaseProviders map[string]databaseProvider

Expand Down
28 changes: 28 additions & 0 deletions pkg/database/migrations/postgres/47_favorite_unique.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
DELETE FROM "performer_favorites" PF
WHERE EXISTS (
SELECT FROM "performer_favorites"
WHERE performer_id = PF.performer_id
AND user_id = PF.user_id
AND ctid < PF.ctid
);

CREATE UNIQUE INDEX "performer_favorites_unique_idx" ON "performer_favorites" (performer_id, user_id);

DROP INDEX performer_favorites_idx;

DELETE FROM "studio_favorites" SF
WHERE EXISTS (
SELECT FROM "studio_favorites"
WHERE studio_id = SF.studio_id
AND user_id = SF.user_id
AND ctid < SF.ctid
);

CREATE UNIQUE INDEX "studio_favorites_unique_idx" ON "studio_favorites" (studio_id, user_id);

DROP INDEX studio_favorites_idx;

ALTER TABLE "performer_favorites" ADD COLUMN "created_at" TIMESTAMP;
ALTER TABLE "performer_favorites" ALTER "created_at" SET DEFAULT NOW();
ALTER TABLE "studio_favorites" ADD COLUMN "created_at" TIMESTAMP;
ALTER TABLE "studio_favorites" ALTER "created_at" SET DEFAULT NOW();
10 changes: 6 additions & 4 deletions pkg/models/model_joins.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,15 @@ func ParseURLInput(input []*URLInput) []*URL {
}

type PerformerFavorite struct {
PerformerID uuid.UUID `db:"performer_id" json:"performer_id"`
UserID uuid.UUID `db:"user_id" json:"user_id"`
PerformerID uuid.UUID `db:"performer_id" json:"performer_id"`
UserID uuid.UUID `db:"user_id" json:"user_id"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
}

type StudioFavorite struct {
StudioID uuid.UUID `db:"studio_id" json:"studio_id"`
UserID uuid.UUID `db:"user_id" json:"user_id"`
StudioID uuid.UUID `db:"studio_id" json:"studio_id"`
UserID uuid.UUID `db:"user_id" json:"user_id"`
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
}

type UserNotification struct {
Expand Down

0 comments on commit f54c444

Please sign in to comment.