Update db.GetSong method to include song existence status

This commit is contained in:
Chigozirim Igweamaka 2024-05-11 21:32:02 +01:00
parent cefe059cda
commit 0dd3ecd2e5
2 changed files with 20 additions and 22 deletions

View file

@ -54,16 +54,12 @@ func SongKeyExists(key string) (bool, error) {
} }
defer db.Close() defer db.Close()
song, err := db.GetSongByKey(key) _, songExists, err := db.GetSongByKey(key)
if err != nil && !strings.Contains(err.Error(), "song not found") { if err != nil {
return false, err return false, err
} }
if song.Title == "" { return songExists, nil
return false, nil
}
return true, nil
} }
func YtIDExists(ytID string) (bool, error) { func YtIDExists(ytID string) (bool, error) {
@ -73,16 +69,12 @@ func YtIDExists(ytID string) (bool, error) {
} }
defer db.Close() defer db.Close()
song, err := db.GetSongByYTID(ytID) _, songExits, err := db.GetSongByYTID(ytID)
if err != nil && !strings.Contains(err.Error(), "song not found") { if err != nil {
return false, err return false, err
} }
if song.Title == "" { return songExits, nil
return false, nil
}
return true, nil
} }
/* fixes some invalid file names (windows is the capricious one) */ /* fixes some invalid file names (windows is the capricious one) */

View file

@ -2,6 +2,7 @@ package utils
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"song-recognition/models" "song-recognition/models"
"strings" "strings"
@ -166,9 +167,14 @@ type Song struct {
YouTubeID string YouTubeID string
} }
func (db *DbClient) GetSong(filterKey string, value any) (Song, error) { const FILTER_KEYS = "_id | ytID | key"
songsCollection := db.client.Database("song-recognition").Collection("songs")
func (db *DbClient) GetSong(filterKey string, value interface{}) (s Song, songExists bool, e error) {
if !strings.Contains(FILTER_KEYS, filterKey) {
return Song{}, false, errors.New("invalid filter key")
}
songsCollection := db.client.Database("song-recognition").Collection("songs")
var song bson.M var song bson.M
filter := bson.M{filterKey: value} filter := bson.M{filterKey: value}
@ -176,9 +182,9 @@ func (db *DbClient) GetSong(filterKey string, value any) (Song, error) {
err := songsCollection.FindOne(context.Background(), filter).Decode(&song) err := songsCollection.FindOne(context.Background(), filter).Decode(&song)
if err != nil { if err != nil {
if err == mongo.ErrNoDocuments { if err == mongo.ErrNoDocuments {
return Song{}, fmt.Errorf("song (%v: %v) not found", filterKey, value) return Song{}, false, nil
} }
return Song{}, fmt.Errorf("failed to retrieve song: %v", err) return Song{}, false, fmt.Errorf("failed to retrieve song: %v", err)
} }
ytID := song["ytID"].(string) ytID := song["ytID"].(string)
@ -187,18 +193,18 @@ func (db *DbClient) GetSong(filterKey string, value any) (Song, error) {
songInstance := Song{title, artist, ytID} songInstance := Song{title, artist, ytID}
return songInstance, nil return songInstance, true, nil
} }
func (db *DbClient) GetSongByID(songID uint32) (Song, error) { func (db *DbClient) GetSongByID(songID uint32) (Song, bool, error) {
return db.GetSong("_id", songID) return db.GetSong("_id", songID)
} }
func (db *DbClient) GetSongByYTID(ytID string) (Song, error) { func (db *DbClient) GetSongByYTID(ytID string) (Song, bool, error) {
return db.GetSong("ytID", ytID) return db.GetSong("ytID", ytID)
} }
func (db *DbClient) GetSongByKey(key string) (Song, error) { func (db *DbClient) GetSongByKey(key string) (Song, bool, error) {
return db.GetSong("key", key) return db.GetSong("key", key)
} }