Skip to content

Commit

Permalink
Merge pull request #24 from getAlby/task-subscriptions
Browse files Browse the repository at this point in the history
chore: rename subscription and remove sqlite
  • Loading branch information
im-adithya authored Feb 28, 2024
2 parents 20b2d80 + d67181d commit 97000a1
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Notifies about new events matching the filter provided via webhooks.
<details>
<summary>
<code>POST</code> <code><b>/subscribe</b></code>
<code>POST</code> <code><b>/subscriptions</b></code>
</summary>
#### Request Body
Expand Down Expand Up @@ -130,7 +130,7 @@ Delete previously requested subscriptions.
<details>
<summary>
<code>DELETE</code> <code><b>/subscribe/:id</b></code>
<code>DELETE</code> <code><b>/subscriptions/:id</b></code>
</summary>
#### Parameter
Expand Down
4 changes: 2 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func main() {

e.POST("/nip47/info", svc.InfoHandler)
e.POST("/nip47", svc.NIP47Handler)
e.POST("/subscribe", svc.SubscriptionHandler)
e.DELETE("/subscribe/:id", svc.StopSubscriptionHandler)
e.POST("/subscriptions", svc.SubscriptionHandler)
e.DELETE("/subscriptions/:id", svc.StopSubscriptionHandler)
e.Use(echologrus.Middleware())

//start Echo server
Expand Down
10 changes: 5 additions & 5 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func (svc *Service) StopSubscriptionHandler(c echo.Context) error {
subscription := Subscription{}
if err := svc.db.First(&subscription, subId).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return c.JSON(http.StatusBadRequest, ErrorResponse{
return c.JSON(http.StatusNotFound, ErrorResponse{
Message: "subscription does not exist",
})
} else {
Expand All @@ -422,12 +422,12 @@ func (svc *Service) StopSubscriptionHandler(c echo.Context) error {

err := svc.stopSubscription(&subscription)
if err != nil {
return c.JSON(http.StatusNotFound, ErrorResponse{
Message: err.Error(),
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: fmt.Sprintf("subscription does not exist: %s", err.Error()),
})
}

return c.JSON(http.StatusOK, fmt.Sprintf("subscription %d stopped", subId))
return c.NoContent(http.StatusNoContent)
}

func (svc *Service) stopSubscription(sub *Subscription) error {
Expand All @@ -441,7 +441,7 @@ func (svc *Service) stopSubscription(sub *Subscription) error {
}
svc.mu.Unlock()
if (!exists) {
return fmt.Errorf("cancel function of subscription doesn't exist")
return fmt.Errorf("cancel function doesn't exist")
}
return nil
}
Expand Down
17 changes: 1 addition & 16 deletions migrations/202402161653_initial_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@ package migrations

import (
_ "embed"
"log"

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

//go:embed initial_migration_postgres.sql
var initialMigrationPostgres string
//go:embed initial_migration_sqlite.sql
var initialMigrationSqlite string

var initialMigrations = map[string]string {
"postgres": initialMigrationPostgres,
"sqlite": initialMigrationSqlite,
}
var initialMigration string

// Initial migration
var _202402161653_initial_migration = &gormigrate.Migration {
Expand All @@ -25,15 +17,8 @@ var _202402161653_initial_migration = &gormigrate.Migration {
// only execute migration if subscriptions table doesn't exist
err := tx.Exec("SELECT * FROM subscriptions").Error;
if err != nil {
// find which initial migration should be executed
initialMigration := initialMigrations[tx.Dialector.Name()]
if initialMigration == "" {
log.Fatalf("unsupported database type: %s", tx.Dialector.Name())
}

return tx.Exec(initialMigration).Error
}

return nil
},
Rollback: func(tx *gorm.DB) error {
Expand Down
3 changes: 0 additions & 3 deletions migrations/initial_migration_sqlite.sql

This file was deleted.

0 comments on commit 97000a1

Please sign in to comment.