Skip to content

Commit

Permalink
build: update models
Browse files Browse the repository at this point in the history
  • Loading branch information
thuongtruong109 committed Nov 15, 2024
1 parent 56dfec2 commit 6866f54
Show file tree
Hide file tree
Showing 20 changed files with 222 additions and 177 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
run:
go run main.go

build:
go build ./...
12 changes: 6 additions & 6 deletions cmd/app.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cmd

import (
"github.com/thuongtruong109/soundlib/internal/handlers"
"github.com/thuongtruong109/soundlib/internal/usecases"
"github.com/thuongtruong109/soundlib/internal/playlists"
"github.com/thuongtruong109/soundlib/internal/tracks"

"github.com/thuongtruong109/soundlib/internal/albums"
"github.com/thuongtruong109/soundlib/internal/artists"
Expand All @@ -26,11 +26,11 @@ func App() {
genreUC := genres.NewGenreUsecase(*genreRepo, *helper)
genreHandler := genres.NewGenreHandler(*genreUC, *helper, *common.NewCommonHandler(*helper, "Genres"))

playlistUC := usecases.NewPlaylistUsecase()
playlistHandler := handlers.NewPlaylistHandler(*playlistUC, *helper)
playlistUC := playlists.NewPlaylistUsecase()
playlistHandler := playlists.NewPlaylistHandler(*playlistUC, *helper)

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

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

Expand Down
33 changes: 17 additions & 16 deletions cmd/delivery.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package cmd

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

Expand All @@ -14,12 +15,12 @@ type Delivery struct {
albumHandler albums.AlbumHandler
artistHandler artists.ArtistHandler
genreHandler genres.GenreHandler
playlistHandler handlers.PlaylistHandler
trackHandler handlers.TrackHandler
playlistHandler playlists.PlaylistHandler
trackHandler tracks.TrackHandler
helper helpers.Helper
}

func NewDelivery(albumHandler albums.AlbumHandler, artistHandler artists.ArtistHandler, genreHandler genres.GenreHandler, playlistHandler handlers.PlaylistHandler, trackHandler handlers.TrackHandler, helper helpers.Helper) *Delivery {
func NewDelivery(albumHandler albums.AlbumHandler, artistHandler artists.ArtistHandler, genreHandler genres.GenreHandler, playlistHandler playlists.PlaylistHandler, trackHandler tracks.TrackHandler, helper helpers.Helper) *Delivery {
return &Delivery{
albumHandler: albumHandler,
artistHandler: artistHandler,
Expand All @@ -32,25 +33,25 @@ func NewDelivery(albumHandler albums.AlbumHandler, artistHandler artists.ArtistH

func (h *Delivery) HandleOption(option int8) {
optionHandlers := map[int8]func(){
1: h.albumHandler.CreateAlbum,
2: h.albumHandler.GetAlbums,
3: h.albumHandler.GetAlbum,
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,
1: h.albumHandler.CreateAlbum,
2: h.albumHandler.GetAlbums,
3: h.albumHandler.GetAlbum,
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,
// 19: h.genreHandler.GetTracksOfGenre,
20: h.trackHandler.CreateTrack,
21: h.trackHandler.GetTracks,
22: h.trackHandler.GetTrack,
Expand Down
16 changes: 11 additions & 5 deletions internal/albums/model.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package albums

import (
"time"

"github.com/thuongtruong109/soundlib/internal/artists"
)

type Album struct {
ID string `json:"id"`
Title string `json:"title"`
Duration float32 `json:"duration"`
Year int `json:"year"`
ID string `json:"id"`
Name string `json:"name"`
Cover string `json:"cover"`
CreatedAt time.Time `json:"created_at"`

ArtistID int `json:"artist_id"`
OwnerID artists.Artist `json:"owner_id"`
}
8 changes: 0 additions & 8 deletions internal/artists/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,3 @@ func (u *ArtistHandler) UpdateArtist() {
u.ch.ErrorWrapper(constants.UPDATE_FAILED, err)
u.ch.SuccessWrapper(constants.UPDATE_SUCCESS, result)
}

func (u *ArtistHandler) GetAlbumsOfArtist() {
u.helper.OutputSuccess("GetAlbumsOfArtist")
}

func (u *ArtistHandler) GetTracksOfArtist() {
u.helper.OutputSuccess("GetSongsOfArtist")
}
8 changes: 6 additions & 2 deletions internal/artists/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package artists

type Artist struct {
ID string `json:"id"`
Name string `json:"name"`
ID string `json:"id"`
Username string `json:"username"`
FullName string `json:"full_name"`
Bio string `json:"bio"`
AvatarUrl string `json:"avatar_url"`
DebutAt string `json:"debut_at"`
}
60 changes: 43 additions & 17 deletions internal/artists/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package artists
import (
"fmt"

gu_date "github.com/thuongtruong109/gouse/date"
gu_helper "github.com/thuongtruong109/gouse/helper"
"github.com/thuongtruong109/soundlib/pkg/constants"
"github.com/thuongtruong109/soundlib/pkg/helpers"
Expand Down Expand Up @@ -32,7 +33,7 @@ func (a *ArtistUsecase) GetArtists() ([]string, error) {

var output []string
for _, v := range result {
output = append(output, fmt.Sprintf("ID: %s, Name: %s", v.ID, v.Name))
output = append(output, fmt.Sprintf("ID: %s, Username: %s, Full Name: %s, Bio: %s, Avatar URL: %s, Debut At: %s", v.ID, v.Username, v.FullName, v.Bio, v.AvatarUrl, v.DebutAt))
}

return output, nil
Expand All @@ -52,19 +53,32 @@ func (a *ArtistUsecase) GetArtist() ([]string, error) {
return nil, fmt.Errorf(constants.NOT_FOUND_DATA)
}

output := []string{fmt.Sprintf("ID: %s, Name: %s", result.ID, result.Name)}
output := []string{fmt.Sprintf("ID: %s, Username: %s, Full Name: %s, Bio: %s, Avatar URL: %s, Debut At: %s", result.ID, result.Username, result.FullName, result.Bio, result.AvatarUrl, result.DebutAt)}

return output, nil
}

func (a *ArtistUsecase) CreateArtist() ([]string, error) {
var name string
fmt.Print("» Enter name: ")
fmt.Scanln(&name)
var username, fullName, bio, avatarUrl string
fmt.Print("» Enter username: ")
fmt.Scanln(&username)

fmt.Print("» Enter full name: ")
fmt.Scanln(&fullName)

fmt.Print("» Enter bio: ")
fmt.Scanln(&bio)

fmt.Print("» Enter avatar URL: ")
fmt.Scanln(&avatarUrl)

artist := &Artist{
ID: gu_helper.RandomID(),
Name: name,
ID: gu_helper.RandomID(),
Username: username,
FullName: fullName,
Bio: bio,
AvatarUrl: avatarUrl,
DebutAt: gu_date.ISO(),
}

result, err := helpers.QueryTimeTwoOutputWithParams[*Artist, *Artist](a.repo.CreateArtist)(artist)
Expand Down Expand Up @@ -95,17 +109,29 @@ func (a *ArtistUsecase) DeleteArtist() error {
}

func (a *ArtistUsecase) UpdateArtist() ([]string, error) {
var id string
var id, username, fullName, bio, avatarUrl string
fmt.Print("» Enter ID: ")
fmt.Scanln(&id)

var name string
fmt.Print("» Enter name: ")
fmt.Scanln(&name)
fmt.Print("» Enter username: ")
fmt.Scanln(&username)

fmt.Print("» Enter full name: ")
fmt.Scanln(&fullName)

fmt.Print("» Enter bio: ")
fmt.Scanln(&bio)

fmt.Print("» Enter avatar URL: ")
fmt.Scanln(&avatarUrl)

newArtist := &Artist{
ID: id,
Name: name,
ID: id,
Username: username,
FullName: fullName,
Bio: bio,
AvatarUrl: avatarUrl,
DebutAt: gu_date.ISO(),
}

result, err := helpers.QueryTimeTwoOutputWithParams[*Artist, *Artist](a.repo.UpdateArtist)(newArtist)
Expand All @@ -122,10 +148,10 @@ func (a *ArtistUsecase) UpdateArtist() ([]string, error) {
return output, nil
}

func (a *ArtistUsecase) GetAlbumsOfArtist() string {
return "Albums of Artist"
func (a *ArtistUsecase) GetTrackOfArtist() string {
return "Tracks of Artist"
}

func (a *ArtistUsecase) GetTracksOfArtist() string {
return "Tracks of Artist"
func (a *ArtistUsecase) GetTracksOfGenre() string {
return "Tracks of Genre"
}
16 changes: 6 additions & 10 deletions internal/genres/handler.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package genres

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

type GenreHandler struct {
uc GenreUsecase
uc GenreUsecase
helper helpers.Helper
ch common.CommonHandler
ch common.CommonHandler
}

func NewGenreHandler(uc GenreUsecase, helper helpers.Helper, ch common.CommonHandler) *GenreHandler {
return &GenreHandler{
uc: uc,
uc: uc,
helper: helper,
ch: ch,
ch: ch,
}
}

Expand Down Expand Up @@ -49,7 +49,3 @@ func (u *GenreHandler) UpdateGenre() {
u.ch.ErrorWrapper(constants.UPDATE_FAILED, err)
u.ch.SuccessWrapper(constants.UPDATE_SUCCESS, result)
}

func (u *GenreHandler) GetTracksOfGenre() {
u.helper.OutputSuccess(constants.GET_SUCCESS)
}
File renamed without changes.
4 changes: 0 additions & 4 deletions internal/genres/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,3 @@ func (g *GenreUsecase) UpdateGenre() ([]string, error) {
return newItem, nil

}

func (g *GenreUsecase) GetTracksOfGenre() string {
return "Tracks of Genre"
}
39 changes: 0 additions & 39 deletions internal/handlers/track.go

This file was deleted.

10 changes: 0 additions & 10 deletions internal/models/playlist.go

This file was deleted.

23 changes: 12 additions & 11 deletions internal/handlers/playlist.go → internal/playlists/handler.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package handlers
package playlists

import (
"github.com/thuongtruong109/soundlib/pkg/helpers"

"github.com/thuongtruong109/soundlib/internal/usecases"
)

type PlaylistHandler struct {
uc usecases.PlaylistUsecase
uc PlaylistUsecase
helper helpers.Helper
}

func NewPlaylistHandler(uc usecases.PlaylistUsecase, helper helpers.Helper) *PlaylistHandler {
func NewPlaylistHandler(uc PlaylistUsecase, helper helpers.Helper) *PlaylistHandler {
return &PlaylistHandler{
uc: uc,
uc: uc,
helper: helper,
}
}

func (u *PlaylistHandler) GetPlaylists() {
u.helper.OutputSuccess("GetPlaylists")
func (h *PlaylistHandler) GetPlaylists() {
// result, err := h.uc.GetPlaylists()
// h.helper.ErrorWrapper(constants.GET_FAILED, err)
// h.helper.SuccessWrapper(constants.GET_SUCCESS, result)
h.helper.OutputSuccess("GetPlaylist")
}

func (u *PlaylistHandler) GetPlaylist() {
u.helper.OutputSuccess("GetPlaylist")
func (h *PlaylistHandler) GetPlaylist() {
h.helper.OutputSuccess("GetPlaylist")
}

func (u *PlaylistHandler) CreatePlaylist() {
Expand Down Expand Up @@ -52,4 +53,4 @@ func (u *PlaylistHandler) DeleteTrackFromPlaylist() {

func (u *PlaylistHandler) GetPlaylistsHaveTrack() {
u.helper.OutputSuccess("GetPlaylistsHaveTrack")
}
}
Loading

0 comments on commit 6866f54

Please sign in to comment.