Skip to content

Commit

Permalink
Update package references for tollbooth middleware in handlers/playli…
Browse files Browse the repository at this point in the history
…st.go, /handlers/rfid.go and app/app.go to v6
  • Loading branch information
ozfive committed Nov 20, 2024
1 parent 62a9a98 commit 28b7b66
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 38 deletions.
16 changes: 12 additions & 4 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/handlers/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/rfid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
3 changes: 0 additions & 3 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 15 additions & 28 deletions internal/services/playlist_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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.

0 comments on commit 28b7b66

Please sign in to comment.