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();
try{
SamplingRate samplingRate = em.find(SamplingRate.class, id);
return Optional.of(samplingRate);
return Optional.ofNullable(samplingRate);
}finally {
if (em.isOpen()) em.close();
}

View File

@ -32,14 +32,21 @@ public class SamplingRateService {
}
public Optional<SamplingRate> getSamplingRateById(Integer id) {
if (id == null) {
throw new IllegalArgumentException("ID cannot be null");
}
logger.info("Getting sampling rate by id:{}", id);
return repository.findById(id);
}
public Optional<SamplingRate> updateSamplingRate(Integer id, String value) {
if (id == null) {
throw new IllegalArgumentException("ID cannot be null");
}
logger.info("Updating sampling rate:{}", value);
if (value == null || value.trim().isEmpty()) {
throw new IllegalArgumentException("Sampling-Rate value cannot be null or empty");}
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);
@ -52,6 +59,9 @@ public class SamplingRateService {
}
public boolean deleteSamplingRate(Integer id) {
if (id == null) {
throw new IllegalArgumentException("Sampling rate id cannot be null");
}
logger.info("Deleting sampling rate:{}", id);
return repository.deleteById(id);
}