Skip to content

Commit

Permalink
feature: Fetch credit package by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Tran Gia Bao authored and GiaBaorr committed Feb 16, 2025
1 parent a22cc2d commit 9cb00b4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ public class CreditPackageEntity extends AbstractAuditableEntity {

@Column(name = "number_of_credits", nullable = false)
@Min(0)
@NotNull
private int numberOfCredits = 0;

@Column(name = "price", nullable = false)
@Min(0)
@NotNull
private long price = 0;

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import green_buildings.commons.api.security.UserRole;
import jakarta.annotation.security.RolesAllowed;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/credit-package")
Expand All @@ -29,4 +32,13 @@ public List<CreditPackageDTO> findAll() {
.toList();
}

@GetMapping("/{id}")
@RolesAllowed({UserRole.RoleNameConstant.SYSTEM_ADMIN, UserRole.RoleNameConstant.ENTERPRISE_OWNER})
public ResponseEntity<CreditPackageDTO> findById(@PathVariable UUID id) {
CreditPackageDTO creditPackageDTO = creditPackageService.findById(id)
.map(mapper::entityToDTO)
.orElseThrow();
return ResponseEntity.ok(creditPackageDTO);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import enterprise.entities.CreditPackageEntity;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

public interface CreditPackageService {

List<CreditPackageEntity> findAll();

Optional<CreditPackageEntity> findById(UUID id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
@RequiredArgsConstructor
Expand All @@ -19,4 +21,9 @@ public class CreditPackageServiceImpl implements CreditPackageService {
public List<CreditPackageEntity> findAll() {
return creditPackageRepository.findAll();
}

@Override
public Optional<CreditPackageEntity> findById(UUID id) {
return creditPackageRepository.findById(id);
}
}

0 comments on commit 9cb00b4

Please sign in to comment.