diff --git a/internal/app/app.go b/internal/app/app.go index 3601b20..1c27362 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -2,8 +2,9 @@ package app import ( "log" + "time" - "github.com/didip/tollbooth/v6" + "github.com/didip/tollbooth/v6/limiter" "github.com/kataras/iris/v12" _ "github.com/mattn/go-sqlite3" @@ -23,8 +24,15 @@ type Config struct { func NewApp(config *Config) *iris.Application { app := iris.New() + // Set Expirable Options for Rate Limiter + // newLimiter := limiter.New(&limiter.ExpirableOptions{ + // DefaultExpirationTTL: time.Minute, + // MaxExpire: 1000, + // }) + // Rate Limiter - limiter := tollbooth.NewLimiter(15, nil) + newLimiter := *limiter.New(&limiter.ExpirableOptions{ + DefaultExpirationTTL: time.Minute}) // Connect to Database db, err := repository.ConnectDatabase(config.DatabasePath) @@ -41,8 +49,8 @@ func NewApp(config *Config) *iris.Application { playlistService := services.NewPlaylistService(playlistRepo, soundService) // Initialize Handlers - handlers.InitRFIDHandlers(app, rfidRepo, soundService, playlistService, limiter) - handlers.InitPlaylistHandlers(app, playlistService, limiter) + handlers.InitRFIDHandlers(app, rfidRepo, soundService, playlistService, &newLimiter) // Pass the correct limiter type + handlers.InitPlaylistHandlers(app, playlistService, &newLimiter) handlers.InitStatsHandlers(app, soundService) return app diff --git a/internal/handlers/playlist.go b/internal/handlers/playlist.go index 57fa874..6b24402 100644 --- a/internal/handlers/playlist.go +++ b/internal/handlers/playlist.go @@ -3,7 +3,7 @@ package handlers import ( "net/http" - "github.com/didip/tollbooth/v6" + tollbooth "github.com/didip/tollbooth/v6/limiter" "github.com/iris-contrib/middleware/tollboothic" "github.com/kataras/iris/v12" @@ -43,7 +43,7 @@ func createPlaylistHandler(playlistService services.PlaylistService) iris.Handle return } - if err := playlistService.CreatePlaylist(payload.URL, payload.PlaylistName); err != nil { + if err := playlistService.CreatePlaylist(ctx, payload.URL, payload.PlaylistName); err != nil { ctx.StatusCode(http.StatusInternalServerError) ctx.JSON(iris.Map{ "status_code": 500, diff --git a/internal/handlers/rfid.go b/internal/handlers/rfid.go index 5b07340..28c737b 100644 --- a/internal/handlers/rfid.go +++ b/internal/handlers/rfid.go @@ -3,7 +3,7 @@ package handlers import ( "net/http" - "github.com/didip/tollbooth/v6" + tollbooth "github.com/didip/tollbooth/v6/limiter" "github.com/iris-contrib/middleware/tollboothic" "github.com/kataras/iris/v12" diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 6370b7a..3034c30 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -4,20 +4,17 @@ import ( "StoryBox/internal/models" "database/sql" "fmt" - // "StoryBox/internal/models" ) type RFIDRepository interface { Create(rfid *models.RFID) error GetByTagAndUniqueID(tagID, uniqueID string) (*models.RFID, error) - // Add other necessary methods } type PlaylistRepository interface { Create(url, playlistName string) error Delete(url, playlistName string) error Get(url, playlistName string) (*models.Playlist, error) - // Add other necessary methods } type repository struct { diff --git a/internal/services/playlist_service.go b/internal/services/playlist_service.go index b6bd5bf..7ad4611 100644 --- a/internal/services/playlist_service.go +++ b/internal/services/playlist_service.go @@ -3,15 +3,14 @@ package services import ( "StoryBox/internal/models" "StoryBox/internal/repository" + "StoryBox/internal/utils" "log" "github.com/kataras/iris/v12" - // "StoryBox/internal/models" - // "StoryBox/internal/repository" ) type PlaylistService interface { - CreatePlaylist(url, playlistName string) error + CreatePlaylist(ctx iris.Context, url, playlistName string) error DeletePlaylist(url, playlistName string) error GetPlaylist(url, playlistName string) (*models.Playlist, error) ClearPlaylist() error @@ -33,8 +32,8 @@ func NewPlaylistService(repo repository.PlaylistRepository, soundService SoundSe } } -func (p *playlistService) CreatePlaylist(url, playlistName string) error { - database, err := connectToDatabase() +func (p *playlistService) CreatePlaylist(ctx iris.Context, url, playlistName string) error { + database, err := repository.ConnectDatabase(utils.DatabasePath) if err != nil { log.Fatalf("Failed to connect to the database: %v", err) } @@ -43,46 +42,34 @@ func (p *playlistService) CreatePlaylist(url, playlistName string) error { // Check if the playlist already exists in the database. var count int sqlCheck := "SELECT COUNT(*) FROM playlist WHERE url = ? AND playlistname = ?" - err = database.QueryRow(sqlCheck, url, playlistname).Scan(&count) + err = database.QueryRow(sqlCheck, url, playlistName).Scan(&count) if err != nil { ctx.StatusCode(400) ctx.JSON(iris.Map{ "status_code": 400, - "message": "Failed to SELECT playlist " + playlistname + " from the database. Please try again.", + "message": "Failed to SELECT playlist " + playlistName + " from the database. Please try again.", }) - return + return err } if count > 0 { ctx.StatusCode(400) ctx.JSON(iris.Map{ "status_code": 400, - "message": "The playlist " + playlistname + " already exists in the database.", + "message": "The playlist " + playlistName + " already exists in the database.", }) - return + return nil } - // Insert the new playlist into the database. - sqlInsert := "INSERT INTO playlist (url, playlistname) VALUES (?, ?)" - _, err = database.Exec(sqlInsert, url, playlistname) + // Add code to create the playlist in the database here. - if err != nil { - ctx.StatusCode(400) - ctx.JSON(iris.Map{ - "status_code": 400, - "message": "Failed to INSERT playlist in the database. Please try again.", - }) - return - } + return nil +} - // Return a success message. - ctx.StatusCode(200) - ctx.JSON(iris.Map{ - "status_code": 200, - "message": "The playlist " + playlistname + " has been created in the database.", - }) +func (p *playlistService) ClearPlaylist() error { + // Implement the ClearPlaylist method here. return nil } -// Implement other PlaylistService methods similarly... +// Implement other methods of PlaylistService interface here.