Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add uuids to subscriptions #37

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/nostr/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Subscription struct {
Search string
CreatedAt time.Time
UpdatedAt time.Time
Uuid string `gorm:"type:uuid;default:gen_random_uuid()"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this have an index then automatically?


// TODO: fix an elegant solution to store datatypes
IdsString string
Expand Down Expand Up @@ -160,6 +161,6 @@ type SubscriptionRequest struct {
}

type SubscriptionResponse struct {
SubscriptionId uint `json:"subscription_id"`
SubscriptionId string `json:"subscription_id"`
WebhookUrl string `json:"webhookUrl"`
}
9 changes: 3 additions & 6 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"
"os"
"os/signal"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -334,7 +333,7 @@ func (svc *Service) SubscriptionHandler(c echo.Context) error {
}

return c.JSON(http.StatusOK, SubscriptionResponse{
SubscriptionId: subscription.ID,
SubscriptionId: subscription.Uuid,
WebhookUrl: requestData.WebhookUrl,
})
}
Expand Down Expand Up @@ -403,12 +402,10 @@ func (svc *Service) handleSubscription(ctx context.Context, subscription *Subscr
}

func (svc *Service) StopSubscriptionHandler(c echo.Context) error {
id := c.Param("id")
uint64Id, _ := strconv.ParseUint(id, 10, 64)
subId := uint(uint64Id)
uuid := c.Param("id")

subscription := Subscription{}
if err := svc.db.First(&subscription, subId).Error; err != nil {
if err := svc.db.Where("uuid = ?", uuid).First(&subscription).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return c.JSON(http.StatusNotFound, ErrorResponse{
Message: "subscription does not exist",
Expand Down
17 changes: 17 additions & 0 deletions migrations/202404021628_add_uuid_to_subscriptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package migrations

import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)

// Add UUID column to subscriptions table
var _202404021628_add_uuid_to_subscriptions = &gormigrate.Migration{
ID: "202404021628_add_uuid_to_subscriptions",
Migrate: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE subscriptions ADD COLUMN uuid UUID DEFAULT gen_random_uuid()").Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE subscriptions DROP COLUMN IF EXISTS uuid").Error
},
}
1 change: 1 addition & 0 deletions migrations/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func Migrate(db *gorm.DB) error {

m := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{
_202402161653_initial_migration,
_202404021628_add_uuid_to_subscriptions,
})

return m.Migrate()
Expand Down
Loading