scan.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package repository
  2. import (
  3. "fmt"
  4. "github.com/felamaslen/go-music-player/pkg/config"
  5. "github.com/felamaslen/go-music-player/pkg/database"
  6. "github.com/felamaslen/go-music-player/pkg/logger"
  7. "github.com/felamaslen/go-music-player/pkg/read"
  8. )
  9. func InsertMusicIntoDatabase(songs chan *read.Song) {
  10. var l = logger.CreateLogger(config.GetConfig().LogLevel)
  11. db := database.GetConnection()
  12. for {
  13. select {
  14. case song, more := <- songs:
  15. if !more {
  16. l.Verbose("Finished inserting songs\n")
  17. return
  18. }
  19. l.Debug("Adding song: %v\n", song)
  20. duration := "NULL"
  21. if song.DurationOk {
  22. duration = fmt.Sprintf("%d", song.Duration)
  23. }
  24. query, err := db.Query(
  25. "insert into songs (title, artist, album, duration, base_path, relative_path) values ($1, $2, $3, $4, $5, $6)",
  26. song.Title,
  27. song.Artist,
  28. song.Album,
  29. duration,
  30. song.BasePath,
  31. song.RelativePath,
  32. )
  33. query.Close()
  34. if err == nil {
  35. l.Info("Added %s\n", song.RelativePath)
  36. } else {
  37. l.Error("Error inserting record: %s\n", err)
  38. }
  39. }
  40. }
  41. }