-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from mbaraa/feat/plays-history
Feat: recent plays history
- Loading branch information
Showing
9 changed files
with
225 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package models | ||
|
||
import "time" | ||
|
||
type History struct { | ||
Id uint `gorm:"primaryKey;autoIncrement"` | ||
SongId uint | ||
ProfileId uint | ||
CreatedAt time.Time | ||
} | ||
|
||
func (h History) GetId() uint { | ||
return h.Id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package history | ||
|
||
import ( | ||
"dankmuzikk/db" | ||
"dankmuzikk/entities" | ||
"dankmuzikk/models" | ||
"time" | ||
) | ||
|
||
type Service struct { | ||
repo db.UnsafeCRUDRepo[models.History] | ||
songRepo db.GetterRepo[models.Song] | ||
} | ||
|
||
func New(repo db.UnsafeCRUDRepo[models.History], songRepo db.GetterRepo[models.Song]) *Service { | ||
return &Service{ | ||
repo: repo, | ||
songRepo: songRepo, | ||
} | ||
} | ||
|
||
func (h *Service) AddSongToHistory(songYtId string, profileId uint) error { | ||
song, err := h.songRepo.GetByConds("yt_id = ?", songYtId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return h.repo.Add(&models.History{ | ||
ProfileId: profileId, | ||
SongId: song[0].Id, | ||
}) | ||
} | ||
|
||
func (h *Service) Get(profileId uint) ([]entities.Song, error) { | ||
gigaQuery := `SELECT yt_id, title, artist, thumbnail_url, duration, h.created_at | ||
FROM | ||
histories h JOIN songs | ||
ON | ||
songs.id = h.song_id | ||
WHERE h.profile_id = ? | ||
ORDER BY h.created_at DESC;` | ||
|
||
rows, err := h.repo. | ||
GetDB(). | ||
Raw(gigaQuery, profileId). | ||
Rows() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rows.Close() | ||
|
||
songs := make([]entities.Song, 0) | ||
for rows.Next() { | ||
var song entities.Song | ||
var addedAt time.Time | ||
err = rows.Scan(&song.YtId, &song.Title, &song.Artist, &song.ThumbnailUrl, &song.Duration, &addedAt) | ||
if err != nil { | ||
continue | ||
} | ||
song.AddedAt = whenDidItHappen(addedAt) | ||
songs = append(songs, song) | ||
} | ||
|
||
return songs, nil | ||
} | ||
|
||
func whenDidItHappen(t time.Time) string { | ||
now := time.Now().UTC() | ||
switch { | ||
case t.Day() == now.Day() && t.Month() == now.Month() && t.Year() == now.Year(): | ||
return "today" | ||
case t.Day()+1 == now.Day() && t.Month() == now.Month() && t.Year() == now.Year(): | ||
return "yesterday" | ||
case t.Day()+5 < now.Day() && t.Month() == now.Month() && t.Year() == now.Year(): | ||
return "last week" | ||
case t.Day() == now.Day() && t.Month()+1 == now.Month() && t.Year() == now.Year(): | ||
return "last month" | ||
default: | ||
return t.Format("2, January, 2006") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters