Commit Graph

2 Commits

Author SHA1 Message Date
Gustavo Henrique Santos Souza de Miranda adb536e135 Enhance Album entity with bidirectional relationships and partial update support
This commit introduces significant improvements to the Album entity and its
  update functionality, adding bidirectional relationship management with
  AlbumHasArtist and implementing proper partial update semantics using
  protobuf wrapper types.

  Key enhancements:

  Model Layer Improvements:
  - Added OneToMany relationship from Album to AlbumHasArtist
    * Bidirectional mapping with cascade ALL and orphan removal
    * Maintains collection of album-artist associations
  - Implemented helper methods for artist management:
    * addArtist(): creates and adds AlbumHasArtist association
    * removeArtist(): removes association by artist ID
    * getArtists(): convenience method to extract Artist list from associations
  - Added getters/setters for albumArtists collection
  - Improved entity relationship management

  Service Layer Enhancements:
  - Refactored updateAlbum() to support partial updates
  - Added boolean flags to control field updates:
    * updateYear: controls whether year field should be modified
    * updateNumberOfDiscs: controls disc count updates
    * updateAlbumType: controls AlbumType relationship updates
    * updateAlbumArt: controls AlbumArt relationship updates
  - Only updates fields when explicitly provided by client
  - Maintains existing values for non-provided fields
  - Allows explicit null/removal of optional relationships
  - Improved update logic clarity and maintainability

  Handler Layer Improvements (UpdateAlbumHandler):
  - Integrated protobuf wrapper type handling
  - Uses hasYear(), hasNumberOfDiscs(), etc. to detect field presence
  - Extracts values from Int32Value wrappers
  - Passes presence flags to service layer
  - Properly distinguishes between "not provided" and "null/0"
  - Improved code formatting and readability

  Protocol Buffer Enhancements:
  - Added import for google.protobuf.wrappers
  - Changed UpdateAlbumRequest optional fields to wrapper types:
    * year: int32 → google.protobuf.Int32Value
    * number_of_discs: int32 → google.protobuf.Int32Value
    * fk_albumtype_id: int32 → google.protobuf.Int32Value
    * fk_albumart_id: int32 → google.protobuf.Int32Value
  - Enables proper optional field semantics
  - Allows clients to omit fields from update requests
  - Supports explicit null to remove relationships

  Benefits of these changes:
  - Proper partial update support: clients can update only specific fields
  - Prevents unintended field overwrites with default values
  - Bidirectional navigation from Album to Artists
  - Cascade operations for album-artist associations
  - Cleaner API semantics with explicit field presence detection
  - Better alignment with REST PATCH semantics
  - Maintains backward compatibility for required fields

  Example usage:
  - Update only name: provide name, omit other fields
  - Remove AlbumType: provide fk_albumtype_id with value 0 or null
  - Keep existing year: omit year field from request
  - Update multiple fields: provide only the fields to change

  🤖 Generated with [Claude Code](https://claude.com/claude-code)

  Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-07 23:03:14 -03:00
Gustavo Henrique Santos Souza de Miranda 21f5b93f48 Implement Album management with full CRUD operations
This commit introduces comprehensive Album entity management following
the established repository pattern used for other entities like AlbumType
and SamplingRate. The Album entity includes relationships with AlbumType
and AlbumArt entities.

Key components implemented:

Model Layer:
- Album entity with complete field set:
  * Basic fields: id, name, year, numberOfDiscs, code, isCompilation
  * Foreign key relationships: AlbumType and AlbumArt (ManyToOne)
  * Audit timestamps: createdAt, updatedAt with JPA lifecycle hooks
- JPA annotations with @PrePersist and @PreUpdate for automatic timestamps
- Complete getters and setters

Repository Layer:
- AlbumRepository with EntityManager-based operations
- Full transaction management with proper rollback handling
- Methods: save, findAll, findById, update, deleteById

Service Layer:
- AlbumService with business logic and validation
- Constructor injection of AlbumRepository, AlbumTypeRepository, AlbumArtRepository
- Relationship handling: validates and sets AlbumType and AlbumArt entities
- Input validation for null/empty name field
- ID validation for all operations requiring entity lookup
- Comprehensive logging using Log4j2

Mapper Layer:
- AlbumMapper for bidirectional entity/protobuf conversion
- Timestamp conversion between LocalDateTime and epoch milliseconds
- Foreign key mapping for AlbumType and AlbumArt relationships
- Null safety checks and validation
- Proper handling of optional fields

Action Handlers:
- CreateAlbumHandler (album.create)
- GetAlbumHandler (album.getAll)
- GetAlbumByIdHandler (album.getById)
- UpdateAlbumHandler (album.update)
- DeleteAlbumHandler (album.delete)
- HTTP status code handling: 200 (success), 400 (validation),
  404 (not found), 500 (server error)
- Handles optional fields with proper default value checks

Protocol Buffers:
- Fixed proto definition with correct Album fields
- CreateAlbumRequest and UpdateAlbumRequest with all entity fields
- All CRUD message definitions (Create, Get, GetById, Update, Delete)
- Support for foreign keys and timestamps

Service Registration:
- AlbumRepository initialized with EntityManagerFactory
- AlbumService registered with ServiceLocator with required dependencies
- Ensures all Album action handlers can resolve dependencies

The implementation follows best practices with proper error handling,
logging, validation, relationship management, and consistency with
existing codebase patterns.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-07 20:07:34 -03:00