Skip to content

Commit

Permalink
fix: crud for tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
thuongtruong109 committed Dec 13, 2024
1 parent 3c02ea7 commit d6deb04
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 246 deletions.
11 changes: 6 additions & 5 deletions cmd/app.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cmd

import (
"github.com/thuongtruong109/soundlib/internal/playlists"
// "github.com/thuongtruong109/soundlib/internal/tracks"
"github.com/thuongtruong109/soundlib/internal/albums"
"github.com/thuongtruong109/soundlib/internal/artists"
"github.com/thuongtruong109/soundlib/internal/genres"
"github.com/thuongtruong109/soundlib/internal/playlists"
"github.com/thuongtruong109/soundlib/internal/tracks"

"github.com/thuongtruong109/soundlib/pkg/common"
"github.com/thuongtruong109/soundlib/pkg/helpers"
Expand All @@ -28,10 +28,11 @@ func App() {
playlistUC := playlists.NewPlaylistUsecase()
playlistHandler := playlists.NewPlaylistHandler(*playlistUC, *helper)

// trackUC := tracks.NewTrackUsecase(*tracks.NewTrackRepository(), *helper)
// trackHandler := tracks.NewTrackHandler(*trackUC, *helper, *common.NewCommonHandler(*helper, "Tracks"))
trackRepo := tracks.NewTrackRepository()
trackUC := tracks.NewTrackUsecase(*trackRepo, *helper)
trackHandler := tracks.NewTrackHandler(*trackUC, *helper, *common.NewCommonHandler(*helper, "Tracks"))

exe := NewDelivery(*albumHandler, *artistHandler, *genreHandler, *playlistHandler, *helper)
exe := NewDelivery(*helper, *albumHandler, *artistHandler, *genreHandler, *playlistHandler, *trackHandler)
/**trackHandler)*/

exe.Execution()
Expand Down
28 changes: 16 additions & 12 deletions cmd/delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/thuongtruong109/soundlib/internal/playlists"
// "github.com/thuongtruong109/soundlib/internal/tracks"
"github.com/thuongtruong109/soundlib/internal/tracks"
"github.com/thuongtruong109/soundlib/pkg/constants"
"github.com/thuongtruong109/soundlib/pkg/helpers"

Expand All @@ -12,22 +12,22 @@ import (
)

type Delivery struct {
helper helpers.Helper
albumHandler albums.AlbumHandler
artistHandler artists.ArtistHandler
genreHandler genres.GenreHandler
playlistHandler playlists.PlaylistHandler
// trackHandler tracks.TrackHandler
helper helpers.Helper
trackHandler tracks.TrackHandler
}

func NewDelivery(albumHandler albums.AlbumHandler, artistHandler artists.ArtistHandler, genreHandler genres.GenreHandler, playlistHandler playlists.PlaylistHandler /*trackHandler tracks.TrackHandler,*/, helper helpers.Helper) *Delivery {
func NewDelivery(helper helpers.Helper, albumHandler albums.AlbumHandler, artistHandler artists.ArtistHandler, genreHandler genres.GenreHandler, playlistHandler playlists.PlaylistHandler, trackHandler tracks.TrackHandler) *Delivery {
return &Delivery{
helper: helper,
albumHandler: albumHandler,
// artistHandler: artistHandler,
artistHandler: artistHandler,
genreHandler: genreHandler,
playlistHandler: playlistHandler,
// trackHandler: trackHandler,
helper: helper,
trackHandler: trackHandler,
}
}

Expand All @@ -39,24 +39,28 @@ func (h *Delivery) HandleOption(option int8) {
4: h.albumHandler.DeleteAlbum,
5: h.albumHandler.UpdateAlbum,
6: h.albumHandler.GetTracksOfAlbum,

7: h.artistHandler.CreateArtist,
8: h.artistHandler.GetArtists,
9: h.artistHandler.GetArtist,
// 10: h.artistHandler.GetAlbumsOfArtist,
// 11: h.artistHandler.GetTracksOfArtist,
12: h.artistHandler.DeleteArtist,
13: h.artistHandler.UpdateArtist,

14: h.genreHandler.CreateGenre,
15: h.genreHandler.GetGenres,
16: h.genreHandler.GetGenre,
17: h.genreHandler.DeleteGenre,
18: h.genreHandler.UpdateGenre,

// 19: h.genreHandler.GetTracksOfGenre,
// 20: h.trackHandler.CreateTrack,
// 21: h.trackHandler.GetTracks,
// 22: h.trackHandler.GetTrack,
// 23: h.trackHandler.DeleteTrack,
// 24: h.trackHandler.UpdateTrack,
20: h.trackHandler.CreateTrack,
21: h.trackHandler.GetTracks,
22: h.trackHandler.GetTrack,
23: h.trackHandler.DeleteTrack,
24: h.trackHandler.UpdateTrack,

25: h.playlistHandler.CreatePlaylist,
26: h.playlistHandler.GetPlaylists,
27: h.playlistHandler.GetPlaylist,
Expand Down
5 changes: 3 additions & 2 deletions cmd/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package cmd

import (
"fmt"
"github.com/thuongtruong109/soundlib/pkg/constants"

gu_console "github.com/thuongtruong109/gouse/console"
gu_shared "github.com/thuongtruong109/gouse/shared"
"github.com/thuongtruong109/soundlib/pkg/constants"
)

func (d *Delivery) Execution() {
Expand All @@ -29,4 +30,4 @@ func (d *Delivery) Execution() {
break
}
}
}
}
2 changes: 1 addition & 1 deletion database/artists.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"id":"1731822899559367500","username":"w","full_name":"w","bio":"ew","avatar_url":"eqeqw","debut_at":"2024-11-17T12:54:59.559Z"}]
[{"id":"1731822899559367500","username":"w","full_name":"w","bio":"ew","avatar_url":"eqeqw","debut_at":"2024-11-17T12:54:59.559Z"},{"id":"1734055056352055600","username":"jiefr","full_name":"jgotege","bio":"jgegege","avatar_url":"eg9giegher","debut_at":"2024-12-13T08:57:36.352Z"}]
1 change: 1 addition & 0 deletions database/tracks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":"1734056816899324200","name":"title","play_count":999,"duration":12,"file_url":"http","created_at":"2024-12-13T09:26:56.899Z","genre_id":"id","artist_id":"aid"}]
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/thuongtruong109/soundlib

go 1.20
go 1.23

require (
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/jedib0t/go-pretty/v6 v6.4.6
github.com/thuongtruong109/gouse v0.0.0-20240709131131-080109e15476
github.com/thuongtruong109/gouse v0.1.0
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
)

Expand All @@ -16,8 +16,8 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/text v0.16.0 // indirect
)
12 changes: 7 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyex
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/thuongtruong109/gouse v0.0.0-20240709131131-080109e15476 h1:vBCVnxAOczzqBUobXn5kVmOk3+MCpVL02ntP3QlqLE8=
github.com/thuongtruong109/gouse v0.0.0-20240709131131-080109e15476/go.mod h1:79vQ38zBdDAynxA/cjdqLzqsx53PmZA1rXcw5Hy4zYw=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/thuongtruong109/gouse v0.1.0 h1:Wxk+zmXceL1f7NeqMdUmdl6hhVQtWzW/Dx8j+itHcoI=
github.com/thuongtruong109/gouse v0.1.0/go.mod h1:HsqBRrlT3r5S+/XtXycoOki/mj4NwYWRhYnSngx2WdU=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand Down Expand Up @@ -67,8 +69,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
98 changes: 49 additions & 49 deletions internal/tracks/handler.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
package tracks

// import (
// "github.com/thuongtruong109/soundlib/pkg/helpers"
// "github.com/thuongtruong109/soundlib/pkg/constants"
// "github.com/thuongtruong109/soundlib/internal/common"
// )

// type TrackHandler struct {
// uc TrackUsecase
// helper helpers.Helper
// ch common.CommonHandler
// }

// func NewTrackHandler(uc TrackUsecase, helper helpers.Helper, ch common.CommonHandler) *TrackHandler {
// return &TrackHandler{
// uc: uc,
// helper: helper,
// ch: ch,
// }
// }

// func (u *TrackHandler) GetTracks() {
// result, err := u.uc.GetTracks()
// u.ch.ErrorWrapper(constants.GET_FAILED, err)
// u.ch.SuccessDataWrapper(constants.GET_SUCCESS, result)
// }

// func (u *TrackHandler) GetTrack() {
// result, err := u.uc.GetTrack()
// u.ch.ErrorWrapper(constants.GET_FAILED, err)
// u.ch.SuccessDataWrapper(constants.GET_SUCCESS, result)
// }

// func (u *TrackHandler) CreateTrack() {
// result, err := u.uc.CreateTrack()
// u.ch.ErrorWrapper(constants.CREATE_FAILED, err)
// u.ch.SuccessDataWrapper(constants.CREATE_SUCCESS, result)
// }

// func (u *TrackHandler) DeleteTrack() {
// err := u.uc.DeleteTrack()
// u.ch.ErrorWrapper(constants.DELETE_FAILED, err)
// u.helper.OutputSuccess(constants.DELETE_SUCCESS)
// }

// func (u *TrackHandler) UpdateTrack() {
// result, err := u.uc.UpdateTrack()
// u.ch.ErrorWrapper(constants.UPDATE_FAILED, err)
// u.ch.SuccessDataWrapper(constants.UPDATE_SUCCESS, result)
// }
import (
"github.com/thuongtruong109/soundlib/pkg/common"
"github.com/thuongtruong109/soundlib/pkg/constants"
"github.com/thuongtruong109/soundlib/pkg/helpers"
)

type TrackHandler struct {
uc TrackUsecase
helper helpers.Helper
ch common.CommonHandler
}

func NewTrackHandler(uc TrackUsecase, helper helpers.Helper, ch common.CommonHandler) *TrackHandler {
return &TrackHandler{
uc: uc,
helper: helper,
ch: ch,
}
}

func (u *TrackHandler) GetTracks() {
result, time, err := u.uc.GetTracks()
u.ch.ErrorWrapper(constants.GET_FAILED, err)
u.ch.SuccessDataWrapper(constants.GET_SUCCESS, result, time)
}

func (u *TrackHandler) GetTrack() {
result, time, err := u.uc.GetTrack()
u.ch.ErrorWrapper(constants.GET_FAILED, err)
u.ch.SuccessDataWrapper(constants.GET_SUCCESS, result, time)
}

func (u *TrackHandler) CreateTrack() {
result, time, err := u.uc.CreateTrack()
u.ch.ErrorWrapper(constants.CREATE_FAILED, err)
u.ch.SuccessDataWrapper(constants.CREATE_SUCCESS, result, time)
}

func (u *TrackHandler) DeleteTrack() {
time, err := u.uc.DeleteTrack()
u.ch.ErrorWrapper(constants.DELETE_FAILED, err)
u.ch.SuccessNoDataWrapper(constants.DELETE_SUCCESS, time)
}

func (u *TrackHandler) UpdateTrack() {
result, time, err := u.uc.UpdateTrack()
u.ch.ErrorWrapper(constants.UPDATE_FAILED, err)
u.ch.SuccessDataWrapper(constants.UPDATE_SUCCESS, result, time)
}

// func (u *TrackHandler) GetAlbumsOfTrack() {
// u.helper.OutputSuccess("GetAlbumsOfTrack")
Expand Down
5 changes: 5 additions & 0 deletions internal/tracks/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func (tr *TrackRepository) CreateTrack(newTrack *Track) (*Track, error) {

trackInit = append(trackInit, newTrack)

err := io.WriteFileObj(constants.TRACK_PATH, trackInit)
if err != nil {
return nil, err
}

return newTrack, nil
}

Expand Down
Loading

0 comments on commit d6deb04

Please sign in to comment.