diff --git a/src/main/java/com/mediamanager/mapper/AlbumMapper.java b/src/main/java/com/mediamanager/mapper/AlbumMapper.java new file mode 100644 index 0000000..57ff6bd --- /dev/null +++ b/src/main/java/com/mediamanager/mapper/AlbumMapper.java @@ -0,0 +1,110 @@ +package com.mediamanager.mapper; + +import com.mediamanager.model.Album; +import com.mediamanager.protocol.messages.AlbumMessages; + +import java.time.Instant; +import java.time.ZoneId; + +public class AlbumMapper { + public static AlbumMessages.Album toProtobuf(Album entity) { + if (entity == null) { + return null; + } + + String name = entity.getName(); + if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("Name cannot be null or empty"); + } + + AlbumMessages.Album.Builder builder = AlbumMessages.Album.newBuilder() + .setName(name); + + Integer id = entity.getId(); + if (id != null) { + builder.setId(id); + } + + Integer year = entity.getYear(); + if (year != null) { + builder.setYear(year); + } + + Integer numberOfDiscs = entity.getNumberOfDiscs(); + if (numberOfDiscs != null) { + builder.setNumberOfDiscs(numberOfDiscs); + } + + String code = entity.getCode(); + if (code != null) { + builder.setCode(code); + } + + Boolean isCompilation = entity.getIsCompilation(); + if (isCompilation != null) { + builder.setIsCompilation(isCompilation); + } + + // Map AlbumType foreign key + if (entity.getAlbumType() != null && entity.getAlbumType().getId() != null) { + builder.setFkAlbumtypeId(entity.getAlbumType().getId()); + } + + // Map AlbumArt foreign key + if (entity.getAlbumArt() != null && entity.getAlbumArt().getId() != null) { + builder.setFkAlbumartId(entity.getAlbumArt().getId()); + } + + // Map timestamps + if (entity.getCreatedAt() != null) { + long createdAtEpoch = entity.getCreatedAt() + .atZone(ZoneId.systemDefault()) + .toInstant() + .toEpochMilli(); + builder.setCreatedAt(createdAtEpoch); + } + + if (entity.getUpdatedAt() != null) { + long updatedAtEpoch = entity.getUpdatedAt() + .atZone(ZoneId.systemDefault()) + .toInstant() + .toEpochMilli(); + builder.setUpdatedAt(updatedAtEpoch); + } + + return builder.build(); + } + + public static Album toEntity(AlbumMessages.Album protobuf) { + if (protobuf == null) { + return null; + } + + Album entity = new Album(); + + if (protobuf.getId() > 0) { + entity.setId(protobuf.getId()); + } + + entity.setName(protobuf.getName()); + + if (protobuf.getYear() > 0) { + entity.setYear(protobuf.getYear()); + } + + if (protobuf.getNumberOfDiscs() > 0) { + entity.setNumberOfDiscs(protobuf.getNumberOfDiscs()); + } + + if (!protobuf.getCode().isEmpty()) { + entity.setCode(protobuf.getCode()); + } + + entity.setIsCompilation(protobuf.getIsCompilation()); + + // Note: Foreign key relationships (AlbumType, AlbumArt) are handled in the service layer + // Timestamps are managed by JPA @PrePersist and @PreUpdate + + return entity; + } +} diff --git a/src/main/java/com/mediamanager/model/Album.java b/src/main/java/com/mediamanager/model/Album.java new file mode 100644 index 0000000..6a740fc --- /dev/null +++ b/src/main/java/com/mediamanager/model/Album.java @@ -0,0 +1,145 @@ +package com.mediamanager.model; + +import jakarta.persistence.*; +import java.time.LocalDateTime; + +@Entity +@Table(name = "album") +public class Album { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @Column(nullable = false) + private String name; + + @Column + private Integer year; + + @Column(name = "number_of_discs") + private Integer numberOfDiscs; + + @Column + private String code; + + @Column(name = "is_compilation") + private Boolean isCompilation; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "fk_albumtype_id") + private AlbumType albumType; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "fk_albumart_id") + private AlbumArt albumArt; + + @Column(name = "created_at", nullable = false, updatable = false) + private LocalDateTime createdAt; + + @Column(name = "updated_at") + private LocalDateTime updatedAt; + + @PrePersist + protected void onCreate() { + this.createdAt = LocalDateTime.now(); + this.updatedAt = LocalDateTime.now(); + } + + @PreUpdate + protected void onUpdate() { + this.updatedAt = LocalDateTime.now(); + } + + public Album() {} + + // Getters and Setters + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getYear() { + return year; + } + + public void setYear(Integer year) { + this.year = year; + } + + public Integer getNumberOfDiscs() { + return numberOfDiscs; + } + + public void setNumberOfDiscs(Integer numberOfDiscs) { + this.numberOfDiscs = numberOfDiscs; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public Boolean getIsCompilation() { + return isCompilation; + } + + public void setIsCompilation(Boolean isCompilation) { + this.isCompilation = isCompilation; + } + + public AlbumType getAlbumType() { + return albumType; + } + + public void setAlbumType(AlbumType albumType) { + this.albumType = albumType; + } + + public AlbumArt getAlbumArt() { + return albumArt; + } + + public void setAlbumArt(AlbumArt albumArt) { + this.albumArt = albumArt; + } + + public LocalDateTime getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(LocalDateTime createdAt) { + this.createdAt = createdAt; + } + + public LocalDateTime getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(LocalDateTime updatedAt) { + this.updatedAt = updatedAt; + } + + @Override + public String toString() { + return "Album{" + + "id=" + id + + ", name='" + name + '\'' + + ", year=" + year + + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/com/mediamanager/repository/AlbumRepository.java b/src/main/java/com/mediamanager/repository/AlbumRepository.java new file mode 100644 index 0000000..f744222 --- /dev/null +++ b/src/main/java/com/mediamanager/repository/AlbumRepository.java @@ -0,0 +1,103 @@ +package com.mediamanager.repository; + + +import com.mediamanager.model.Album; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + + +import java.util.List; +import java.util.Optional; + +public class AlbumRepository { + private static final Logger logger = LogManager.getLogger(AlbumRepository.class); + + private final EntityManagerFactory entityManagerFactory; + + public AlbumRepository(EntityManagerFactory entityManagerFactory) { + this.entityManagerFactory = entityManagerFactory; + } + + public Album save(Album album) { + logger.debug("Saving Album: {}", album.getName()); + EntityManager em = entityManagerFactory.createEntityManager(); + em.getTransaction().begin(); + try { + em.persist(album); + em.getTransaction().commit(); + logger.debug("Album has been saved successfully"); + return album; + } catch (Exception e) { + em.getTransaction().rollback(); + logger.error("Error while saving Album: {}", e.getMessage()); + throw e; + } finally { + if (em.isOpen()) em.close(); + } + } + + public List findAll() { + logger.debug("Finding All Album"); + EntityManager em = entityManagerFactory.createEntityManager(); + try{ + return em.createQuery("select a from Album a", Album.class).getResultList(); + }finally { + if (em.isOpen()) em.close(); + } + } + + public Optional findById(Integer id) { + logger.debug("Finding Album with id: {}", id); + EntityManager em = entityManagerFactory.createEntityManager(); + try{ + Album album = em.find(Album.class, id); + return Optional.ofNullable(album); + }finally { + if (em.isOpen()) em.close(); + } + } + + public Album update(Album album) { + logger.debug("Updating Album: {}", album.getName()); + EntityManager em = entityManagerFactory.createEntityManager(); + em.getTransaction().begin(); + try { + Album updated = em.merge(album); + em.getTransaction().commit(); + logger.debug("Album has been updated successfully"); + return updated; + } catch (Exception e) { + em.getTransaction().rollback(); + logger.error("Error while updating Album: {}", e.getMessage()); + throw e; + } finally { + if (em.isOpen()) em.close(); + } + } + + public boolean deleteById(Integer id){ + logger.debug("Deleting Album with id: {}", id); + EntityManager em = entityManagerFactory.createEntityManager(); + em.getTransaction().begin(); + try{ + Album album = em.find(Album.class, id); + if (album == null) { + em.getTransaction().rollback(); + return false; + } + em.remove(album); + em.getTransaction().commit(); + logger.debug("Album has been deleted successfully"); + return true; + } catch (Exception e) { + em.getTransaction().rollback(); + logger.error("Error while deleting Album: {}", e.getMessage()); + throw e; + } finally { + if (em.isOpen()) em.close(); + } + } + +} diff --git a/src/main/java/com/mediamanager/service/album/AlbumService.java b/src/main/java/com/mediamanager/service/album/AlbumService.java new file mode 100644 index 0000000..82d1664 --- /dev/null +++ b/src/main/java/com/mediamanager/service/album/AlbumService.java @@ -0,0 +1,130 @@ +package com.mediamanager.service.album; + +import com.mediamanager.model.Album; +import com.mediamanager.model.AlbumArt; +import com.mediamanager.model.AlbumType; +import com.mediamanager.repository.AlbumRepository; +import com.mediamanager.repository.AlbumArtRepository; +import com.mediamanager.repository.AlbumTypeRepository; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.List; +import java.util.Optional; + +public class AlbumService { + private static final Logger logger = LogManager.getLogger(AlbumService.class); + private final AlbumRepository repository; + private final AlbumTypeRepository albumTypeRepository; + private final AlbumArtRepository albumArtRepository; + + public AlbumService(AlbumRepository repository, AlbumTypeRepository albumTypeRepository, AlbumArtRepository albumArtRepository) { + this.repository = repository; + this.albumTypeRepository = albumTypeRepository; + this.albumArtRepository = albumArtRepository; + } + + public Album createAlbum(String name, Integer year, Integer numberOfDiscs, String code, Boolean isCompilation, Integer albumTypeId, Integer albumArtId) { + logger.debug("Creating album:{}", name); + if (name == null || name.trim().isEmpty()) { + throw new IllegalArgumentException("Album name cannot be null or empty"); + } + + Album album = new Album(); + album.setName(name); + album.setYear(year); + album.setNumberOfDiscs(numberOfDiscs); + album.setCode(code); + album.setIsCompilation(isCompilation); + + // Set AlbumType if provided + if (albumTypeId != null && albumTypeId > 0) { + Optional albumType = albumTypeRepository.findById(albumTypeId); + if (albumType.isEmpty()) { + throw new IllegalArgumentException("AlbumType not found with id: " + albumTypeId); + } + album.setAlbumType(albumType.get()); + } + + // Set AlbumArt if provided + if (albumArtId != null && albumArtId > 0) { + Optional albumArt = albumArtRepository.findById(albumArtId); + if (albumArt.isEmpty()) { + throw new IllegalArgumentException("AlbumArt not found with id: " + albumArtId); + } + album.setAlbumArt(albumArt.get()); + } + + return repository.save(album); + } + + public List getAllAlbums() { + logger.info("Getting all albums"); + return repository.findAll(); + } + + public Optional getAlbumById(Integer id) { + if (id == null) { + throw new IllegalArgumentException("ID cannot be null"); + } + logger.info("Getting album by id:{}", id); + return repository.findById(id); + } + + public Optional updateAlbum(Integer id, String name, Integer year, Integer numberOfDiscs, String code, Boolean isCompilation, Integer albumTypeId, Integer albumArtId) { + if (id == null) { + throw new IllegalArgumentException("ID cannot be null"); + } + logger.info("Updating album:{}", name); + if (name == null || name.trim().isEmpty()) { + throw new IllegalArgumentException("Album name cannot be null or empty"); + } + + Optional existingAlbum = repository.findById(id); + if(existingAlbum.isEmpty()) { + logger.warn("Album not found with id:{}", id); + return Optional.empty(); + } + + Album album = existingAlbum.get(); + album.setName(name); + album.setYear(year); + album.setNumberOfDiscs(numberOfDiscs); + album.setCode(code); + album.setIsCompilation(isCompilation); + + // Update AlbumType if provided + if (albumTypeId != null && albumTypeId > 0) { + Optional albumType = albumTypeRepository.findById(albumTypeId); + if (albumType.isEmpty()) { + throw new IllegalArgumentException("AlbumType not found with id: " + albumTypeId); + } + album.setAlbumType(albumType.get()); + } else { + album.setAlbumType(null); + } + + // Update AlbumArt if provided + if (albumArtId != null && albumArtId > 0) { + Optional albumArt = albumArtRepository.findById(albumArtId); + if (albumArt.isEmpty()) { + throw new IllegalArgumentException("AlbumArt not found with id: " + albumArtId); + } + album.setAlbumArt(albumArt.get()); + } else { + album.setAlbumArt(null); + } + + Album updatedAlbum = repository.update(album); + return Optional.of(updatedAlbum); + } + + public boolean deleteAlbum(Integer id) { + if (id == null) { + throw new IllegalArgumentException("Album id cannot be null"); + } + logger.info("Deleting album:{}", id); + return repository.deleteById(id); + } + +} diff --git a/src/main/java/com/mediamanager/service/delegate/DelegateActionManager.java b/src/main/java/com/mediamanager/service/delegate/DelegateActionManager.java index a001eaa..c8a7251 100644 --- a/src/main/java/com/mediamanager/service/delegate/DelegateActionManager.java +++ b/src/main/java/com/mediamanager/service/delegate/DelegateActionManager.java @@ -3,6 +3,7 @@ package com.mediamanager.service.delegate; import com.google.protobuf.ByteString; import com.mediamanager.protocol.TransportProtocol; import com.mediamanager.repository.*; +import com.mediamanager.service.album.AlbumService; import com.mediamanager.service.albumart.AlbumArtService; import com.mediamanager.service.albumtype.AlbumTypeService; import com.mediamanager.service.bitdepth.BitDepthService; @@ -85,6 +86,10 @@ public class DelegateActionManager { AlbumTypeService albumTypeService = new AlbumTypeService(albumTypeRepository); serviceLocator.register(AlbumTypeService.class, albumTypeService); + AlbumRepository albumRepository = new AlbumRepository(entityManagerFactory); + AlbumService albumService = new AlbumService(albumRepository, albumTypeRepository, albumArtRepository); + serviceLocator.register(AlbumService.class, albumService); + serviceLocator.logRegisteredServices(); logger.info("Services initialized successfully"); diff --git a/src/main/java/com/mediamanager/service/delegate/handler/album/CreateAlbumHandler.java b/src/main/java/com/mediamanager/service/delegate/handler/album/CreateAlbumHandler.java new file mode 100644 index 0000000..a7c3d75 --- /dev/null +++ b/src/main/java/com/mediamanager/service/delegate/handler/album/CreateAlbumHandler.java @@ -0,0 +1,59 @@ +package com.mediamanager.service.delegate.handler.album; + +import com.google.protobuf.ByteString; +import com.google.protobuf.InvalidProtocolBufferException; +import com.mediamanager.mapper.AlbumMapper; +import com.mediamanager.model.Album; +import com.mediamanager.protocol.TransportProtocol; +import com.mediamanager.protocol.messages.AlbumMessages; +import com.mediamanager.service.delegate.ActionHandler; +import com.mediamanager.service.delegate.annotation.Action; +import com.mediamanager.service.album.AlbumService; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +@Action("album.create") +public class CreateAlbumHandler implements ActionHandler { + private static final Logger logger = LogManager.getLogger(CreateAlbumHandler.class); + private final AlbumService albumService; + + public CreateAlbumHandler(AlbumService albumService) { + this.albumService = albumService; + } + + @Override + public TransportProtocol.Response.Builder handle(ByteString requestPayload) throws InvalidProtocolBufferException { + try{ + AlbumMessages.CreateAlbumRequest createRequest = + AlbumMessages.CreateAlbumRequest.parseFrom(requestPayload); + + Album album = albumService.createAlbum( + createRequest.getName(), + createRequest.getYear() > 0 ? createRequest.getYear() : null, + createRequest.getNumberOfDiscs() > 0 ? createRequest.getNumberOfDiscs() : null, + createRequest.getCode().isEmpty() ? null : createRequest.getCode(), + createRequest.getIsCompilation(), + createRequest.getFkAlbumtypeId() > 0 ? createRequest.getFkAlbumtypeId() : null, + createRequest.getFkAlbumartId() > 0 ? createRequest.getFkAlbumartId() : null + ); + + AlbumMessages.Album albumProto = AlbumMapper.toProtobuf(album); + AlbumMessages.CreateAlbumResponse createAlbumResponse = AlbumMessages.CreateAlbumResponse.newBuilder() + .setAlbum(albumProto) + .build(); + return TransportProtocol.Response.newBuilder() + .setPayload(createAlbumResponse.toByteString()); + } catch (IllegalArgumentException e) { + logger.error("Validation error", e); + return TransportProtocol.Response.newBuilder() + .setStatusCode(400) + .setPayload(ByteString.copyFromUtf8("Validation error: " + e.getMessage())); + + } catch (Exception e) { + logger.error("Error creating album", e); + return TransportProtocol.Response.newBuilder() + .setStatusCode(500) + .setPayload(ByteString.copyFromUtf8("Error: " + e.getMessage())); + } + } +} diff --git a/src/main/java/com/mediamanager/service/delegate/handler/album/DeleteAlbumHandler.java b/src/main/java/com/mediamanager/service/delegate/handler/album/DeleteAlbumHandler.java new file mode 100644 index 0000000..101325c --- /dev/null +++ b/src/main/java/com/mediamanager/service/delegate/handler/album/DeleteAlbumHandler.java @@ -0,0 +1,62 @@ +package com.mediamanager.service.delegate.handler.album; + +import com.google.protobuf.ByteString; +import com.google.protobuf.InvalidProtocolBufferException; +import com.mediamanager.protocol.TransportProtocol; +import com.mediamanager.protocol.messages.AlbumMessages; +import com.mediamanager.service.delegate.ActionHandler; +import com.mediamanager.service.delegate.annotation.Action; +import com.mediamanager.service.album.AlbumService; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +@Action("album.delete") +public class DeleteAlbumHandler implements ActionHandler { + private static final Logger logger = LogManager.getLogger(DeleteAlbumHandler.class); + + private final AlbumService albumService; + + public DeleteAlbumHandler(AlbumService albumService) { + this.albumService = albumService; + } + + @Override + public TransportProtocol.Response.Builder handle(ByteString requestPayload) + throws InvalidProtocolBufferException { + + try { + AlbumMessages.DeleteAlbumRequest deleteRequest = + AlbumMessages.DeleteAlbumRequest.parseFrom(requestPayload); + int id = deleteRequest.getId(); + boolean success = albumService.deleteAlbum(id); + AlbumMessages.DeleteAlbumResponse deleteResponse; + if (success) { + deleteResponse = AlbumMessages.DeleteAlbumResponse.newBuilder() + .setSuccess(true) + .setMessage("Album deleted successfully") + .build(); + return TransportProtocol.Response.newBuilder() + .setPayload(deleteResponse.toByteString()); + } else { + deleteResponse = AlbumMessages.DeleteAlbumResponse.newBuilder() + .setSuccess(false) + .setMessage("Album not found") + .build(); + + return TransportProtocol.Response.newBuilder() + .setStatusCode(404) + .setPayload(deleteResponse.toByteString()); + } + } catch (Exception e) { + logger.error("Error deleting album", e); + AlbumMessages.DeleteAlbumResponse deleteResponse = + AlbumMessages.DeleteAlbumResponse.newBuilder() + .setSuccess(false) + .setMessage("Error: " + e.getMessage()) + .build(); + return TransportProtocol.Response.newBuilder() + .setStatusCode(500) + .setPayload(deleteResponse.toByteString()); + } + } + } diff --git a/src/main/java/com/mediamanager/service/delegate/handler/album/GetAlbumByIdHandler.java b/src/main/java/com/mediamanager/service/delegate/handler/album/GetAlbumByIdHandler.java new file mode 100644 index 0000000..97d2814 --- /dev/null +++ b/src/main/java/com/mediamanager/service/delegate/handler/album/GetAlbumByIdHandler.java @@ -0,0 +1,56 @@ +package com.mediamanager.service.delegate.handler.album; + +import com.google.protobuf.ByteString; +import com.google.protobuf.InvalidProtocolBufferException; +import com.mediamanager.mapper.AlbumMapper; +import com.mediamanager.model.Album; +import com.mediamanager.protocol.TransportProtocol; +import com.mediamanager.protocol.messages.AlbumMessages; +import com.mediamanager.service.delegate.ActionHandler; +import com.mediamanager.service.delegate.annotation.Action; +import com.mediamanager.service.album.AlbumService; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.Optional; + +@Action(value = "album.getById") +public class GetAlbumByIdHandler implements ActionHandler { + private static final Logger logger = LogManager.getLogger(GetAlbumByIdHandler.class); + private final AlbumService albumService; + + public GetAlbumByIdHandler(AlbumService albumService) { + this.albumService = albumService; + } + + @Override + public TransportProtocol.Response.Builder handle(ByteString requestPayload) + throws InvalidProtocolBufferException{ + + try{ + AlbumMessages.GetAlbumByIdRequest getByIdRequest = + AlbumMessages.GetAlbumByIdRequest.parseFrom(requestPayload); + int id = getByIdRequest.getId(); + + Optional albumOpt = albumService.getAlbumById(id); + + if (albumOpt.isEmpty()){ + logger.warn("Album not found with ID: {}", id); + return TransportProtocol.Response.newBuilder() + .setStatusCode(404) + .setPayload(ByteString.copyFromUtf8("Album not found")); + } + AlbumMessages.Album albumProto = AlbumMapper.toProtobuf(albumOpt.get()); + AlbumMessages.GetAlbumByIdResponse getByIdResponse = AlbumMessages.GetAlbumByIdResponse.newBuilder() + .setAlbum(albumProto) + .build(); + return TransportProtocol.Response.newBuilder() + .setPayload(getByIdResponse.toByteString()); + } catch (Exception e) { + logger.error("Error getting album by ID", e); + return TransportProtocol.Response.newBuilder() + .setStatusCode(500) + .setPayload(ByteString.copyFromUtf8("Error: "+ e.getMessage())); + } + } +} diff --git a/src/main/java/com/mediamanager/service/delegate/handler/album/GetAlbumHandler.java b/src/main/java/com/mediamanager/service/delegate/handler/album/GetAlbumHandler.java new file mode 100644 index 0000000..9d388f3 --- /dev/null +++ b/src/main/java/com/mediamanager/service/delegate/handler/album/GetAlbumHandler.java @@ -0,0 +1,48 @@ +package com.mediamanager.service.delegate.handler.album; + +import com.google.protobuf.ByteString; +import com.google.protobuf.InvalidProtocolBufferException; +import com.mediamanager.mapper.AlbumMapper; +import com.mediamanager.model.Album; +import com.mediamanager.protocol.TransportProtocol; +import com.mediamanager.protocol.messages.AlbumMessages; +import com.mediamanager.service.delegate.ActionHandler; +import com.mediamanager.service.delegate.annotation.Action; +import com.mediamanager.service.album.AlbumService; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.List; + + +@Action("album.getAll") +public class GetAlbumHandler implements ActionHandler { + private static final Logger logger = LogManager.getLogger(GetAlbumHandler.class); + + private final AlbumService albumService; + + public GetAlbumHandler(AlbumService albumService){this.albumService = albumService;} + + @Override + public TransportProtocol.Response.Builder handle(ByteString requestPayload) throws InvalidProtocolBufferException { + try{ + List albums = albumService.getAllAlbums(); + AlbumMessages.GetAlbumsResponse.Builder responseBuilder = AlbumMessages.GetAlbumsResponse.newBuilder(); + + for (Album album : albums) { + AlbumMessages.Album albumProto = AlbumMapper.toProtobuf(album); + responseBuilder.addAlbums(albumProto); + } + AlbumMessages.GetAlbumsResponse getAlbumsResponse = responseBuilder.build(); + + return TransportProtocol.Response.newBuilder() + .setPayload(getAlbumsResponse.toByteString()); + + }catch (Exception e){ + logger.error("Error getting albums", e); + return TransportProtocol.Response.newBuilder() + .setStatusCode(500) + .setPayload(ByteString.copyFromUtf8("Error: " + e.getMessage())); + } + } +} diff --git a/src/main/java/com/mediamanager/service/delegate/handler/album/UpdateAlbumHandler.java b/src/main/java/com/mediamanager/service/delegate/handler/album/UpdateAlbumHandler.java new file mode 100644 index 0000000..ac10f55 --- /dev/null +++ b/src/main/java/com/mediamanager/service/delegate/handler/album/UpdateAlbumHandler.java @@ -0,0 +1,72 @@ +package com.mediamanager.service.delegate.handler.album; + +import com.google.protobuf.ByteString; +import com.google.protobuf.InvalidProtocolBufferException; +import com.mediamanager.mapper.AlbumMapper; +import com.mediamanager.model.Album; +import com.mediamanager.protocol.TransportProtocol; +import com.mediamanager.protocol.messages.AlbumMessages; +import com.mediamanager.service.delegate.ActionHandler; +import com.mediamanager.service.delegate.annotation.Action; +import com.mediamanager.service.album.AlbumService; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.Optional; + +@Action("album.update") +public class UpdateAlbumHandler implements ActionHandler { + private static final Logger logger = LogManager.getLogger(UpdateAlbumHandler.class); + private final AlbumService albumService; + + public UpdateAlbumHandler(AlbumService albumService) { + this.albumService = albumService; + } + + @Override + public TransportProtocol.Response.Builder handle(ByteString requestPayload) throws InvalidProtocolBufferException { + try{ + AlbumMessages.UpdateAlbumRequest updateRequest = + AlbumMessages.UpdateAlbumRequest.parseFrom(requestPayload); + + int id = updateRequest.getId(); + Optional albumOpt = albumService.updateAlbum( + id, + updateRequest.getName(), + updateRequest.getYear() > 0 ? updateRequest.getYear() : null, + updateRequest.getNumberOfDiscs() > 0 ? updateRequest.getNumberOfDiscs() : null, + updateRequest.getCode().isEmpty() ? null : updateRequest.getCode(), + updateRequest.getIsCompilation(), + updateRequest.getFkAlbumtypeId() > 0 ? updateRequest.getFkAlbumtypeId() : null, + updateRequest.getFkAlbumartId() > 0 ? updateRequest.getFkAlbumartId() : null + ); + + if(albumOpt.isEmpty()){ + logger.warn("Album not found with ID: {}", id); + return TransportProtocol.Response.newBuilder() + .setStatusCode(404) + .setPayload(ByteString.copyFromUtf8("Album not found")); + } + + AlbumMessages.Album albumProto = AlbumMapper.toProtobuf(albumOpt.get()); + + AlbumMessages.UpdateAlbumResponse updateResponse = AlbumMessages.UpdateAlbumResponse.newBuilder() + .setAlbum(albumProto) + .build(); + + return TransportProtocol.Response.newBuilder() + .setPayload(updateResponse.toByteString()); + + } catch (IllegalArgumentException e){ + logger.error("Validation error", e); + return TransportProtocol.Response.newBuilder() + .setStatusCode(400) + .setPayload(ByteString.copyFromUtf8("Validation error: " + e.getMessage())); + } catch (Exception e) { + logger.error("Error updating album", e); + return TransportProtocol.Response.newBuilder() + .setStatusCode(500) + .setPayload(ByteString.copyFromUtf8("Error: " + e.getMessage())); + } + } +} diff --git a/src/main/proto/album.proto b/src/main/proto/album.proto new file mode 100644 index 0000000..44e876e --- /dev/null +++ b/src/main/proto/album.proto @@ -0,0 +1,77 @@ +syntax = "proto3"; + +option java_package = "com.mediamanager.protocol.messages"; +option java_outer_classname = "AlbumMessages"; + +package mediamanager.messages; + +message Album { + int32 id = 1; + string name = 2; + int32 year = 3; + int32 number_of_discs = 4; + 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; +} + +message CreateAlbumRequest { + string name = 1; + int32 year = 2; + int32 number_of_discs = 3; + string code = 4; + bool is_compilation = 5; + int32 fk_albumtype_id = 6; + int32 fk_albumart_id = 7; +} + +message CreateAlbumResponse { + Album album = 1; +} + +message GetAlbumsRequest { + +} + +message GetAlbumsResponse { + repeated Album albums = 1; +} + +message GetAlbumByIdRequest { + int32 id = 1; +} + +message GetAlbumByIdResponse { + Album album = 1; +} + +message UpdateAlbumRequest { + int32 id = 1; + string name = 2; + int32 year = 3; + int32 number_of_discs = 4; + string code = 5; + bool is_compilation = 6; + int32 fk_albumtype_id = 7; + int32 fk_albumart_id = 8; +} + +message UpdateAlbumResponse { + Album album = 1; +} + +message DeleteAlbumRequest { + int32 id = 1; +} + +message DeleteAlbumResponse { + bool success = 1; + string message = 2; +}