Compare commits

...

3 Commits

Author SHA1 Message Date
Gustavo Henrique Santos Souza de Miranda 94297e75b9 Add id validation and improve code formatting in updateSamplingRate
Enhance input validation and code quality in the SamplingRate service:

- Add null check for id parameter in updateSamplingRate() method to prevent
  potential NullPointerException when calling repository.findById()
- Standardize indentation throughout the updateSamplingRate() method body,
  improving code readability and consistency with project style guidelines

This complements the previous validation improvements by ensuring all
method parameters are properly validated before use, creating a more
defensive and robust API surface.

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-06 22:30:51 -03:00
Gustavo Henrique Santos Souza de Miranda ff09d1b89a Fix null handling and validation in SamplingRate management
This commit addresses potential runtime exceptions and improves input validation:

- Replace Optional.of() with Optional.ofNullable() in SamplingRateRepository.findById()
  to properly handle cases where no sampling rate is found, preventing NullPointerException
- Add null validation for id parameter in SamplingRateService.deleteSamplingRate()
  to ensure proper error handling before repository operations
- Clean up code formatting in updateSamplingRate() validation block

These changes enhance the robustness of the sampling rate management feature
by preventing NPEs and providing clearer error messages for invalid inputs.

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-06 22:18:57 -03:00
Gustavo Henrique Miranda 4fa147282f
Update src/main/java/com/mediamanager/service/samplingrate/SamplingRateService.java
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-12-06 22:07:39 -03:00
2 changed files with 23 additions and 13 deletions

View File

@ -53,7 +53,7 @@ public class SamplingRateRepository {
EntityManager em = entityManagerFactory.createEntityManager(); EntityManager em = entityManagerFactory.createEntityManager();
try{ try{
SamplingRate samplingRate = em.find(SamplingRate.class, id); SamplingRate samplingRate = em.find(SamplingRate.class, id);
return Optional.of(samplingRate); return Optional.ofNullable(samplingRate);
}finally { }finally {
if (em.isOpen()) em.close(); if (em.isOpen()) em.close();
} }

View File

@ -32,26 +32,36 @@ public class SamplingRateService {
} }
public Optional<SamplingRate> getSamplingRateById(Integer id) { public Optional<SamplingRate> getSamplingRateById(Integer id) {
if (id == null) {
throw new IllegalArgumentException("ID cannot be null");
}
logger.info("Getting sampling rate by id:{}", id); logger.info("Getting sampling rate by id:{}", id);
return repository.findById(id); return repository.findById(id);
} }
public Optional<SamplingRate> updateSamplingRate(Integer id, String value) { public Optional<SamplingRate> updateSamplingRate(Integer id, String value) {
logger.info("Updating sampling rate:{}", value); if (id == null) {
if (value == null || value.trim().isEmpty()) { throw new IllegalArgumentException("ID cannot be null");
throw new IllegalArgumentException("Sampling-Rate value cannot be null or empty");} }
Optional<SamplingRate> existingSamplingRate = repository.findById(id); logger.info("Updating sampling rate:{}", value);
if(existingSamplingRate.isEmpty()) { if (value == null || value.trim().isEmpty()) {
logger.warn("Sampling rate not found with id:{}", id); throw new IllegalArgumentException("Sampling-Rate value cannot be null or empty");
return Optional.empty(); }
} Optional<SamplingRate> existingSamplingRate = repository.findById(id);
SamplingRate samplingRate = existingSamplingRate.get(); if(existingSamplingRate.isEmpty()) {
samplingRate.setValue(value); logger.warn("Sampling rate not found with id:{}", id);
SamplingRate updatedSamplingRate = repository.update(samplingRate); return Optional.empty();
return Optional.of(updatedSamplingRate); }
SamplingRate samplingRate = existingSamplingRate.get();
samplingRate.setValue(value);
SamplingRate updatedSamplingRate = repository.update(samplingRate);
return Optional.of(updatedSamplingRate);
} }
public boolean deleteSamplingRate(Integer id) { public boolean deleteSamplingRate(Integer id) {
if (id == null) {
throw new IllegalArgumentException("Sampling rate id cannot be null");
}
logger.info("Deleting sampling rate:{}", id); logger.info("Deleting sampling rate:{}", id);
return repository.deleteById(id); return repository.deleteById(id);
} }