Избранное (Лайки)
Relevant source files
The following files were used as context for generating this wiki page:
The "Favorites" (Likes) system is a core feature of the Music Subscription Service that allows users to bookmark specific audio tracks for quick access. This module manages the association between users and tracks, enforcing business rules such as limits based on subscription types.
Within the project architecture, this feature is implemented as a dedicated module located in internal/features/favorites, following a standard layered architecture: Repository, Service, and Transport (HTTP).
Sources: README.md:129-137, cmd/app/main.go:106-108
Database Schema and Data Model
The favorites system relies on a many-to-many relationship between users and tracks. This is implemented via a junction table in PostgreSQL.
Entity Relationship Diagram
The following diagram illustrates the relationship between the favorites table and the primary entities of the system:
The favorites table uses a composite primary key consisting of user_id and track_id to ensure a user cannot like the same track multiple times.
Sources: migrations/001_init.up.sql:71-76
Data Fields
| Field | Type | Constraints | Description |
|---|---|---|---|
user_id | BIGINT | PK, FK (users.id) | Reference to the user who added the track |
track_id | BIGINT | PK, FK (tracks.id) | Reference to the liked track |
created_at | TIMESTAMP | DEFAULT NOW() | Timestamp when the track was added to favorites |
Sources: migrations/001_init.up.sql:71-76
Logic and Architecture
The favorites module is initialized in the main application entry point and integrated into the HTTP routing table with mandatory authentication.
Component Initialization
The service is configured with a specific limit (e.g., 20) for users with a "FREE" subscription type.
Sources: cmd/app/main.go:106-108, cmd/app/main.go:154-155
Business Logic Implementation
The system performs the following operations:
- Add Favorite: Validates if the track is already in the list and checks subscription limits.
- Remove Favorite: Deletes the association from the database.
- List Favorites: Returns a list of tracks liked by the user, joined with artist, album, and genre data.
Sources: internal/features/favorites/repository/postgres/favorites_repository.go:21-72
SQL Queries for Operations
The repository layer uses direct SQL queries for high performance:
-- List User Favorites with metadata
SELECT t.id, t.title, art.name AS artist_name, alb.title AS album_title
FROM tracks t
JOIN favorites f ON t.id = f.track_id
JOIN artists art ON t.artist_id = art.id
LEFT JOIN albums alb ON t.album_id = alb.id
WHERE f.user_id = $1 AND t.is_active = TRUE
ORDER BY f.created_at DESC;
Sources: internal/features/favorites/repository/postgres/favorites_repository.go:56-72
API Endpoints
All favorites endpoints are protected and require a valid JWT Authorization: Bearer <token> header. The user ID is extracted from the JWT claims.
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
GET | /api/v1/favorites/tracks | Retrieve all tracks liked by the current user | Yes |
POST | /api/v1/favorites/tracks/{id} | Add a track to the favorites list | Yes |
DELETE | /api/v1/favorites/tracks/{id} | Remove a track from the favorites list | Yes |
Sources: README.md:78-82, internal/core/middleware/middleware.go:109-138
Sequence: Adding a Favorite
Summary
The Favorites (Likes) module provides a streamlined way for users to manage their preferred tracks. It is tightly integrated with the authentication system and the track catalog. By enforcing limits at the service layer based on the user's subscription_type (found in the JWT claims), it serves as a monetization hook for the platform.
Sources: README.md:52, internal/core/middleware/middleware.go:20-26