Documentation
¶
Index ¶
- type Repository
- func (r *Repository) AddSong(ctx context.Context, song models.Song) (uuid.UUID, error)
- func (r *Repository) DeleteSong(ctx context.Context, id uuid.UUID) error
- func (r *Repository) GetLibrary(ctx context.Context, filters models.Filters) (models.Library, error)
- func (r *Repository) GetLyrics(ctx context.Context, id uuid.UUID) (string, error)
- func (r *Repository) UpdateSong(ctx context.Context, song models.Song) error
- type Settings
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
Repository implements the MusicRepository interface. It represents a PostgreSQL database connection pool and provides methods for interacting with the database. Must be created with New function.
func New ¶
func New(ctx context.Context, settings Settings) (*Repository, error)
New returns a new Repository instance based on the provided settings. It configures the PostgreSQL connection based on the connection string, sets the retry count and backoff time, and establishes a connection to the database. If the connection string is invalid or the database connection fails, it returns an error.
func (*Repository) AddSong ¶
AddSong adds a song to the library.
The function logs debug messages before and after adding, and error messages if an error occurs during the process.
Parameters:
- ctx: The context for managing cancellation.
- song: The models.Song to be added.
Returns:
- The ID of the added song if successful, otherwise nil.
- An error if the operation fails, otherwise nil.
func (*Repository) DeleteSong ¶
DeleteSong deletes a song with the given ID by marking it as deleted in the database.
It logs debug messages before and after the deletion, and error messages if an error occurs during the process.
Parameters:
- ctx: The context for managing cancellation.
- id: The UUID of the song to be deleted.
Returns:
- An error if the operation fails, otherwise nil.
func (*Repository) GetLibrary ¶
func (r *Repository) GetLibrary( ctx context.Context, filters models.Filters, ) (models.Library, error)
GetLibrary retrieves a filtered list of songs from the database.
It logs debug messages before and after retrieving the library, and error messages if an error occurs.
Parameters:
- ctx: The context for managing cancellation.
- filters: The filters to apply.
Returns:
- The filtered library with pagination data if successful.
- An error if the operation fails or if no songs are found.
func (*Repository) GetLyrics ¶
GetSongLyrics retrieves the lyrics of a song with the given ID from the database.
It logs debug messages before and after retrieving the lyrics, and error messages if an error occurs.
Parameters:
- ctx: The context for managing cancellation.
- id: The UUID of the song whose lyrics are to be retrieved.
Returns:
- The lyrics as a string if successful.
- An error if the operation fails or if no lyrics are found.
func (*Repository) UpdateSong ¶
Important: The approach chosen was not the most optimal one in order to demonstrate the ability to work with transactions.
UpdateSong updates the details of an existing song in the database. It updates only the fields provided in the song parameter that are not empty. The function starts a transaction for updating multiple fields and ensures atomicity by rolling back if any update fails.
Parameters:
- ctx: The context for managing cancellation.
- song: The models.Song containing updated song details. Only non-empty fields will be updated in the database.
Returns:
- An error if the operation fails, or nil if the update is successful.
type Settings ¶
type Settings struct { // DatabaseDSN is the connection string for the PostgreSQL database. DatabaseDSN string // RetryCount is the number of times to retry the database connection. RetryCount int // RetryBackoff is the duration to wait before retrying the database connection. RetryBackoff time.Duration }