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 package main
import ( import (
"encoding/json"
"fmt" "fmt"
"gorm.io/driver/sqlite" "gorm.io/driver/sqlite"
"encoding/json"
"gorm.io/gorm" "gorm.io/gorm"
"log/slog" "log/slog"
"time" "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/:artistID", env.getArtistByID)
r.GET("/artists", env.getArtistByName) r.GET("/artists", env.getArtistByName)
r.GET("/genres", env.getGenres) r.GET("/genres", env.getGenres)
// POST create endpoint
r.POST("/genres", env.createGenre) r.POST("/genres", env.createGenre)
return r return r
} }

View file

@ -17,7 +17,7 @@ func paginator() gin.HandlerFunc {
queryPageSize := c.DefaultQuery("page_size", "10") queryPageSize := c.DefaultQuery("page_size", "10")
// add some defaults here if user gives us empty values // add some defaults here if user gives us empty values
if queryPage == ""{ if queryPage == "" {
queryPage = "1" queryPage = "1"
} }
if queryPageSize == "" { if queryPageSize == "" {
@ -38,8 +38,12 @@ func paginator() gin.HandlerFunc {
return return
} }
if page < 1 || pageSize < 1 { if page < 1 {
page, pageSize = 1, 10 page = 1
}
if pageSize < 10 {
pageSize = 10
} }
// Calculate the correct SQL offset for this page // 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: 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 // NOTE: In future better DBs with unicode support such as postgres should be used
var artistProfiles []ArtistProfile 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 { if dbResult.Error != nil {
slog.Error("[GOMUSIC] Failed to query local database for artist name", "Name", artistName) slog.Error("[GOMUSIC] Failed to query local database for artist name", "Name", artistName)
c.JSON(http.StatusInternalServerError, gin.H{"Error": "Failed to lookup name"}) c.JSON(http.StatusInternalServerError, gin.H{"Error": "Failed to lookup name"})
@ -156,12 +161,12 @@ func (env *Env) getGenres(c *gin.Context) {
c.JSON(http.StatusOK, genreList) c.JSON(http.StatusOK, genreList)
} }
func (env *Env) createGenre(c * gin.Context) { func (env *Env) createGenre(c *gin.Context) {
var userGenre Genre var userGenre Genre
if c.ShouldBind(&userGenre) == nil { if c.ShouldBind(&userGenre) == nil {
// If we didn't get the correct data abort early // If we didn't get the correct data abort early
if userGenre.Name == "" { if userGenre.Name == "" {
c.JSON(http.StatusBadRequest, gin.H{"Error":"Could not find genre name"}) c.JSON(http.StatusBadRequest, gin.H{"Error": "Could not find genre name"})
return return
} }
dbResult := env.db.Clauses(clause.OnConflict{ dbResult := env.db.Clauses(clause.OnConflict{
@ -175,5 +180,5 @@ func (env *Env) createGenre(c * gin.Context) {
c.String(http.StatusOK, "Success") c.String(http.StatusOK, "Success")
return return
} }
c.JSON(http.StatusInternalServerError, gin.H{"Error":"Failed to parse input data"}) c.JSON(http.StatusInternalServerError, gin.H{"Error": "Failed to parse input data"})
} }

View file

@ -6,8 +6,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing"
"strings" "strings"
"testing"
) )
// Define some custom types to make response parsing easier // Define some custom types to make response parsing easier
@ -216,7 +216,7 @@ func TestGetPaginatedArtistByName(t *testing.T) {
Name: testName, Name: testName,
SpotifyID: testName, SpotifyID: testName,
Popularity: i, Popularity: i,
Genres: []Genre{ Genre{Name: testName} }, Genres: []Genre{Genre{Name: testName}},
} }
testData = append(testData, testArtist) testData = append(testData, testArtist)
} }