Skip to content

Commit

Permalink
[PBS-79][HoangHN] feat: 👑 api get amenity by type
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoang-Nguyen-Huy committed Oct 17, 2024
1 parent 972fe7b commit 01c276b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -31,6 +32,14 @@ public ApiResponse<List<AmenityResponse>> getAllAmentity(){
.build();
}

@GetMapping("/type")
public ApiResponse<List<AmenityResponse>> getAmenitiesByType(@RequestParam String type){
List<AmenityResponse> amenityResponses = amenityService.getAmenitiesByType(AmenityType.valueOf(type));
return ApiResponse.<List<AmenityResponse>>builder()
.data(amenityResponses)
.build();
}

@PostMapping
ApiResponse<AmenityResponse> createAmenity(@RequestBody AmenityCreationRequest request){
return ApiResponse.<AmenityResponse>builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Amenity, Integer> {
List<Amenity> findAllByType(AmenityType type);
}
11 changes: 11 additions & 0 deletions src/main/java/com/swp/PodBookingSystem/service/AmenityService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +29,16 @@ public List<AmenityResponse> getAllAmenities(){
.collect(Collectors.toList());
}

/*
[GET]: /amenity/type?amenityType
*/
public List<AmenityResponse> getAmenitiesByType(AmenityType amenityType) {
List<Amenity> 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));
Expand Down

0 comments on commit 01c276b

Please sign in to comment.