Compare commits
8 Commits
61c5124d27
...
ae6f522901
| Author | SHA1 | Date |
|---|---|---|
|
|
ae6f522901 | |
|
|
fd71114426 | |
|
|
2e0a39a455 | |
|
|
eebe142b8c | |
|
|
4073c53b3d | |
|
|
8208b58b34 | |
|
|
dfa4185e64 | |
|
|
07f7405b03 |
|
|
@ -0,0 +1,50 @@
|
|||
package com.mediamanager.mapper;
|
||||
|
||||
import com.mediamanager.model.Disc;
|
||||
import com.mediamanager.protocol.messages.DiscMessages;
|
||||
|
||||
public class DiscMapper {
|
||||
public static DiscMessages.Disc toProtobuf(Disc entity) {
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Integer discNumber = entity.getDiscNumber();
|
||||
if (discNumber == null) {
|
||||
throw new IllegalArgumentException("Disc number cannot be null");
|
||||
}
|
||||
|
||||
DiscMessages.Disc.Builder builder = DiscMessages.Disc.newBuilder()
|
||||
.setDiscNumber(discNumber);
|
||||
|
||||
Integer id = entity.getId();
|
||||
if (id != null) {
|
||||
builder.setId(id);
|
||||
}
|
||||
|
||||
// Map Album foreign key
|
||||
if (entity.getAlbum() != null && entity.getAlbum().getId() != null) {
|
||||
builder.setFkAlbumId(entity.getAlbum().getId());
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
public static Disc toEntity(DiscMessages.Disc protobuf) {
|
||||
if (protobuf == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Disc entity = new Disc();
|
||||
|
||||
if (protobuf.getId() > 0) {
|
||||
entity.setId(protobuf.getId());
|
||||
}
|
||||
|
||||
entity.setDiscNumber(protobuf.getDiscNumber());
|
||||
|
||||
// Note: Foreign key relationship (Album) is handled in the service layer
|
||||
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
package com.mediamanager.mapper;
|
||||
|
||||
import com.mediamanager.model.Track;
|
||||
import com.mediamanager.protocol.messages.TrackMessages;
|
||||
|
||||
public class TrackMapper {
|
||||
public static TrackMessages.Track toProtobuf(Track entity) {
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Integer trackNumber = entity.getTrackNumber();
|
||||
if (trackNumber == null) {
|
||||
throw new IllegalArgumentException("Track number cannot be null");
|
||||
}
|
||||
|
||||
String title = entity.getTitle();
|
||||
if (title == null) {
|
||||
throw new IllegalArgumentException("Title cannot be null");
|
||||
}
|
||||
|
||||
String filepath = entity.getFilepath();
|
||||
if (filepath == null) {
|
||||
throw new IllegalArgumentException("Filepath cannot be null");
|
||||
}
|
||||
|
||||
TrackMessages.Track.Builder builder = TrackMessages.Track.newBuilder()
|
||||
.setTrackNumber(trackNumber)
|
||||
.setTitle(title)
|
||||
.setFilepath(filepath);
|
||||
|
||||
Integer id = entity.getId();
|
||||
if (id != null) {
|
||||
builder.setId(id);
|
||||
}
|
||||
|
||||
// Map duration (optional)
|
||||
Integer duration = entity.getDuration();
|
||||
if (duration != null) {
|
||||
builder.setDuration(duration);
|
||||
}
|
||||
|
||||
// Map Disc foreign key (required)
|
||||
if (entity.getDisc() != null && entity.getDisc().getId() != null) {
|
||||
builder.setFkDiscId(entity.getDisc().getId());
|
||||
}
|
||||
|
||||
// Map Composer foreign key (optional)
|
||||
if (entity.getComposer() != null && entity.getComposer().getId() != null) {
|
||||
builder.setFkComposerId(entity.getComposer().getId());
|
||||
}
|
||||
|
||||
// Map BitDepth foreign key (optional)
|
||||
if (entity.getBitDepth() != null && entity.getBitDepth().getId() != null) {
|
||||
builder.setFkBitdepthId(entity.getBitDepth().getId());
|
||||
}
|
||||
|
||||
// Map BitRate foreign key (optional)
|
||||
if (entity.getBitRate() != null && entity.getBitRate().getId() != null) {
|
||||
builder.setFkBitrateId(entity.getBitRate().getId());
|
||||
}
|
||||
|
||||
// Map SamplingRate foreign key (optional)
|
||||
if (entity.getSamplingRate() != null && entity.getSamplingRate().getId() != null) {
|
||||
builder.setFkSamplingrateId(entity.getSamplingRate().getId());
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
public static Track toEntity(TrackMessages.Track protobuf) {
|
||||
if (protobuf == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Track entity = new Track();
|
||||
|
||||
if (protobuf.getId() > 0) {
|
||||
entity.setId(protobuf.getId());
|
||||
}
|
||||
|
||||
entity.setTrackNumber(protobuf.getTrackNumber());
|
||||
entity.setTitle(protobuf.getTitle());
|
||||
entity.setFilepath(protobuf.getFilepath());
|
||||
|
||||
if (protobuf.getDuration() > 0) {
|
||||
entity.setDuration(protobuf.getDuration());
|
||||
}
|
||||
|
||||
// Note: Foreign key relationships (Disc, Composer, BitDepth, BitRate, SamplingRate)
|
||||
// are handled in the service layer
|
||||
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
package com.mediamanager.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "disc")
|
||||
public class Disc {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Integer id;
|
||||
|
||||
@Column(name = "disc_number", nullable = false)
|
||||
private Integer discNumber;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "fk_album_id",nullable = false)
|
||||
private Album album;
|
||||
|
||||
public Disc() {
|
||||
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getDiscNumber() {
|
||||
return discNumber;
|
||||
}
|
||||
|
||||
public void setDiscNumber(Integer discNumber) {
|
||||
this.discNumber = discNumber;
|
||||
}
|
||||
|
||||
public Album getAlbum() {
|
||||
return album;
|
||||
}
|
||||
|
||||
public void setAlbum(Album album) {
|
||||
this.album = album;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,128 @@
|
|||
package com.mediamanager.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "track")
|
||||
public class Track {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Integer id;
|
||||
|
||||
@Column(name = "track_number")
|
||||
private Integer trackNumber;
|
||||
|
||||
@Column
|
||||
private String title;
|
||||
|
||||
@Column
|
||||
private Integer duration;
|
||||
|
||||
@Column
|
||||
private String filepath;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "fk_disc_id")
|
||||
private Disc disc;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "fk_composer_id")
|
||||
private Composer composer;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "fk_bit_depth_id")
|
||||
private BitDepth bitDepth;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "fk_bit_rate_id")
|
||||
private BitRate bitRate;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "fk_sampling_rate_id")
|
||||
private SamplingRate samplingRate;
|
||||
|
||||
public Track() {
|
||||
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getTrackNumber() {
|
||||
return trackNumber;
|
||||
}
|
||||
|
||||
public void setTrackNumber(Integer trackNumber) {
|
||||
this.trackNumber = trackNumber;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String name) {
|
||||
this.title = name;
|
||||
}
|
||||
|
||||
public Integer getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public void setDuration(Integer duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public String getFilepath() {
|
||||
return filepath;
|
||||
}
|
||||
|
||||
public void setFilepath(String filepath) {
|
||||
this.filepath = filepath;
|
||||
}
|
||||
|
||||
public Disc getDisc() {
|
||||
return disc;
|
||||
}
|
||||
|
||||
public void setDisc(Disc disc) {
|
||||
this.disc = disc;
|
||||
}
|
||||
|
||||
public Composer getComposer() {
|
||||
return composer;
|
||||
}
|
||||
|
||||
public void setComposer(Composer composer) {
|
||||
this.composer = composer;
|
||||
}
|
||||
|
||||
public BitDepth getBitDepth() {
|
||||
return bitDepth;
|
||||
}
|
||||
|
||||
public void setBitDepth(BitDepth bitDepth) {
|
||||
this.bitDepth = bitDepth;
|
||||
}
|
||||
|
||||
public BitRate getBitRate() {
|
||||
return bitRate;
|
||||
}
|
||||
|
||||
public void setBitRate(BitRate bitRate) {
|
||||
this.bitRate = bitRate;
|
||||
}
|
||||
|
||||
public SamplingRate getSamplingRate() {
|
||||
return samplingRate;
|
||||
}
|
||||
|
||||
public void setSamplingRate(SamplingRate samplingRate) {
|
||||
this.samplingRate = samplingRate;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
package com.mediamanager.repository;
|
||||
|
||||
import com.mediamanager.model.Disc;
|
||||
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 DiscRepository {
|
||||
private static final Logger logger = LogManager.getLogger(DiscRepository.class);
|
||||
|
||||
private final EntityManagerFactory entityManagerFactory;
|
||||
|
||||
public DiscRepository(EntityManagerFactory entityManagerFactory) {
|
||||
this.entityManagerFactory = entityManagerFactory;
|
||||
}
|
||||
|
||||
public Disc save(Disc disc) {
|
||||
logger.debug("Saving Disc: {}", disc.getDiscNumber());
|
||||
EntityManager em = entityManagerFactory.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
try {
|
||||
em.persist(disc);
|
||||
em.getTransaction().commit();
|
||||
logger.debug("Disc has been saved successfully");
|
||||
return disc;
|
||||
} catch (Exception e) {
|
||||
em.getTransaction().rollback();
|
||||
logger.error("Error while saving Disc: {}", e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
if (em.isOpen()) em.close();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Disc> findAll() {
|
||||
logger.debug("Finding All Disc");
|
||||
EntityManager em = entityManagerFactory.createEntityManager();
|
||||
try{
|
||||
return em.createQuery("select d from Disc d", Disc.class).getResultList();
|
||||
}finally {
|
||||
if (em.isOpen()) em.close();
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Disc> findById(Integer id) {
|
||||
logger.debug("Finding Disc with id: {}", id);
|
||||
EntityManager em = entityManagerFactory.createEntityManager();
|
||||
try{
|
||||
Disc disc = em.find(Disc.class, id);
|
||||
return Optional.ofNullable(disc);
|
||||
}finally {
|
||||
if (em.isOpen()) em.close();
|
||||
}
|
||||
}
|
||||
|
||||
public Disc update(Disc disc) {
|
||||
logger.debug("Updating Disc: {}", disc.getDiscNumber());
|
||||
EntityManager em = entityManagerFactory.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
try {
|
||||
Disc updated = em.merge(disc);
|
||||
em.getTransaction().commit();
|
||||
logger.debug("Disc has been updated successfully");
|
||||
return updated;
|
||||
} catch (Exception e) {
|
||||
em.getTransaction().rollback();
|
||||
logger.error("Error while updating Disc: {}", e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
if (em.isOpen()) em.close();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean deleteById(Integer id){
|
||||
logger.debug("Deleting Disc with id: {}", id);
|
||||
EntityManager em = entityManagerFactory.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
try{
|
||||
Disc disc = em.find(Disc.class, id);
|
||||
if (disc == null) {
|
||||
em.getTransaction().rollback();
|
||||
return false;
|
||||
}
|
||||
em.remove(disc);
|
||||
em.getTransaction().commit();
|
||||
logger.debug("Disc has been deleted successfully");
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
em.getTransaction().rollback();
|
||||
logger.error("Error while deleting Disc: {}", e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
if (em.isOpen()) em.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
package com.mediamanager.repository;
|
||||
|
||||
import com.mediamanager.model.Track;
|
||||
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 TrackRepository {
|
||||
private static final Logger logger = LogManager.getLogger(TrackRepository.class);
|
||||
|
||||
private final EntityManagerFactory entityManagerFactory;
|
||||
|
||||
public TrackRepository(EntityManagerFactory entityManagerFactory) {
|
||||
this.entityManagerFactory = entityManagerFactory;
|
||||
}
|
||||
|
||||
public Track save(Track track) {
|
||||
logger.debug("Saving Track: {}", track.getTitle());
|
||||
EntityManager em = entityManagerFactory.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
try {
|
||||
em.persist(track);
|
||||
em.getTransaction().commit();
|
||||
logger.debug("Track has been saved successfully");
|
||||
return track;
|
||||
} catch (Exception e) {
|
||||
em.getTransaction().rollback();
|
||||
logger.error("Error while saving Track: {}", e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
if (em.isOpen()) em.close();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Track> findAll() {
|
||||
logger.debug("Finding All Track");
|
||||
EntityManager em = entityManagerFactory.createEntityManager();
|
||||
try{
|
||||
return em.createQuery("select t from Track t", Track.class).getResultList();
|
||||
}finally {
|
||||
if (em.isOpen()) em.close();
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Track> findById(Integer id) {
|
||||
logger.debug("Finding Track with id: {}", id);
|
||||
EntityManager em = entityManagerFactory.createEntityManager();
|
||||
try{
|
||||
Track track = em.find(Track.class, id);
|
||||
return Optional.ofNullable(track);
|
||||
}finally {
|
||||
if (em.isOpen()) em.close();
|
||||
}
|
||||
}
|
||||
|
||||
public Track update(Track track) {
|
||||
logger.debug("Updating Track: {}", track.getTitle());
|
||||
EntityManager em = entityManagerFactory.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
try {
|
||||
Track updated = em.merge(track);
|
||||
em.getTransaction().commit();
|
||||
logger.debug("Track has been updated successfully");
|
||||
return updated;
|
||||
} catch (Exception e) {
|
||||
em.getTransaction().rollback();
|
||||
logger.error("Error while updating Track: {}", e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
if (em.isOpen()) em.close();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean deleteById(Integer id){
|
||||
logger.debug("Deleting Track with id: {}", id);
|
||||
EntityManager em = entityManagerFactory.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
try{
|
||||
Track track = em.find(Track.class, id);
|
||||
if (track == null) {
|
||||
em.getTransaction().rollback();
|
||||
return false;
|
||||
}
|
||||
em.remove(track);
|
||||
em.getTransaction().commit();
|
||||
logger.debug("Track has been deleted successfully");
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
em.getTransaction().rollback();
|
||||
logger.error("Error while deleting Track: {}", e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
if (em.isOpen()) em.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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.repository.DiscRepository;
|
||||
import com.mediamanager.service.album.AlbumService;
|
||||
import com.mediamanager.service.albumart.AlbumArtService;
|
||||
import com.mediamanager.service.albumhasartist.AlbumHasArtistService;
|
||||
|
|
@ -11,10 +12,13 @@ import com.mediamanager.service.albumtype.AlbumTypeService;
|
|||
import com.mediamanager.service.bitdepth.BitDepthService;
|
||||
import com.mediamanager.service.bitrate.BitRateService;
|
||||
import com.mediamanager.service.composer.ComposerService;
|
||||
|
||||
import com.mediamanager.repository.GenreRepository;
|
||||
import com.mediamanager.service.artist.ArtistService;
|
||||
import com.mediamanager.service.track.TrackService;
|
||||
import com.mediamanager.service.delegate.annotation.Action;
|
||||
|
||||
import com.mediamanager.service.disc.DiscService;
|
||||
import com.mediamanager.service.genre.GenreService;
|
||||
import com.mediamanager.service.samplingrate.SamplingRateService;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
|
|
@ -100,6 +104,14 @@ public class DelegateActionManager {
|
|||
AlbumHasGenreService albumHasGenreService = new AlbumHasGenreService(albumHasGenreRepository, albumRepository, genreRepository);
|
||||
serviceLocator.register(AlbumHasGenreService.class, albumHasGenreService);
|
||||
|
||||
DiscRepository discRepository = new DiscRepository(entityManagerFactory);
|
||||
DiscService discService = new DiscService(discRepository, albumRepository);
|
||||
serviceLocator.register(DiscService.class, discService);
|
||||
|
||||
TrackRepository trackRepository = new TrackRepository(entityManagerFactory);
|
||||
TrackService trackService = new TrackService(trackRepository, discRepository, composerRepository, bitDepthRepository, bitRateRepository, samplingRateRepository);
|
||||
serviceLocator.register(TrackService.class, trackService);
|
||||
|
||||
serviceLocator.logRegisteredServices();
|
||||
|
||||
logger.info("Services initialized successfully");
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
package com.mediamanager.service.delegate.handler.disc;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.mediamanager.mapper.DiscMapper;
|
||||
import com.mediamanager.model.Disc;
|
||||
import com.mediamanager.protocol.TransportProtocol;
|
||||
import com.mediamanager.protocol.messages.DiscMessages;
|
||||
import com.mediamanager.service.delegate.ActionHandler;
|
||||
import com.mediamanager.service.delegate.annotation.Action;
|
||||
import com.mediamanager.service.disc.DiscService;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@Action("disc.create")
|
||||
public class CreateDiscHandler implements ActionHandler {
|
||||
private static final Logger logger = LogManager.getLogger(CreateDiscHandler.class);
|
||||
private final DiscService discService;
|
||||
|
||||
public CreateDiscHandler(DiscService discService) {
|
||||
this.discService = discService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportProtocol.Response.Builder handle(ByteString requestPayload) throws InvalidProtocolBufferException {
|
||||
try {
|
||||
DiscMessages.CreateDiscRequest createRequest =
|
||||
DiscMessages.CreateDiscRequest.parseFrom(requestPayload);
|
||||
|
||||
Disc disc = discService.createDisc(
|
||||
createRequest.getDiscNumber(),
|
||||
createRequest.getFkAlbumId() > 0 ? createRequest.getFkAlbumId() : null
|
||||
);
|
||||
|
||||
DiscMessages.Disc discProto = DiscMapper.toProtobuf(disc);
|
||||
DiscMessages.CreateDiscResponse createDiscResponse = DiscMessages.CreateDiscResponse.newBuilder()
|
||||
.setDisc(discProto)
|
||||
.build();
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setPayload(createDiscResponse.toByteString());
|
||||
} catch (IllegalArgumentException e) {
|
||||
logger.error("Validation error", e);
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(400)
|
||||
.setPayload(ByteString.copyFromUtf8("Invalid request"));
|
||||
|
||||
} catch (InvalidProtocolBufferException e) {
|
||||
logger.error("Invalid CreateDiscRequest payload", e);
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(400)
|
||||
.setPayload(ByteString.copyFromUtf8("Invalid request payload"));
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Error creating disc", e);
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(500)
|
||||
.setPayload(ByteString.copyFromUtf8("Internal server error"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
package com.mediamanager.service.delegate.handler.disc;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.mediamanager.protocol.TransportProtocol;
|
||||
import com.mediamanager.protocol.messages.DiscMessages;
|
||||
import com.mediamanager.service.delegate.ActionHandler;
|
||||
import com.mediamanager.service.delegate.annotation.Action;
|
||||
import com.mediamanager.service.disc.DiscService;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@Action("disc.delete")
|
||||
public class DeleteDiscHandler implements ActionHandler {
|
||||
private static final Logger logger = LogManager.getLogger(DeleteDiscHandler.class);
|
||||
|
||||
private final DiscService discService;
|
||||
|
||||
public DeleteDiscHandler(DiscService discService) {
|
||||
this.discService = discService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportProtocol.Response.Builder handle(ByteString requestPayload)
|
||||
throws InvalidProtocolBufferException {
|
||||
|
||||
try {
|
||||
DiscMessages.DeleteDiscRequest deleteRequest =
|
||||
DiscMessages.DeleteDiscRequest.parseFrom(requestPayload);
|
||||
int id = deleteRequest.getId();
|
||||
boolean success = discService.deleteDisc(id);
|
||||
DiscMessages.DeleteDiscResponse deleteResponse;
|
||||
if (success) {
|
||||
deleteResponse = DiscMessages.DeleteDiscResponse.newBuilder()
|
||||
.setMessage("Disc deleted successfully")
|
||||
.build();
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setPayload(deleteResponse.toByteString());
|
||||
} else {
|
||||
deleteResponse = DiscMessages.DeleteDiscResponse.newBuilder()
|
||||
.setMessage("Disc not found")
|
||||
.build();
|
||||
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(404)
|
||||
.setPayload(deleteResponse.toByteString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Error deleting disc", e);
|
||||
DiscMessages.DeleteDiscResponse deleteResponse =
|
||||
DiscMessages.DeleteDiscResponse.newBuilder()
|
||||
.setMessage("Error: " + e.getMessage())
|
||||
.build();
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(500)
|
||||
.setPayload(deleteResponse.toByteString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
package com.mediamanager.service.delegate.handler.disc;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.mediamanager.mapper.DiscMapper;
|
||||
import com.mediamanager.model.Disc;
|
||||
import com.mediamanager.protocol.TransportProtocol;
|
||||
import com.mediamanager.protocol.messages.DiscMessages;
|
||||
import com.mediamanager.service.delegate.ActionHandler;
|
||||
import com.mediamanager.service.delegate.annotation.Action;
|
||||
import com.mediamanager.service.disc.DiscService;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Action(value = "disc.getById")
|
||||
public class GetDiscByIdHandler implements ActionHandler {
|
||||
private static final Logger logger = LogManager.getLogger(GetDiscByIdHandler.class);
|
||||
private final DiscService discService;
|
||||
|
||||
public GetDiscByIdHandler(DiscService discService) {
|
||||
this.discService = discService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportProtocol.Response.Builder handle(ByteString requestPayload)
|
||||
throws InvalidProtocolBufferException{
|
||||
|
||||
try{
|
||||
DiscMessages.GetDiscByIdRequest getByIdRequest =
|
||||
DiscMessages.GetDiscByIdRequest.parseFrom(requestPayload);
|
||||
int id = getByIdRequest.getId();
|
||||
|
||||
Optional<Disc> discOpt = discService.getDiscById(id);
|
||||
|
||||
if (discOpt.isEmpty()){
|
||||
logger.warn("Disc not found with ID: {}", id);
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(404)
|
||||
.setPayload(ByteString.copyFromUtf8("Disc not found"));
|
||||
}
|
||||
DiscMessages.Disc discProto = DiscMapper.toProtobuf(discOpt.get());
|
||||
DiscMessages.GetDiscByIdResponse getByIdResponse = DiscMessages.GetDiscByIdResponse.newBuilder()
|
||||
.setDisc(discProto)
|
||||
.build();
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setPayload(getByIdResponse.toByteString());
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting disc by ID", e);
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(500)
|
||||
.setPayload(ByteString.copyFromUtf8("Error: "+ e.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
package com.mediamanager.service.delegate.handler.disc;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.mediamanager.mapper.DiscMapper;
|
||||
import com.mediamanager.model.Disc;
|
||||
import com.mediamanager.protocol.TransportProtocol;
|
||||
import com.mediamanager.protocol.messages.DiscMessages;
|
||||
import com.mediamanager.service.delegate.ActionHandler;
|
||||
import com.mediamanager.service.delegate.annotation.Action;
|
||||
import com.mediamanager.service.disc.DiscService;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Action("disc.getAll")
|
||||
public class GetDiscHandler implements ActionHandler {
|
||||
private static final Logger logger = LogManager.getLogger(GetDiscHandler.class);
|
||||
|
||||
private final DiscService discService;
|
||||
|
||||
public GetDiscHandler(DiscService discService){this.discService = discService;}
|
||||
|
||||
@Override
|
||||
public TransportProtocol.Response.Builder handle(ByteString requestPayload) throws InvalidProtocolBufferException {
|
||||
try{
|
||||
List<Disc> discs = discService.getAllDiscs();
|
||||
DiscMessages.GetDiscsResponse.Builder responseBuilder = DiscMessages.GetDiscsResponse.newBuilder();
|
||||
|
||||
for (Disc disc : discs) {
|
||||
DiscMessages.Disc discProto = DiscMapper.toProtobuf(disc);
|
||||
responseBuilder.addDiscs(discProto);
|
||||
}
|
||||
DiscMessages.GetDiscsResponse getDiscsResponse = responseBuilder.build();
|
||||
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setPayload(getDiscsResponse.toByteString());
|
||||
|
||||
}catch (Exception e){
|
||||
logger.error("Error getting discs", e);
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(500)
|
||||
.setPayload(ByteString.copyFromUtf8("Error: " + e.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
package com.mediamanager.service.delegate.handler.disc;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.mediamanager.mapper.DiscMapper;
|
||||
import com.mediamanager.model.Disc;
|
||||
import com.mediamanager.protocol.TransportProtocol;
|
||||
import com.mediamanager.protocol.messages.DiscMessages;
|
||||
import com.mediamanager.service.delegate.ActionHandler;
|
||||
import com.mediamanager.service.delegate.annotation.Action;
|
||||
import com.mediamanager.service.disc.DiscService;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Action("disc.update")
|
||||
public class UpdateDiscHandler implements ActionHandler {
|
||||
private static final Logger logger = LogManager.getLogger(UpdateDiscHandler.class);
|
||||
private final DiscService discService;
|
||||
|
||||
public UpdateDiscHandler(DiscService discService) {
|
||||
this.discService = discService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportProtocol.Response.Builder handle(ByteString requestPayload)
|
||||
throws InvalidProtocolBufferException {
|
||||
try {
|
||||
DiscMessages.UpdateDiscRequest updateRequest =
|
||||
DiscMessages.UpdateDiscRequest.parseFrom(requestPayload);
|
||||
|
||||
int id = updateRequest.getId();
|
||||
Integer discNumber = updateRequest.getDiscNumber();
|
||||
Integer albumId = updateRequest.getFkAlbumId() > 0 ? updateRequest.getFkAlbumId() : null;
|
||||
|
||||
Optional<Disc> discOpt = discService.updateDisc(
|
||||
id,
|
||||
discNumber,
|
||||
albumId
|
||||
);
|
||||
|
||||
if (discOpt.isEmpty()) {
|
||||
logger.warn("Disc not found with ID: {}", id);
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(404)
|
||||
.setPayload(ByteString.copyFromUtf8("Disc not found"));
|
||||
}
|
||||
|
||||
DiscMessages.Disc discProto = DiscMapper.toProtobuf(discOpt.get());
|
||||
|
||||
DiscMessages.UpdateDiscResponse updateResponse =
|
||||
DiscMessages.UpdateDiscResponse.newBuilder()
|
||||
.setDisc(discProto)
|
||||
.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 disc", e);
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(500)
|
||||
.setPayload(ByteString.copyFromUtf8("Error: " + e.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
package com.mediamanager.service.delegate.handler.track;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.mediamanager.mapper.TrackMapper;
|
||||
import com.mediamanager.model.Track;
|
||||
import com.mediamanager.protocol.TransportProtocol;
|
||||
import com.mediamanager.protocol.messages.TrackMessages;
|
||||
import com.mediamanager.service.delegate.ActionHandler;
|
||||
import com.mediamanager.service.delegate.annotation.Action;
|
||||
import com.mediamanager.service.track.TrackService;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@Action("track.create")
|
||||
public class CreateTrackHandler implements ActionHandler {
|
||||
private static final Logger logger = LogManager.getLogger(CreateTrackHandler.class);
|
||||
private final TrackService trackService;
|
||||
|
||||
public CreateTrackHandler(TrackService trackService) {
|
||||
this.trackService = trackService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportProtocol.Response.Builder handle(ByteString requestPayload) throws InvalidProtocolBufferException {
|
||||
try{
|
||||
TrackMessages.CreateTrackRequest createRequest =
|
||||
TrackMessages.CreateTrackRequest.parseFrom(requestPayload);
|
||||
|
||||
Track track = trackService.createTrack(
|
||||
createRequest.getTrackNumber(),
|
||||
createRequest.getTitle(),
|
||||
createRequest.hasDuration() ? createRequest.getDuration().getValue() : null,
|
||||
createRequest.getFilepath(),
|
||||
createRequest.getFkDiscId() > 0 ? createRequest.getFkDiscId() : null,
|
||||
createRequest.hasFkComposerId() ? createRequest.getFkComposerId().getValue() : null,
|
||||
createRequest.hasFkBitdepthId() ? createRequest.getFkBitdepthId().getValue() : null,
|
||||
createRequest.hasFkBitrateId() ? createRequest.getFkBitrateId().getValue() : null,
|
||||
createRequest.hasFkSamplingrateId() ? createRequest.getFkSamplingrateId().getValue() : null
|
||||
);
|
||||
|
||||
TrackMessages.Track trackProto = TrackMapper.toProtobuf(track);
|
||||
TrackMessages.CreateTrackResponse createTrackResponse = TrackMessages.CreateTrackResponse.newBuilder()
|
||||
.setTrack(trackProto)
|
||||
.build();
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setPayload(createTrackResponse.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 track", e);
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(500)
|
||||
.setPayload(ByteString.copyFromUtf8("Error: " + e.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
package com.mediamanager.service.delegate.handler.track;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.mediamanager.protocol.TransportProtocol;
|
||||
import com.mediamanager.protocol.messages.TrackMessages;
|
||||
import com.mediamanager.service.delegate.ActionHandler;
|
||||
import com.mediamanager.service.delegate.annotation.Action;
|
||||
import com.mediamanager.service.track.TrackService;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@Action("track.delete")
|
||||
public class DeleteTrackHandler implements ActionHandler {
|
||||
private static final Logger logger = LogManager.getLogger(DeleteTrackHandler.class);
|
||||
|
||||
private final TrackService trackService;
|
||||
|
||||
public DeleteTrackHandler(TrackService trackService) {
|
||||
this.trackService = trackService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportProtocol.Response.Builder handle(ByteString requestPayload)
|
||||
throws InvalidProtocolBufferException {
|
||||
|
||||
try {
|
||||
TrackMessages.DeleteTrackRequest deleteRequest =
|
||||
TrackMessages.DeleteTrackRequest.parseFrom(requestPayload);
|
||||
int id = deleteRequest.getId();
|
||||
boolean success = trackService.deleteTrack(id);
|
||||
TrackMessages.DeleteTrackResponse deleteResponse;
|
||||
if (success) {
|
||||
deleteResponse = TrackMessages.DeleteTrackResponse.newBuilder()
|
||||
.setSuccess(true)
|
||||
.setMessage("Track deleted successfully")
|
||||
.build();
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setPayload(deleteResponse.toByteString());
|
||||
} else {
|
||||
deleteResponse = TrackMessages.DeleteTrackResponse.newBuilder()
|
||||
.setSuccess(false)
|
||||
.setMessage("Track not found")
|
||||
.build();
|
||||
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(404)
|
||||
.setPayload(deleteResponse.toByteString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Error deleting track", e);
|
||||
TrackMessages.DeleteTrackResponse deleteResponse =
|
||||
TrackMessages.DeleteTrackResponse.newBuilder()
|
||||
.setSuccess(false)
|
||||
.setMessage("Error: " + e.getMessage())
|
||||
.build();
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(500)
|
||||
.setPayload(deleteResponse.toByteString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
package com.mediamanager.service.delegate.handler.track;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.mediamanager.mapper.TrackMapper;
|
||||
import com.mediamanager.model.Track;
|
||||
import com.mediamanager.protocol.TransportProtocol;
|
||||
import com.mediamanager.protocol.messages.TrackMessages;
|
||||
import com.mediamanager.service.delegate.ActionHandler;
|
||||
import com.mediamanager.service.delegate.annotation.Action;
|
||||
import com.mediamanager.service.track.TrackService;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Action(value = "track.getById")
|
||||
public class GetTrackByIdHandler implements ActionHandler {
|
||||
private static final Logger logger = LogManager.getLogger(GetTrackByIdHandler.class);
|
||||
private final TrackService trackService;
|
||||
|
||||
public GetTrackByIdHandler(TrackService trackService) {
|
||||
this.trackService = trackService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportProtocol.Response.Builder handle(ByteString requestPayload)
|
||||
throws InvalidProtocolBufferException{
|
||||
|
||||
try{
|
||||
TrackMessages.GetTrackByIdRequest getByIdRequest =
|
||||
TrackMessages.GetTrackByIdRequest.parseFrom(requestPayload);
|
||||
int id = getByIdRequest.getId();
|
||||
|
||||
Optional<Track> trackOpt = trackService.getTrackById(id);
|
||||
|
||||
if (trackOpt.isEmpty()){
|
||||
logger.warn("Track not found with ID: {}", id);
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(404)
|
||||
.setPayload(ByteString.copyFromUtf8("Track not found"));
|
||||
}
|
||||
TrackMessages.Track trackProto = TrackMapper.toProtobuf(trackOpt.get());
|
||||
TrackMessages.GetTrackByIdResponse getByIdResponse = TrackMessages.GetTrackByIdResponse.newBuilder()
|
||||
.setTrack(trackProto)
|
||||
.build();
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setPayload(getByIdResponse.toByteString());
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting track by ID", e);
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(500)
|
||||
.setPayload(ByteString.copyFromUtf8("Error: "+ e.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
package com.mediamanager.service.delegate.handler.track;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.mediamanager.mapper.TrackMapper;
|
||||
import com.mediamanager.model.Track;
|
||||
import com.mediamanager.protocol.TransportProtocol;
|
||||
import com.mediamanager.protocol.messages.TrackMessages;
|
||||
import com.mediamanager.service.delegate.ActionHandler;
|
||||
import com.mediamanager.service.delegate.annotation.Action;
|
||||
import com.mediamanager.service.track.TrackService;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Action("track.getAll")
|
||||
public class GetTrackHandler implements ActionHandler {
|
||||
private static final Logger logger = LogManager.getLogger(GetTrackHandler.class);
|
||||
|
||||
private final TrackService trackService;
|
||||
|
||||
public GetTrackHandler(TrackService trackService){this.trackService = trackService;}
|
||||
|
||||
@Override
|
||||
public TransportProtocol.Response.Builder handle(ByteString requestPayload) throws InvalidProtocolBufferException {
|
||||
try{
|
||||
List<Track> tracks = trackService.getAllTracks();
|
||||
TrackMessages.GetTracksResponse.Builder responseBuilder = TrackMessages.GetTracksResponse.newBuilder();
|
||||
|
||||
for (Track track : tracks) {
|
||||
TrackMessages.Track trackProto = TrackMapper.toProtobuf(track);
|
||||
responseBuilder.addTracks(trackProto);
|
||||
}
|
||||
TrackMessages.GetTracksResponse getTracksResponse = responseBuilder.build();
|
||||
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setPayload(getTracksResponse.toByteString());
|
||||
|
||||
}catch (Exception e){
|
||||
logger.error("Error getting tracks", e);
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(500)
|
||||
.setPayload(ByteString.copyFromUtf8("Error: " + e.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
package com.mediamanager.service.delegate.handler.track;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.mediamanager.mapper.TrackMapper;
|
||||
import com.mediamanager.model.Track;
|
||||
import com.mediamanager.protocol.TransportProtocol;
|
||||
import com.mediamanager.protocol.messages.TrackMessages;
|
||||
import com.mediamanager.service.delegate.ActionHandler;
|
||||
import com.mediamanager.service.delegate.annotation.Action;
|
||||
import com.mediamanager.service.track.TrackService;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Action("track.update")
|
||||
public class UpdateTrackHandler implements ActionHandler {
|
||||
private static final Logger logger = LogManager.getLogger(UpdateTrackHandler.class);
|
||||
private final TrackService trackService;
|
||||
|
||||
public UpdateTrackHandler(TrackService trackService) {
|
||||
this.trackService = trackService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportProtocol.Response.Builder handle(ByteString requestPayload)
|
||||
throws InvalidProtocolBufferException {
|
||||
try {
|
||||
TrackMessages.UpdateTrackRequest updateRequest =
|
||||
TrackMessages.UpdateTrackRequest.parseFrom(requestPayload);
|
||||
|
||||
int id = updateRequest.getId();
|
||||
Integer trackNumber = updateRequest.hasTrackNumber() ? updateRequest.getTrackNumber().getValue() : null;
|
||||
String title = updateRequest.getTitle();
|
||||
Integer duration = updateRequest.hasDuration() ? updateRequest.getDuration().getValue() : null;
|
||||
String filepath = updateRequest.getFilepath();
|
||||
Integer discId = updateRequest.hasFkDiscId() ? updateRequest.getFkDiscId().getValue() : null;
|
||||
Integer composerId = updateRequest.hasFkComposerId() ? updateRequest.getFkComposerId().getValue() : null;
|
||||
Integer bitDepthId = updateRequest.hasFkBitdepthId() ? updateRequest.getFkBitdepthId().getValue() : null;
|
||||
Integer bitRateId = updateRequest.hasFkBitrateId() ? updateRequest.getFkBitrateId().getValue() : null;
|
||||
Integer samplingRateId = updateRequest.hasFkSamplingrateId() ? updateRequest.getFkSamplingrateId().getValue() : null;
|
||||
|
||||
Optional<Track> trackOpt = trackService.updateTrack(
|
||||
id,
|
||||
trackNumber,
|
||||
title,
|
||||
duration,
|
||||
filepath,
|
||||
discId,
|
||||
composerId,
|
||||
bitDepthId,
|
||||
bitRateId,
|
||||
samplingRateId
|
||||
);
|
||||
|
||||
if (trackOpt.isEmpty()) {
|
||||
logger.warn("Track not found with ID: {}", id);
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(404)
|
||||
.setPayload(ByteString.copyFromUtf8("Track not found"));
|
||||
}
|
||||
|
||||
TrackMessages.Track trackProto = TrackMapper.toProtobuf(trackOpt.get());
|
||||
|
||||
TrackMessages.UpdateTrackResponse updateResponse =
|
||||
TrackMessages.UpdateTrackResponse.newBuilder()
|
||||
.setTrack(trackProto)
|
||||
.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 track", e);
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setStatusCode(500)
|
||||
.setPayload(ByteString.copyFromUtf8("Error: " + e.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
package com.mediamanager.service.disc;
|
||||
|
||||
import com.mediamanager.model.Album;
|
||||
import com.mediamanager.model.Disc;
|
||||
import com.mediamanager.repository.AlbumRepository;
|
||||
import com.mediamanager.repository.DiscRepository;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class DiscService {
|
||||
private static final Logger logger = LogManager.getLogger(DiscService.class);
|
||||
private final DiscRepository repository;
|
||||
private final AlbumRepository albumRepository;
|
||||
|
||||
public DiscService(DiscRepository repository, AlbumRepository albumRepository) {
|
||||
this.repository = repository;
|
||||
this.albumRepository = albumRepository;
|
||||
}
|
||||
|
||||
public Disc createDisc(Integer discNumber, Integer albumId) {
|
||||
logger.debug("Creating disc with number: {}", discNumber);
|
||||
if (discNumber == null) {
|
||||
throw new IllegalArgumentException("Disc number cannot be null");
|
||||
}
|
||||
if (albumId == null) {
|
||||
throw new IllegalArgumentException("Album ID cannot be null");
|
||||
}
|
||||
|
||||
Disc disc = new Disc();
|
||||
disc.setDiscNumber(discNumber);
|
||||
|
||||
// Set Album (required)
|
||||
Optional<Album> album = albumRepository.findById(albumId);
|
||||
if (album.isEmpty()) {
|
||||
throw new IllegalArgumentException("Album not found with id: " + albumId);
|
||||
}
|
||||
disc.setAlbum(album.get());
|
||||
|
||||
return repository.save(disc);
|
||||
}
|
||||
|
||||
public List<Disc> getAllDiscs() {
|
||||
logger.info("Getting all discs");
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
public Optional<Disc> getDiscById(Integer id) {
|
||||
if (id == null) {
|
||||
throw new IllegalArgumentException("ID cannot be null");
|
||||
}
|
||||
logger.info("Getting disc by id: {}", id);
|
||||
return repository.findById(id);
|
||||
}
|
||||
|
||||
public Optional<Disc> updateDisc(Integer id, Integer discNumber, Integer albumId) {
|
||||
if (id == null) {
|
||||
throw new IllegalArgumentException("ID cannot be null");
|
||||
}
|
||||
if (discNumber == null) {
|
||||
throw new IllegalArgumentException("Disc number cannot be null");
|
||||
}
|
||||
if (albumId == null) {
|
||||
throw new IllegalArgumentException("Album ID cannot be null");
|
||||
}
|
||||
|
||||
logger.info("Updating disc with id: {}", id);
|
||||
|
||||
Optional<Disc> existingDisc = repository.findById(id);
|
||||
if (existingDisc.isEmpty()) {
|
||||
logger.warn("Disc not found with id: {}", id);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
Disc disc = existingDisc.get();
|
||||
disc.setDiscNumber(discNumber);
|
||||
|
||||
// Update Album
|
||||
Optional<Album> album = albumRepository.findById(albumId);
|
||||
if (album.isEmpty()) {
|
||||
throw new IllegalArgumentException("Album not found with id: " + albumId);
|
||||
}
|
||||
disc.setAlbum(album.get());
|
||||
|
||||
Disc updatedDisc = repository.update(disc);
|
||||
return Optional.of(updatedDisc);
|
||||
}
|
||||
|
||||
public boolean deleteDisc(Integer id) {
|
||||
if (id == null) {
|
||||
throw new IllegalArgumentException("Disc id cannot be null");
|
||||
}
|
||||
logger.info("Deleting disc: {}", id);
|
||||
return repository.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,213 @@
|
|||
package com.mediamanager.service.track;
|
||||
|
||||
import com.mediamanager.model.*;
|
||||
import com.mediamanager.repository.*;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class TrackService {
|
||||
private static final Logger logger = LogManager.getLogger(TrackService.class);
|
||||
private final TrackRepository repository;
|
||||
private final DiscRepository discRepository;
|
||||
private final ComposerRepository composerRepository;
|
||||
private final BitDepthRepository bitDepthRepository;
|
||||
private final BitRateRepository bitRateRepository;
|
||||
private final SamplingRateRepository samplingRateRepository;
|
||||
|
||||
public TrackService(TrackRepository repository,
|
||||
DiscRepository discRepository,
|
||||
ComposerRepository composerRepository,
|
||||
BitDepthRepository bitDepthRepository,
|
||||
BitRateRepository bitRateRepository,
|
||||
SamplingRateRepository samplingRateRepository) {
|
||||
this.repository = repository;
|
||||
this.discRepository = discRepository;
|
||||
this.composerRepository = composerRepository;
|
||||
this.bitDepthRepository = bitDepthRepository;
|
||||
this.bitRateRepository = bitRateRepository;
|
||||
this.samplingRateRepository = samplingRateRepository;
|
||||
}
|
||||
|
||||
public Track createTrack(Integer trackNumber, String title, Integer duration,
|
||||
String filepath, Integer discId, Integer composerId,
|
||||
Integer bitDepthId, Integer bitRateId, Integer samplingRateId) {
|
||||
logger.debug("Creating track with title: {}", title);
|
||||
|
||||
if (trackNumber == null) {
|
||||
throw new IllegalArgumentException("Track number cannot be null");
|
||||
}
|
||||
if (title == null || title.isEmpty()) {
|
||||
throw new IllegalArgumentException("Title cannot be null or empty");
|
||||
}
|
||||
if (filepath == null || filepath.isEmpty()) {
|
||||
throw new IllegalArgumentException("Filepath cannot be null or empty");
|
||||
}
|
||||
if (discId == null) {
|
||||
throw new IllegalArgumentException("Disc ID cannot be null");
|
||||
}
|
||||
|
||||
Track track = new Track();
|
||||
track.setTrackNumber(trackNumber);
|
||||
track.setTitle(title);
|
||||
track.setDuration(duration);
|
||||
track.setFilepath(filepath);
|
||||
|
||||
// Set Disc (required)
|
||||
Optional<Disc> disc = discRepository.findById(discId);
|
||||
if (disc.isEmpty()) {
|
||||
throw new IllegalArgumentException("Disc not found with id: " + discId);
|
||||
}
|
||||
track.setDisc(disc.get());
|
||||
|
||||
// Set Composer (optional)
|
||||
if (composerId != null) {
|
||||
Optional<Composer> composer = composerRepository.findById(composerId);
|
||||
if (composer.isEmpty()) {
|
||||
throw new IllegalArgumentException("Composer not found with id: " + composerId);
|
||||
}
|
||||
track.setComposer(composer.get());
|
||||
}
|
||||
|
||||
// Set BitDepth (optional)
|
||||
if (bitDepthId != null) {
|
||||
Optional<BitDepth> bitDepth = bitDepthRepository.findById(bitDepthId);
|
||||
if (bitDepth.isEmpty()) {
|
||||
throw new IllegalArgumentException("BitDepth not found with id: " + bitDepthId);
|
||||
}
|
||||
track.setBitDepth(bitDepth.get());
|
||||
}
|
||||
|
||||
// Set BitRate (optional)
|
||||
if (bitRateId != null) {
|
||||
Optional<BitRate> bitRate = bitRateRepository.findById(bitRateId);
|
||||
if (bitRate.isEmpty()) {
|
||||
throw new IllegalArgumentException("BitRate not found with id: " + bitRateId);
|
||||
}
|
||||
track.setBitRate(bitRate.get());
|
||||
}
|
||||
|
||||
// Set SamplingRate (optional)
|
||||
if (samplingRateId != null) {
|
||||
Optional<SamplingRate> samplingRate = samplingRateRepository.findById(samplingRateId);
|
||||
if (samplingRate.isEmpty()) {
|
||||
throw new IllegalArgumentException("SamplingRate not found with id: " + samplingRateId);
|
||||
}
|
||||
track.setSamplingRate(samplingRate.get());
|
||||
}
|
||||
|
||||
return repository.save(track);
|
||||
}
|
||||
|
||||
public List<Track> getAllTracks() {
|
||||
logger.info("Getting all tracks");
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
public Optional<Track> getTrackById(Integer id) {
|
||||
if (id == null) {
|
||||
throw new IllegalArgumentException("ID cannot be null");
|
||||
}
|
||||
logger.info("Getting track by id: {}", id);
|
||||
return repository.findById(id);
|
||||
}
|
||||
|
||||
public Optional<Track> updateTrack(Integer id, Integer trackNumber, String title,
|
||||
Integer duration, String filepath, Integer discId,
|
||||
Integer composerId, Integer bitDepthId,
|
||||
Integer bitRateId, Integer samplingRateId) {
|
||||
if (id == null) {
|
||||
throw new IllegalArgumentException("ID cannot be null");
|
||||
}
|
||||
if (trackNumber == null) {
|
||||
throw new IllegalArgumentException("Track number cannot be null");
|
||||
}
|
||||
if (title == null || title.isEmpty()) {
|
||||
throw new IllegalArgumentException("Title cannot be null or empty");
|
||||
}
|
||||
if (filepath == null || filepath.isEmpty()) {
|
||||
throw new IllegalArgumentException("Filepath cannot be null or empty");
|
||||
}
|
||||
if (discId == null) {
|
||||
throw new IllegalArgumentException("Disc ID cannot be null");
|
||||
}
|
||||
|
||||
logger.info("Updating track with id: {}", id);
|
||||
|
||||
Optional<Track> existingTrack = repository.findById(id);
|
||||
if (existingTrack.isEmpty()) {
|
||||
logger.warn("Track not found with id: {}", id);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
Track track = existingTrack.get();
|
||||
track.setTrackNumber(trackNumber);
|
||||
track.setTitle(title);
|
||||
track.setDuration(duration);
|
||||
track.setFilepath(filepath);
|
||||
|
||||
// Update Disc (required)
|
||||
Optional<Disc> disc = discRepository.findById(discId);
|
||||
if (disc.isEmpty()) {
|
||||
throw new IllegalArgumentException("Disc not found with id: " + discId);
|
||||
}
|
||||
track.setDisc(disc.get());
|
||||
|
||||
// Update Composer (optional)
|
||||
if (composerId != null) {
|
||||
Optional<Composer> composer = composerRepository.findById(composerId);
|
||||
if (composer.isEmpty()) {
|
||||
throw new IllegalArgumentException("Composer not found with id: " + composerId);
|
||||
}
|
||||
track.setComposer(composer.get());
|
||||
} else {
|
||||
track.setComposer(null);
|
||||
}
|
||||
|
||||
// Update BitDepth (optional)
|
||||
if (bitDepthId != null) {
|
||||
Optional<BitDepth> bitDepth = bitDepthRepository.findById(bitDepthId);
|
||||
if (bitDepth.isEmpty()) {
|
||||
throw new IllegalArgumentException("BitDepth not found with id: " + bitDepthId);
|
||||
}
|
||||
track.setBitDepth(bitDepth.get());
|
||||
} else {
|
||||
track.setBitDepth(null);
|
||||
}
|
||||
|
||||
// Update BitRate (optional)
|
||||
if (bitRateId != null) {
|
||||
Optional<BitRate> bitRate = bitRateRepository.findById(bitRateId);
|
||||
if (bitRate.isEmpty()) {
|
||||
throw new IllegalArgumentException("BitRate not found with id: " + bitRateId);
|
||||
}
|
||||
track.setBitRate(bitRate.get());
|
||||
} else {
|
||||
track.setBitRate(null);
|
||||
}
|
||||
|
||||
// Update SamplingRate (optional)
|
||||
if (samplingRateId != null) {
|
||||
Optional<SamplingRate> samplingRate = samplingRateRepository.findById(samplingRateId);
|
||||
if (samplingRate.isEmpty()) {
|
||||
throw new IllegalArgumentException("SamplingRate not found with id: " + samplingRateId);
|
||||
}
|
||||
track.setSamplingRate(samplingRate.get());
|
||||
} else {
|
||||
track.setSamplingRate(null);
|
||||
}
|
||||
|
||||
Track updatedTrack = repository.update(track);
|
||||
return Optional.of(updatedTrack);
|
||||
}
|
||||
|
||||
public boolean deleteTrack(Integer id) {
|
||||
if (id == null) {
|
||||
throw new IllegalArgumentException("Track id cannot be null");
|
||||
}
|
||||
logger.info("Deleting track: {}", id);
|
||||
return repository.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
syntax = "proto3";
|
||||
|
||||
option java_package = "com.mediamanager.protocol.messages";
|
||||
option java_outer_classname = "DiscMessages";
|
||||
|
||||
package mediamanager.messages;
|
||||
|
||||
message Disc{
|
||||
int32 id = 1;
|
||||
int32 disc_number = 2;
|
||||
int32 fk_album_id = 3;
|
||||
}
|
||||
|
||||
message CreateDiscRequest {
|
||||
int32 disc_number = 1;
|
||||
int32 fk_album_id = 2;
|
||||
}
|
||||
|
||||
message CreateDiscResponse {
|
||||
Disc disc = 1;
|
||||
}
|
||||
|
||||
message GetDiscsRequest {
|
||||
}
|
||||
|
||||
message GetDiscsResponse {
|
||||
repeated Disc discs = 1;
|
||||
}
|
||||
|
||||
message GetDiscByIdRequest {
|
||||
int32 id = 1;
|
||||
}
|
||||
|
||||
message GetDiscByIdResponse {
|
||||
Disc disc = 1;
|
||||
}
|
||||
|
||||
message UpdateDiscRequest {
|
||||
int32 id = 1;
|
||||
int32 disc_number = 2;
|
||||
int32 fk_album_id = 3;
|
||||
}
|
||||
|
||||
message UpdateDiscResponse {
|
||||
Disc disc = 1;
|
||||
}
|
||||
|
||||
message DeleteDiscRequest {
|
||||
int32 id = 1;
|
||||
}
|
||||
|
||||
message DeleteDiscResponse {
|
||||
string message = 1;
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
syntax = "proto3";
|
||||
|
||||
import "google/protobuf/wrappers.proto";
|
||||
|
||||
option java_package = "com.mediamanager.protocol.messages";
|
||||
option java_outer_classname = "TrackMessages";
|
||||
|
||||
package mediamanager.messages;
|
||||
|
||||
message Track{
|
||||
int32 id = 1;
|
||||
int32 track_number = 2;
|
||||
string title = 3;
|
||||
int32 duration = 4;
|
||||
string filepath = 5;
|
||||
|
||||
int32 fk_disc_id = 6;
|
||||
int32 fk_composer_id = 7;
|
||||
int32 fk_bitdepth_id = 8;
|
||||
int32 fk_bitrate_id = 9;
|
||||
int32 fk_samplingrate_id = 10;
|
||||
}
|
||||
|
||||
message CreateTrackRequest {
|
||||
int32 track_number = 1;
|
||||
string title = 2;
|
||||
google.protobuf.Int32Value duration = 3;
|
||||
string filepath = 4;
|
||||
|
||||
int32 fk_disc_id = 5;
|
||||
google.protobuf.Int32Value fk_composer_id = 6;
|
||||
google.protobuf.Int32Value fk_bitdepth_id = 7;
|
||||
google.protobuf.Int32Value fk_bitrate_id = 8;
|
||||
google.protobuf.Int32Value fk_samplingrate_id = 9;
|
||||
}
|
||||
|
||||
message CreateTrackResponse {
|
||||
Track track = 1;
|
||||
}
|
||||
|
||||
message GetTracksRequest {
|
||||
}
|
||||
|
||||
message GetTracksResponse {
|
||||
repeated Track tracks = 1;
|
||||
}
|
||||
|
||||
message GetTrackByIdRequest {
|
||||
int32 id = 1;
|
||||
}
|
||||
|
||||
message GetTrackByIdResponse {
|
||||
Track track = 1;
|
||||
}
|
||||
|
||||
message UpdateTrackRequest {
|
||||
int32 id = 1;
|
||||
google.protobuf.Int32Value track_number = 2;
|
||||
string title = 3;
|
||||
google.protobuf.Int32Value duration = 4;
|
||||
string filepath = 5;
|
||||
|
||||
google.protobuf.Int32Value fk_disc_id = 6;
|
||||
google.protobuf.Int32Value fk_composer_id = 7;
|
||||
google.protobuf.Int32Value fk_bitdepth_id = 8;
|
||||
google.protobuf.Int32Value fk_bitrate_id = 9;
|
||||
google.protobuf.Int32Value fk_samplingrate_id = 10;
|
||||
}
|
||||
|
||||
message UpdateTrackResponse {
|
||||
Track track = 1;
|
||||
}
|
||||
|
||||
message DeleteTrackRequest {
|
||||
int32 id = 1;
|
||||
}
|
||||
|
||||
message DeleteTrackResponse {
|
||||
bool success = 1;
|
||||
string message = 2;
|
||||
}
|
||||
Loading…
Reference in New Issue