Run go fmt, fix pagination bug, clean up code

This commit is contained in:
froge 2025-02-14 12:24:31 +10:00
parent 0c123c415b
commit dc3820d6f4
Signed by: froge
GPG key ID: A825E09930271BFA
4 changed files with 37 additions and 30 deletions

View file

@ -1,9 +1,9 @@
package main
import (
"encoding/json"
"fmt"
"gorm.io/driver/sqlite"
"encoding/json"
"gorm.io/gorm"
"log/slog"
"time"

View file

@ -27,6 +27,8 @@ func setupRouter(env *Env, spotifyID string, spotifySecret string) *gin.Engine {
r.GET("/artists/:artistID", env.getArtistByID)
r.GET("/artists", env.getArtistByName)
r.GET("/genres", env.getGenres)
// POST create endpoint
r.POST("/genres", env.createGenre)
return r
}

View file

@ -38,8 +38,12 @@ func paginator() gin.HandlerFunc {
return
}
if page < 1 || pageSize < 1 {
page, pageSize = 1, 10
if page < 1 {
page = 1
}
if pageSize < 10 {
pageSize = 10
}
// Calculate the correct SQL offset for this page
@ -123,7 +127,8 @@ func (env *Env) getArtistByName(c *gin.Context) {
// NOTE: However unicode is treated case sensitive due to very difficult conversions for some languages/characters
// NOTE: In future better DBs with unicode support such as postgres should be used
var artistProfiles []ArtistProfile
dbResult := env.db.Where("name LIKE ?", fmt.Sprintf("%%%s%%", artistName)).Preload("Genres").Offset(pageOffset).Limit(pageSize).Find(&artistProfiles)
searchString := fmt.Sprintf("%%%s%%"), artistName)
dbResult := env.db.Where("name LIKE ?", searchString).Preload("Genres").Offset(pageOffset).Limit(pageSize).Find(&artistProfiles)
if dbResult.Error != nil {
slog.Error("[GOMUSIC] Failed to query local database for artist name", "Name", artistName)
c.JSON(http.StatusInternalServerError, gin.H{"Error": "Failed to lookup name"})

View file

@ -6,8 +6,8 @@ import (
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
"strings"
"testing"
)
// Define some custom types to make response parsing easier