Skip to content

Commit

Permalink
Merge pull request #27 from Lokesh-Bisht/feature/api_updateBrandById
Browse files Browse the repository at this point in the history
feat(api): #23 Add api to update a brand
  • Loading branch information
Lokesh-Bisht committed Apr 19, 2023
2 parents ac31568 + 5a378dc commit 60df1b9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ public Brand createBrand(@Valid @RequestBody BrandDto brandDto) {
public Optional<Brand> getBrand(@PathVariable("id") String brandId) {
return brandService.getBrand(brandId);
}

@Operation(summary = "updateBrand")
@PutMapping("/{id}")
public Brand updateBrand(@PathVariable("id") String brandId, @Valid @RequestBody BrandDto brandDto) {
return brandService.updateBrand(brandId, brandDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public interface BrandService {

Brand createBrand(BrandDto brandDto);
Optional<Brand> getBrand(String brandId);
Brand updateBrand(String brandId, BrandDto brandDto);
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,29 @@ public Optional<Brand> getBrand(String brandId) {
logger.info("Found brand: {}", brand);
return brand;
}

@Override
public Brand updateBrand(String brandId, BrandDto brandDto) {
logger.info("Update brand: {} with id: {}", brandDto, brandId);
Optional<Brand> brand = brandRepository.findByBrandId(Integer.parseInt(brandId));
if (brand.isEmpty()) {
logger.error("Brand with id {} is not found.", brandId);
throw new BrandNotFoundException("Brand not found!");
}
try {
Brand updatedBrand = objectMapper.readValue(objectMapper.writeValueAsString(brandDto), Brand.class);
updatedBrand.setId(brand.get().getId());
updatedBrand.setCreatedAt(brand.get().getCreatedAt());
updatedBrand.setCreatedBy(brand.get().getCreatedBy());
updatedBrand.setUpdatedAt(Instant.now().getEpochSecond());
logger.info("Successfully updated brand.");
return brandRepository.save(updatedBrand);
} catch (JsonMappingException e) {
logger.error("Error occurred during updating document: {}", brandDto);
throw new JsonRuntimeException("Json Mapping exception encountered during object to string conversion", e);
} catch (JsonProcessingException e) {
logger.error("Error occurred during updating document: {}", brandDto);
throw new JsonRuntimeException("Json Processing exception encountered during object to string conversion", e);
}
}
}

0 comments on commit 60df1b9

Please sign in to comment.