Implement sampling rate management with full CRUD operations
This commit introduces comprehensive sampling rate management functionality to the MediaManager system, following the established patterns from BitDepth and BitRate implementations. Core Components Added: - SamplingRate entity model with JPA annotations for database persistence - SamplingRateRepository with full CRUD operations (save, findAll, findById, update, deleteById) - SamplingRateService business logic layer with validation and error handling - SamplingRateMapper for bidirectional entity-protobuf conversions Protocol Buffers: - Defined samplingrate.proto with complete message specifications - SamplingRate message structure (id, value) - Request/Response pairs for all CRUD operations (Create, GetAll, GetById, Update, Delete) - Fixed typo in UpdateSamplingRateRequest (valeu -> value) Action Handlers: - CreateSamplingRateHandler: Validates and creates new sampling rate entries - GetSamplingRateHandler: Retrieves all sampling rates from the database - GetSamplingRateByIdHandler: Fetches individual sampling rates with 404 handling - UpdateSamplingRateHandler: Updates existing sampling rates with validation - DeleteSamplingRateHandler: Removes sampling rates with success confirmation Integration: - Registered SamplingRateService in DelegateActionManager - All handlers follow the @Action annotation pattern for automatic discovery - Consistent error handling with appropriate HTTP status codes (400, 404, 500) This implementation provides a complete foundation for managing audio sampling rates within the media management system, enabling clients to perform all standard CRUD operations through the established protocol buffer interface. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
c787eba20e
commit
5aeb54516c
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.mediamanager.mapper;
|
||||||
|
|
||||||
|
import com.mediamanager.model.SamplingRate;
|
||||||
|
import com.mediamanager.protocol.messages.SamplingRateMessages;
|
||||||
|
|
||||||
|
public class SamplingRateMapper {
|
||||||
|
public static SamplingRateMessages.SamplingRate toProtobuf(SamplingRate entity) {
|
||||||
|
if (entity == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String value = entity.getValue();
|
||||||
|
if (value == null || value.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Value cannot be null or empty");
|
||||||
|
}
|
||||||
|
SamplingRateMessages.SamplingRate.Builder builder = SamplingRateMessages.SamplingRate.newBuilder()
|
||||||
|
.setValue(value);
|
||||||
|
Integer id = entity.getId();
|
||||||
|
if (id != null) {
|
||||||
|
builder.setId(id);
|
||||||
|
}
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SamplingRate toEntity(SamplingRateMessages.SamplingRate protobuf) {
|
||||||
|
if (protobuf == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
SamplingRate entity = new SamplingRate();
|
||||||
|
if (protobuf.getId() >0) {
|
||||||
|
entity.setId(protobuf.getId());
|
||||||
|
}
|
||||||
|
entity.setValue(protobuf.getValue());
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.mediamanager.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "sampling_rate")
|
||||||
|
public class SamplingRate {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,103 @@
|
||||||
|
package com.mediamanager.repository;
|
||||||
|
|
||||||
|
|
||||||
|
import com.mediamanager.model.SamplingRate;
|
||||||
|
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 SamplingRateRepository {
|
||||||
|
private static final Logger logger = LogManager.getLogger(SamplingRateRepository.class);
|
||||||
|
|
||||||
|
private final EntityManagerFactory entityManagerFactory;
|
||||||
|
|
||||||
|
public SamplingRateRepository(EntityManagerFactory entityManagerFactory) {
|
||||||
|
this.entityManagerFactory = entityManagerFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SamplingRate save(SamplingRate samplingRate) {
|
||||||
|
logger.debug("Saving SamplingRate: {}", samplingRate.getValue());
|
||||||
|
EntityManager em = entityManagerFactory.createEntityManager();
|
||||||
|
em.getTransaction().begin();
|
||||||
|
try {
|
||||||
|
em.persist(samplingRate);
|
||||||
|
em.getTransaction().commit();
|
||||||
|
logger.debug("SamplingRate has been saved successfully");
|
||||||
|
return samplingRate;
|
||||||
|
} catch (Exception e) {
|
||||||
|
em.getTransaction().rollback();
|
||||||
|
logger.error("Error while saving SamplingRate: {}", e.getMessage());
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
if (em.isOpen()) em.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SamplingRate> findAll() {
|
||||||
|
logger.debug("Finding All SamplingRate");
|
||||||
|
EntityManager em = entityManagerFactory.createEntityManager();
|
||||||
|
try{
|
||||||
|
return em.createQuery("select s from SamplingRate s", SamplingRate.class).getResultList();
|
||||||
|
}finally {
|
||||||
|
if (em.isOpen()) em.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<SamplingRate> findById(Integer id) {
|
||||||
|
logger.debug("Finding SamplingRate with id: {}", id);
|
||||||
|
EntityManager em = entityManagerFactory.createEntityManager();
|
||||||
|
try{
|
||||||
|
SamplingRate samplingRate = em.find(SamplingRate.class, id);
|
||||||
|
return Optional.of(samplingRate);
|
||||||
|
}finally {
|
||||||
|
if (em.isOpen()) em.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public SamplingRate update(SamplingRate samplingRate) {
|
||||||
|
logger.debug("Updating SamplingRate: {}", samplingRate.getValue());
|
||||||
|
EntityManager em = entityManagerFactory.createEntityManager();
|
||||||
|
em.getTransaction().begin();
|
||||||
|
try {
|
||||||
|
SamplingRate updated = em.merge(samplingRate);
|
||||||
|
em.getTransaction().commit();
|
||||||
|
logger.debug("SamplingRate has been updated successfully");
|
||||||
|
return updated;
|
||||||
|
} catch (Exception e) {
|
||||||
|
em.getTransaction().rollback();
|
||||||
|
logger.error("Error while updating SamplingRate: {}", e.getMessage());
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
if (em.isOpen()) em.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean deleteById(Integer id){
|
||||||
|
logger.debug("Deleting SamplingRate with id: {}", id);
|
||||||
|
EntityManager em = entityManagerFactory.createEntityManager();
|
||||||
|
em.getTransaction().begin();
|
||||||
|
try{
|
||||||
|
SamplingRate samplingRate = em.find(SamplingRate.class, id);
|
||||||
|
if (samplingRate == null) {
|
||||||
|
em.getTransaction().rollback();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
em.remove(samplingRate);
|
||||||
|
em.getTransaction().commit();
|
||||||
|
logger.debug("SamplingRate has been deleted successfully");
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
em.getTransaction().rollback();
|
||||||
|
logger.error("Error while deleting SamplingRate: {}", e.getMessage());
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
if (em.isOpen()) em.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -11,6 +11,7 @@ import com.mediamanager.service.artist.ArtistService;
|
||||||
import com.mediamanager.service.delegate.annotation.Action;
|
import com.mediamanager.service.delegate.annotation.Action;
|
||||||
|
|
||||||
import com.mediamanager.service.genre.GenreService;
|
import com.mediamanager.service.genre.GenreService;
|
||||||
|
import com.mediamanager.service.samplingrate.SamplingRateService;
|
||||||
import jakarta.persistence.EntityManagerFactory;
|
import jakarta.persistence.EntityManagerFactory;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
@ -70,6 +71,10 @@ public class DelegateActionManager {
|
||||||
BitRateService bitRateService = new BitRateService(bitRateRepository);
|
BitRateService bitRateService = new BitRateService(bitRateRepository);
|
||||||
serviceLocator.register(BitRateService.class, bitRateService);
|
serviceLocator.register(BitRateService.class, bitRateService);
|
||||||
|
|
||||||
|
SamplingRateRepository samplingRateRepository = new SamplingRateRepository(entityManagerFactory);
|
||||||
|
SamplingRateService samplingRateService = new SamplingRateService(samplingRateRepository);
|
||||||
|
serviceLocator.register(SamplingRateService.class, samplingRateService);
|
||||||
|
|
||||||
serviceLocator.logRegisteredServices();
|
serviceLocator.logRegisteredServices();
|
||||||
|
|
||||||
logger.info("Services initialized successfully");
|
logger.info("Services initialized successfully");
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,50 @@
|
||||||
|
package com.mediamanager.service.delegate.handler.samplingrate;
|
||||||
|
|
||||||
|
import com.google.protobuf.ByteString;
|
||||||
|
import com.google.protobuf.InvalidProtocolBufferException;
|
||||||
|
import com.mediamanager.mapper.SamplingRateMapper;
|
||||||
|
import com.mediamanager.model.SamplingRate;
|
||||||
|
import com.mediamanager.protocol.TransportProtocol;
|
||||||
|
import com.mediamanager.protocol.messages.SamplingRateMessages;
|
||||||
|
import com.mediamanager.service.delegate.ActionHandler;
|
||||||
|
import com.mediamanager.service.delegate.annotation.Action;
|
||||||
|
import com.mediamanager.service.samplingrate.SamplingRateService;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
@Action("samplingrate.create")
|
||||||
|
public class CreateSamplingRateHandler implements ActionHandler {
|
||||||
|
private static final Logger logger = LogManager.getLogger(CreateSamplingRateHandler.class);
|
||||||
|
private final SamplingRateService samplingRateService;
|
||||||
|
|
||||||
|
public CreateSamplingRateHandler(SamplingRateService samplingRateService) {
|
||||||
|
this.samplingRateService = samplingRateService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TransportProtocol.Response.Builder handle(ByteString requestPayload) throws InvalidProtocolBufferException {
|
||||||
|
try{
|
||||||
|
SamplingRateMessages.CreateSamplingRateRequest createRequest =
|
||||||
|
SamplingRateMessages.CreateSamplingRateRequest.parseFrom(requestPayload);
|
||||||
|
SamplingRate samplingRate = samplingRateService.createSamplingRate(createRequest.getValue());
|
||||||
|
SamplingRateMessages.SamplingRate samplingRateProto = SamplingRateMapper.toProtobuf(samplingRate);
|
||||||
|
SamplingRateMessages.CreateSamplingRateResponse createSamplingRateResponse = SamplingRateMessages.CreateSamplingRateResponse.newBuilder()
|
||||||
|
.setSamplingrate(samplingRateProto)
|
||||||
|
.build();
|
||||||
|
return TransportProtocol.Response.newBuilder()
|
||||||
|
.setPayload(createSamplingRateResponse.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 sampling rate", e);
|
||||||
|
return TransportProtocol.Response.newBuilder()
|
||||||
|
.setStatusCode(500)
|
||||||
|
.setPayload(ByteString.copyFromUtf8("Error: " + e.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,62 @@
|
||||||
|
package com.mediamanager.service.delegate.handler.samplingrate;
|
||||||
|
|
||||||
|
import com.google.protobuf.ByteString;
|
||||||
|
import com.google.protobuf.InvalidProtocolBufferException;
|
||||||
|
import com.mediamanager.protocol.TransportProtocol;
|
||||||
|
import com.mediamanager.protocol.messages.SamplingRateMessages;
|
||||||
|
import com.mediamanager.service.delegate.ActionHandler;
|
||||||
|
import com.mediamanager.service.delegate.annotation.Action;
|
||||||
|
import com.mediamanager.service.samplingrate.SamplingRateService;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
@Action("samplingrate.delete")
|
||||||
|
public class DeleteSamplingRateHandler implements ActionHandler {
|
||||||
|
private static final Logger logger = LogManager.getLogger(DeleteSamplingRateHandler.class);
|
||||||
|
|
||||||
|
private final SamplingRateService samplingRateService;
|
||||||
|
|
||||||
|
public DeleteSamplingRateHandler(SamplingRateService samplingRateService) {
|
||||||
|
this.samplingRateService = samplingRateService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TransportProtocol.Response.Builder handle(ByteString requestPayload)
|
||||||
|
throws InvalidProtocolBufferException {
|
||||||
|
|
||||||
|
try {
|
||||||
|
SamplingRateMessages.DeleteSamplingRateRequest deleteRequest =
|
||||||
|
SamplingRateMessages.DeleteSamplingRateRequest.parseFrom(requestPayload);
|
||||||
|
int id = deleteRequest.getId();
|
||||||
|
boolean success = samplingRateService.deleteSamplingRate(id);
|
||||||
|
SamplingRateMessages.DeleteSamplingRateResponse deleteResponse;
|
||||||
|
if (success) {
|
||||||
|
deleteResponse = SamplingRateMessages.DeleteSamplingRateResponse.newBuilder()
|
||||||
|
.setSuccess(true)
|
||||||
|
.setMessage("Sampling rate deleted successfully")
|
||||||
|
.build();
|
||||||
|
return TransportProtocol.Response.newBuilder()
|
||||||
|
.setPayload(deleteResponse.toByteString());
|
||||||
|
} else {
|
||||||
|
deleteResponse = SamplingRateMessages.DeleteSamplingRateResponse.newBuilder()
|
||||||
|
.setSuccess(false)
|
||||||
|
.setMessage("Sampling rate not found")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return TransportProtocol.Response.newBuilder()
|
||||||
|
.setStatusCode(404)
|
||||||
|
.setPayload(deleteResponse.toByteString());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error deleting sampling rate", e);
|
||||||
|
SamplingRateMessages.DeleteSamplingRateResponse deleteResponse =
|
||||||
|
SamplingRateMessages.DeleteSamplingRateResponse.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.samplingrate;
|
||||||
|
|
||||||
|
import com.google.protobuf.ByteString;
|
||||||
|
import com.google.protobuf.InvalidProtocolBufferException;
|
||||||
|
import com.mediamanager.mapper.SamplingRateMapper;
|
||||||
|
import com.mediamanager.model.SamplingRate;
|
||||||
|
import com.mediamanager.protocol.TransportProtocol;
|
||||||
|
import com.mediamanager.protocol.messages.SamplingRateMessages;
|
||||||
|
import com.mediamanager.service.delegate.ActionHandler;
|
||||||
|
import com.mediamanager.service.delegate.annotation.Action;
|
||||||
|
import com.mediamanager.service.samplingrate.SamplingRateService;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Action(value = "samplingrate.getById")
|
||||||
|
public class GetSamplingRateByIdHandler implements ActionHandler {
|
||||||
|
private static final Logger logger = LogManager.getLogger(GetSamplingRateByIdHandler.class);
|
||||||
|
private final SamplingRateService samplingRateService;
|
||||||
|
|
||||||
|
public GetSamplingRateByIdHandler(SamplingRateService samplingRateService) {
|
||||||
|
this.samplingRateService = samplingRateService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TransportProtocol.Response.Builder handle(ByteString requestPayload)
|
||||||
|
throws InvalidProtocolBufferException{
|
||||||
|
|
||||||
|
try{
|
||||||
|
SamplingRateMessages.GetSamplingRateByIDRequest getByIdRequest =
|
||||||
|
SamplingRateMessages.GetSamplingRateByIDRequest.parseFrom(requestPayload);
|
||||||
|
int id = getByIdRequest.getId();
|
||||||
|
|
||||||
|
Optional<SamplingRate> samplingRateOpt = samplingRateService.getSamplingRateById(id);
|
||||||
|
|
||||||
|
if (samplingRateOpt.isEmpty()){
|
||||||
|
logger.warn("SamplingRate not found with ID: {}", id);
|
||||||
|
return TransportProtocol.Response.newBuilder()
|
||||||
|
.setStatusCode(404)
|
||||||
|
.setPayload(ByteString.copyFromUtf8("SamplingRate not found"));
|
||||||
|
}
|
||||||
|
SamplingRateMessages.SamplingRate samplingRateProto = SamplingRateMapper.toProtobuf(samplingRateOpt.get());
|
||||||
|
SamplingRateMessages.GetSamplingRateByIdResponse getByIdResponse = SamplingRateMessages.GetSamplingRateByIdResponse.newBuilder()
|
||||||
|
.setSamplingrate(samplingRateProto)
|
||||||
|
.build();
|
||||||
|
return TransportProtocol.Response.newBuilder()
|
||||||
|
.setPayload(getByIdResponse.toByteString());
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error getting sampling rate by ID", e);
|
||||||
|
return TransportProtocol.Response.newBuilder()
|
||||||
|
.setStatusCode(500)
|
||||||
|
.setPayload(ByteString.copyFromUtf8("Error: "+ e.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
package com.mediamanager.service.delegate.handler.samplingrate;
|
||||||
|
|
||||||
|
import com.google.protobuf.ByteString;
|
||||||
|
import com.google.protobuf.InvalidProtocolBufferException;
|
||||||
|
import com.mediamanager.mapper.SamplingRateMapper;
|
||||||
|
import com.mediamanager.model.SamplingRate;
|
||||||
|
import com.mediamanager.protocol.TransportProtocol;
|
||||||
|
import com.mediamanager.protocol.messages.SamplingRateMessages;
|
||||||
|
import com.mediamanager.service.delegate.ActionHandler;
|
||||||
|
import com.mediamanager.service.delegate.annotation.Action;
|
||||||
|
import com.mediamanager.service.samplingrate.SamplingRateService;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
@Action("samplingrate.getAll")
|
||||||
|
public class GetSamplingRateHandler implements ActionHandler {
|
||||||
|
private static final Logger logger = LogManager.getLogger(GetSamplingRateHandler.class);
|
||||||
|
|
||||||
|
private final SamplingRateService samplingRateService;
|
||||||
|
|
||||||
|
public GetSamplingRateHandler(SamplingRateService samplingRateService){this.samplingRateService = samplingRateService;}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TransportProtocol.Response.Builder handle(ByteString requestPayload) throws InvalidProtocolBufferException {
|
||||||
|
try{
|
||||||
|
List<SamplingRate> samplingRates = samplingRateService.getAllSamplingRates();
|
||||||
|
SamplingRateMessages.GetSamplingRatesResponse.Builder responseBuilder = SamplingRateMessages.GetSamplingRatesResponse.newBuilder();
|
||||||
|
|
||||||
|
for (SamplingRate samplingRate : samplingRates) {
|
||||||
|
SamplingRateMessages.SamplingRate samplingRateProto = SamplingRateMapper.toProtobuf(samplingRate);
|
||||||
|
responseBuilder.addSamplingrates(samplingRateProto);
|
||||||
|
}
|
||||||
|
SamplingRateMessages.GetSamplingRatesResponse getSamplingRatesResponse = responseBuilder.build();
|
||||||
|
|
||||||
|
return TransportProtocol.Response.newBuilder()
|
||||||
|
.setPayload(getSamplingRatesResponse.toByteString());
|
||||||
|
|
||||||
|
}catch (Exception e){
|
||||||
|
logger.error("Error getting sampling rates", e);
|
||||||
|
return TransportProtocol.Response.newBuilder()
|
||||||
|
.setStatusCode(500)
|
||||||
|
.setPayload(ByteString.copyFromUtf8("Error: " + e.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,65 @@
|
||||||
|
package com.mediamanager.service.delegate.handler.samplingrate;
|
||||||
|
|
||||||
|
import com.google.protobuf.ByteString;
|
||||||
|
import com.google.protobuf.InvalidProtocolBufferException;
|
||||||
|
import com.mediamanager.mapper.SamplingRateMapper;
|
||||||
|
import com.mediamanager.model.SamplingRate;
|
||||||
|
import com.mediamanager.protocol.TransportProtocol;
|
||||||
|
import com.mediamanager.protocol.messages.SamplingRateMessages;
|
||||||
|
import com.mediamanager.service.delegate.ActionHandler;
|
||||||
|
import com.mediamanager.service.delegate.annotation.Action;
|
||||||
|
import com.mediamanager.service.samplingrate.SamplingRateService;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Action("samplingrate.update")
|
||||||
|
public class UpdateSamplingRateHandler implements ActionHandler {
|
||||||
|
private static final Logger logger = LogManager.getLogger(UpdateSamplingRateHandler.class);
|
||||||
|
private final SamplingRateService samplingRateService;
|
||||||
|
|
||||||
|
public UpdateSamplingRateHandler(SamplingRateService samplingRateService) {
|
||||||
|
this.samplingRateService = samplingRateService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TransportProtocol.Response.Builder handle(ByteString requestPayload) throws InvalidProtocolBufferException {
|
||||||
|
try{
|
||||||
|
SamplingRateMessages.UpdateSamplingRateRequest updateRequest =
|
||||||
|
SamplingRateMessages.UpdateSamplingRateRequest.parseFrom(requestPayload);
|
||||||
|
|
||||||
|
int id = updateRequest.getId();
|
||||||
|
String newValue = updateRequest.getValue();
|
||||||
|
|
||||||
|
Optional<SamplingRate> samplingRateOpt = samplingRateService.updateSamplingRate(id, newValue);
|
||||||
|
|
||||||
|
if(samplingRateOpt.isEmpty()){
|
||||||
|
logger.warn("SamplingRate not found with ID: {}", id);
|
||||||
|
return TransportProtocol.Response.newBuilder()
|
||||||
|
.setStatusCode(404)
|
||||||
|
.setPayload(ByteString.copyFromUtf8("SamplingRate not found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
SamplingRateMessages.SamplingRate samplingRateProto = SamplingRateMapper.toProtobuf(samplingRateOpt.get());
|
||||||
|
|
||||||
|
SamplingRateMessages.UpdateSamplingRateResponse updateResponse = SamplingRateMessages.UpdateSamplingRateResponse.newBuilder()
|
||||||
|
.setSamplingrate(samplingRateProto)
|
||||||
|
.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 sampling rate", e);
|
||||||
|
return TransportProtocol.Response.newBuilder()
|
||||||
|
.setStatusCode(500)
|
||||||
|
.setPayload(ByteString.copyFromUtf8("Error: " + e.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,59 @@
|
||||||
|
package com.mediamanager.service.samplingrate;
|
||||||
|
|
||||||
|
import com.mediamanager.model.SamplingRate;
|
||||||
|
import com.mediamanager.repository.SamplingRateRepository;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class SamplingRateService {
|
||||||
|
private static final Logger logger = LogManager.getLogger(SamplingRateService.class);
|
||||||
|
private final SamplingRateRepository repository;
|
||||||
|
|
||||||
|
public SamplingRateService(SamplingRateRepository repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SamplingRate createSamplingRate(String value) {
|
||||||
|
logger.debug("Creating sampling rate:{}", value);
|
||||||
|
if (value == null || value.trim().isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Sampling-Rate value cannot be null or empty");
|
||||||
|
}
|
||||||
|
SamplingRate samplingRate = new SamplingRate();
|
||||||
|
samplingRate.setValue(value);
|
||||||
|
return repository.save(samplingRate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SamplingRate> getAllSamplingRates() {
|
||||||
|
logger.info("Getting all sampling rates");
|
||||||
|
return repository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<SamplingRate> getSamplingRateById(Integer id) {
|
||||||
|
logger.info("Getting sampling rate by id:{}", id);
|
||||||
|
return repository.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<SamplingRate> updateSamplingRate(Integer id, String value) {
|
||||||
|
logger.info("Updating sampling rate:{}", value);
|
||||||
|
if (value == null || value.trim().isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Sampling-Rate value cannot be null or empty");}
|
||||||
|
Optional<SamplingRate> existingSamplingRate = repository.findById(id);
|
||||||
|
if(existingSamplingRate.isEmpty()) {
|
||||||
|
logger.warn("Sampling rate not found with id:{}", id);
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
SamplingRate samplingRate = existingSamplingRate.get();
|
||||||
|
samplingRate.setValue(value);
|
||||||
|
SamplingRate updatedSamplingRate = repository.update(samplingRate);
|
||||||
|
return Optional.of(updatedSamplingRate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean deleteSamplingRate(Integer id) {
|
||||||
|
logger.info("Deleting sampling rate:{}", id);
|
||||||
|
return repository.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
option java_package = "com.mediamanager.protocol.messages";
|
||||||
|
option java_outer_classname = "SamplingRateMessages";
|
||||||
|
|
||||||
|
package mediamanager.messages;
|
||||||
|
|
||||||
|
message SamplingRate{
|
||||||
|
int32 id=1;
|
||||||
|
string value=2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message CreateSamplingRateRequest{
|
||||||
|
string value= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message CreateSamplingRateResponse{
|
||||||
|
SamplingRate samplingrate =1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GetSamplingRatesRequest{}
|
||||||
|
|
||||||
|
message GetSamplingRatesResponse{
|
||||||
|
repeated SamplingRate samplingrates = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GetSamplingRateByIDRequest{
|
||||||
|
int32 id =1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GetSamplingRateByIdResponse{
|
||||||
|
SamplingRate samplingrate = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message UpdateSamplingRateRequest{
|
||||||
|
int32 id = 1;
|
||||||
|
string value = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message UpdateSamplingRateResponse{
|
||||||
|
SamplingRate samplingrate = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message DeleteSamplingRateRequest{
|
||||||
|
int32 id =1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message DeleteSamplingRateResponse{
|
||||||
|
bool success = 1;
|
||||||
|
string message =2;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue