diff --git a/src/main/java/com/swp/PodBookingSystem/controller/AmenityController.java b/src/main/java/com/swp/PodBookingSystem/controller/AmenityController.java index 3229dd59..46ac0cbc 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/AmenityController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/AmenityController.java @@ -3,6 +3,7 @@ import com.swp.PodBookingSystem.dto.request.Amenity.AmenityCreationRequest; import com.swp.PodBookingSystem.dto.respone.AmenityResponse; import com.swp.PodBookingSystem.dto.respone.ApiResponse; +import com.swp.PodBookingSystem.enums.AmenityType; import com.swp.PodBookingSystem.service.AmenityService; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; @@ -31,6 +32,14 @@ public ApiResponse> getAllAmentity(){ .build(); } + @GetMapping("/type") + public ApiResponse> getAmenitiesByType(@RequestParam String type){ + List amenityResponses = amenityService.getAmenitiesByType(AmenityType.valueOf(type)); + return ApiResponse.>builder() + .data(amenityResponses) + .build(); + } + @PostMapping ApiResponse createAmenity(@RequestBody AmenityCreationRequest request){ return ApiResponse.builder() diff --git a/src/main/java/com/swp/PodBookingSystem/repository/AmenityRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/AmenityRepository.java index 55016d8d..df716f83 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/AmenityRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/AmenityRepository.java @@ -1,9 +1,13 @@ package com.swp.PodBookingSystem.repository; import com.swp.PodBookingSystem.entity.Amenity; +import com.swp.PodBookingSystem.enums.AmenityType; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface AmenityRepository extends JpaRepository { + List findAllByType(AmenityType type); } diff --git a/src/main/java/com/swp/PodBookingSystem/service/AmenityService.java b/src/main/java/com/swp/PodBookingSystem/service/AmenityService.java index 3eab4b32..1c4a02e5 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/AmenityService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/AmenityService.java @@ -3,6 +3,7 @@ import com.swp.PodBookingSystem.dto.request.Amenity.AmenityCreationRequest; import com.swp.PodBookingSystem.dto.respone.AmenityResponse; import com.swp.PodBookingSystem.entity.Amenity; +import com.swp.PodBookingSystem.enums.AmenityType; import com.swp.PodBookingSystem.mapper.AmenityMapper; import com.swp.PodBookingSystem.repository.AmenityRepository; import jakarta.persistence.EntityNotFoundException; @@ -28,6 +29,16 @@ public List getAllAmenities(){ .collect(Collectors.toList()); } + /* + [GET]: /amenity/type?amenityType + */ + public List getAmenitiesByType(AmenityType amenityType) { + List amenities = amenityRepository.findAllByType(amenityType); + return amenities.stream() + .map(amenityMapper::toAmenityResponse) + .collect(Collectors.toList()); + } + public AmenityResponse createAmenity(AmenityCreationRequest request){ Amenity newAmenity = amenityMapper.toAmenity(request); return amenityMapper.toAmenityResponse(amenityRepository.save(newAmenity));