Skip to content

Commit

Permalink
refactor: modular codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
thuongtruong109 committed Nov 12, 2024
1 parent 4562eaf commit d0844c0
Show file tree
Hide file tree
Showing 23 changed files with 468 additions and 152 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/analys.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: "CodeQL"

on:
push:
branches:
- main

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: ["go"]
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}

- name: Auto build
uses: github/codeql-action/autobuild@v2

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Soundlib - music management system

![GitHub CI status](https://img.shields.io/github/actions/workflow/status/thuongtruong109/soundlib/ci.yml)
![CodeQL analysis](https://github.com/thuongtruong109/soundlib/actions/workflows/analysis.yml/badge.svg?branch=main)
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/thuongtruong109/soundlib)
![GitHub License](https://img.shields.io/github/license/thuongtruong109/soundlib?color=orange)

## Preview

Expand All @@ -10,7 +12,7 @@

## Description

This is a music management system that allows you to interect and manage data through cli. Program is written in Go and uses JSON to store data. Practicing and handle usecases such as: file I/O, data structure, error handling, etc.
This is a music management system that allows you to interect and manage data through cli. Program is written in Go and uses JSON as store data format. Practicing and handle usecases such as: programing logic, database relation, file I/O, data structure, error handling, etc.

## Features

Expand Down
17 changes: 9 additions & 8 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import (
"github.com/thuongtruong109/soundlib/internal/handlers"
"github.com/thuongtruong109/soundlib/internal/usecases"

"github.com/thuongtruong109/soundlib/pkg/helpers"
"github.com/thuongtruong109/soundlib/internal/albums"
"github.com/thuongtruong109/soundlib/internal/artists"
"github.com/thuongtruong109/soundlib/internal/genres"
"github.com/thuongtruong109/soundlib/internal/common"
"github.com/thuongtruong109/soundlib/internal/genres"

"github.com/thuongtruong109/soundlib/pkg/helpers"
)

func App() {
helper := helpers.NewHelper()

albumUC := usecases.NewAlbumUsecase()
albumHandler := handlers.NewAlbumHandler(*albumUC, *helper)
albumUC := albums.NewAlbumUsecase()
albumHandler := albums.NewAlbumHandler(*albumUC, *helper)

artistRepo := artists.NewArtistRepository(*helper)
artistRepo := artists.NewArtistRepository()
artistUC := artists.NewArtistUsecase(*artistRepo, *helper)
artistHandler := artists.NewArtistHandler(*artistUC, *helper, *common.NewCommonHandler(*helper, "Artists"))

Expand All @@ -30,8 +32,7 @@ func App() {
trackUC := usecases.NewTrackUsecase()
trackHandler := handlers.NewTrackHandler(*trackUC, *helper)


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

exe.Execution()
}
}
27 changes: 14 additions & 13 deletions cmd/delivery.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package cmd

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

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

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

func NewDelivery(albumHandler handlers.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 handlers.PlaylistHandler, trackHandler handlers.TrackHandler, helper helpers.Helper) *Delivery {
return &Delivery{
albumHandler: albumHandler,
artistHandler: artistHandler,
genreHandler: genreHandler,
albumHandler: albumHandler,
artistHandler: artistHandler,
genreHandler: genreHandler,
playlistHandler: playlistHandler,
trackHandler: trackHandler,
helper: helper,
trackHandler: trackHandler,
helper: helper,
}
}

Expand Down
10 changes: 4 additions & 6 deletions internal/handlers/album.go → internal/albums/handler.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package handlers
package albums

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

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

type AlbumHandler struct {
uc usecases.AlbumUsecase
uc AlbumUsecase
helper helpers.Helper
}

func NewAlbumHandler(uc usecases.AlbumUsecase, helper helpers.Helper) *AlbumHandler {
func NewAlbumHandler(uc AlbumUsecase, helper helpers.Helper) *AlbumHandler {
return &AlbumHandler{
uc: uc,
uc: uc,
helper: helper,
}
}
Expand Down
10 changes: 10 additions & 0 deletions internal/albums/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package albums

type Album struct {
ID string `json:"id"`
Title string `json:"title"`
Duration float32 `json:"duration"`
Year int `json:"year"`

ArtistID int `json:"artist_id"`
}
6 changes: 3 additions & 3 deletions internal/usecases/album.go → internal/albums/usecase.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package usecases
package albums

type AlbumUsecase struct {}
type AlbumUsecase struct{}

func NewAlbumUsecase() *AlbumUsecase {
return &AlbumUsecase{}
Expand Down Expand Up @@ -28,4 +28,4 @@ func (a *AlbumUsecase) UpdateAlbum() string {

func (a *AlbumUsecase) GetTracksOfAlbum() string {
return "Tracks of Album"
}
}
12 changes: 6 additions & 6 deletions internal/artists/handler.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package artists

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 ArtistHandler struct {
uc ArtistUsecase
uc ArtistUsecase
helper helpers.Helper
ch common.CommonHandler
ch common.CommonHandler
}

func NewArtistHandler(uc ArtistUsecase, helper helpers.Helper, ch common.CommonHandler) *ArtistHandler {
return &ArtistHandler{
uc: uc,
uc: uc,
helper: helper,
ch: ch,
ch: ch,
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/models/artist.go → internal/artists/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package models
package artists

type Artist struct {
ID string `json:"id"`
ID string `json:"id"`
Name string `json:"name"`
}
}
47 changes: 21 additions & 26 deletions internal/artists/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@ package artists

import (
"fmt"

"github.com/thuongtruong109/gouse/io"
"github.com/thuongtruong109/soundlib/pkg/constants"
"github.com/thuongtruong109/soundlib/internal/models"
"github.com/thuongtruong109/soundlib/pkg/helpers"
)

type ArtistRepository struct {
helper helpers.Helper
}
type ArtistRepository struct{}

func NewArtistRepository(helper helpers.Helper) *ArtistRepository {
return &ArtistRepository{
helper: helper,
}
func NewArtistRepository() *ArtistRepository {
return &ArtistRepository{}
}

func (a *ArtistRepository) GetArtists() ([]*models.Artist, error) {
allArtist, err := io.ReadFileObj[*models.Artist](constants.ARTIST_PATH)
func (ar *ArtistRepository) GetArtists() ([]*Artist, error) {
allArtist, err := io.ReadFileObj[*Artist](constants.ARTIST_PATH)
if err != nil {
return nil, err
}
Expand All @@ -31,8 +26,8 @@ func (a *ArtistRepository) GetArtists() ([]*models.Artist, error) {
return allArtist, nil
}

func (a *ArtistRepository) GetArtist(artistID string) (*models.Artist, error) {
allArtist, err := a.GetArtists()
func (ar *ArtistRepository) GetArtist(artistID string) (*Artist, error) {
allArtist, err := ar.GetArtists()
if err != nil {
return nil, err
}
Expand All @@ -50,35 +45,35 @@ func (a *ArtistRepository) GetArtist(artistID string) (*models.Artist, error) {
return nil, nil
}

func (a *ArtistRepository) CreateArtist(newArtist *models.Artist) (*models.Artist, error) {
allGenres, _ := a.GetArtists()
func (ar *ArtistRepository) CreateArtist(newArtist *Artist) (*Artist, error) {
allGenres, _ := ar.GetArtists()

var artistInit []*models.Artist
var artistInit []*Artist

if allGenres == nil {
artistInit = make([]*models.Artist, 0)
artistInit = make([]*Artist, 0)
} else {
artistInit = make([]*models.Artist, len(allGenres))
artistInit = make([]*Artist, len(allGenres))
copy(artistInit, allGenres)
}

artistInit = append(artistInit, newArtist)

err2 := io.WriteFileObj[[]*models.Artist](constants.ARTIST_PATH, artistInit)
err2 := io.WriteFileObj[[]*Artist](constants.ARTIST_PATH, artistInit)
if err2 != nil {
return nil, fmt.Errorf(constants.CREATE_FAILED)
}
return newArtist, nil
}

func (a *ArtistRepository) UpdateArtist(artlistUpdate *models.Artist) (*models.Artist, error) {
allArtist, _ := a.GetArtists()
func (ar *ArtistRepository) UpdateArtist(artlistUpdate *Artist) (*Artist, error) {
allArtist, _ := ar.GetArtists()

if allArtist == nil {
return nil, fmt.Errorf(constants.UPDATE_FAILED)
}

var artistInit []*models.Artist
var artistInit []*Artist

for i, v := range allArtist {
if v.ID == artlistUpdate.ID {
Expand All @@ -89,15 +84,15 @@ func (a *ArtistRepository) UpdateArtist(artlistUpdate *models.Artist) (*models.A

artistInit = append(artistInit, allArtist[len(artistInit):]...)

err2 := io.WriteFileObj[[]*models.Artist](constants.ARTIST_PATH, allArtist)
err2 := io.WriteFileObj[[]*Artist](constants.ARTIST_PATH, allArtist)
if err2 != nil {
return nil, fmt.Errorf(constants.UPDATE_FAILED)
}
return artlistUpdate, nil
}

func (a *ArtistRepository) DeleteArtist(artlistID string) error {
allArtist, _ := a.GetArtists()
func (ar *ArtistRepository) DeleteArtist(artlistID string) error {
allArtist, _ := ar.GetArtists()

if allArtist == nil {
return fmt.Errorf(constants.DELETE_FAILED)
Expand All @@ -110,7 +105,7 @@ func (a *ArtistRepository) DeleteArtist(artlistID string) error {
}
}

err2 := io.WriteFileObj[[]*models.Artist](constants.ARTIST_PATH, allArtist)
err2 := io.WriteFileObj[[]*Artist](constants.ARTIST_PATH, allArtist)
if err2 != nil {
return fmt.Errorf(constants.DELETE_FAILED)
}
Expand Down
Loading

0 comments on commit d0844c0

Please sign in to comment.