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>
This commit is contained in:
Gustavo Henrique Santos Souza de Miranda
2025-12-07 23:03:14 -03:00
parent f31c657d61
commit adb536e135
4 changed files with 126 additions and 43 deletions
+7 -7
View File
@@ -1,5 +1,7 @@
syntax = "proto3";
import "google/protobuf/wrappers.proto"; // ← Adicione esse import!
option java_package = "com.mediamanager.protocol.messages";
option java_outer_classname = "AlbumMessages";
@@ -13,11 +15,9 @@ message Album {
string code = 5;
bool is_compilation = 6;
int32 fk_albumtype_id = 7;
int32 fk_albumart_id = 8;
int64 created_at = 9;
int64 updated_at = 10;
}
@@ -55,12 +55,12 @@ message GetAlbumByIdResponse {
message UpdateAlbumRequest {
int32 id = 1;
string name = 2;
int32 year = 3;
int32 number_of_discs = 4;
google.protobuf.Int32Value year = 3; // ← Mudou!
google.protobuf.Int32Value number_of_discs = 4; // ← Mudou!
string code = 5;
bool is_compilation = 6;
int32 fk_albumtype_id = 7;
int32 fk_albumart_id = 8;
google.protobuf.Int32Value fk_albumtype_id = 7; // ← Mudou!
google.protobuf.Int32Value fk_albumart_id = 8; // ← Mudou!
}
message UpdateAlbumResponse {
@@ -74,4 +74,4 @@ message DeleteAlbumRequest {
message DeleteAlbumResponse {
bool success = 1;
string message = 2;
}
}