From cefe059cda78e16c98978d9a835c74071a240730 Mon Sep 17 00:00:00 2001 From: Chigozirim Igweamaka Date: Sat, 11 May 2024 08:47:01 +0100 Subject: [PATCH] refactor: remove redundancy; add more detail to error message --- utils/dbClient.go | 53 ++++++++--------------------------------------- 1 file changed, 9 insertions(+), 44 deletions(-) diff --git a/utils/dbClient.go b/utils/dbClient.go index b506a41..8842012 100644 --- a/utils/dbClient.go +++ b/utils/dbClient.go @@ -166,17 +166,17 @@ type Song struct { YouTubeID string } -func (db *DbClient) GetSongByID(songID uint32) (Song, error) { +func (db *DbClient) GetSong(filterKey string, value any) (Song, error) { songsCollection := db.client.Database("song-recognition").Collection("songs") var song bson.M - filter := bson.M{"_id": songID} + filter := bson.M{filterKey: value} err := songsCollection.FindOne(context.Background(), filter).Decode(&song) if err != nil { if err == mongo.ErrNoDocuments { - return Song{}, fmt.Errorf("song not found") + return Song{}, fmt.Errorf("song (%v: %v) not found", filterKey, value) } return Song{}, fmt.Errorf("failed to retrieve song: %v", err) } @@ -190,51 +190,16 @@ func (db *DbClient) GetSongByID(songID uint32) (Song, error) { return songInstance, nil } +func (db *DbClient) GetSongByID(songID uint32) (Song, error) { + return db.GetSong("_id", songID) +} + func (db *DbClient) GetSongByYTID(ytID string) (Song, error) { - songsCollection := db.client.Database("song-recognition").Collection("songs") - - var song bson.M - - filter := bson.M{"ytID": ytID} - - err := songsCollection.FindOne(context.Background(), filter).Decode(&song) - if err != nil { - if err == mongo.ErrNoDocuments { - return Song{}, fmt.Errorf("song not found") - } - return Song{}, fmt.Errorf("failed to retrieve song: %v", err) - } - - title := strings.Split(song["key"].(string), "---")[0] - artist := strings.Split(song["key"].(string), "---")[1] - - songInstance := Song{title, artist, song["ytID"].(string)} - - return songInstance, nil + return db.GetSong("ytID", ytID) } func (db *DbClient) GetSongByKey(key string) (Song, error) { - songsCollection := db.client.Database("song-recognition").Collection("songs") - - var song bson.M - - filter := bson.M{"key": key} - - err := songsCollection.FindOne(context.Background(), filter).Decode(&song) - if err != nil { - if err == mongo.ErrNoDocuments { - return Song{}, fmt.Errorf("song not found") - } - return Song{}, fmt.Errorf("failed to retrieve song: %v", err) - } - - ytID := song["ytID"].(string) - title := strings.Split(song["key"].(string), "---")[0] - artist := strings.Split(song["key"].(string), "---")[1] - - songInstance := Song{title, artist, ytID} - - return songInstance, nil + return db.GetSong("key", key) } func (db *DbClient) DeleteSongByID(songID uint32) error {