Skip to content
Snippets Groups Projects
Commit 9b906ee0 authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

The endpoint to get the wastes based on category and group id was added

parent bb821c5f
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@ import java.util.List;
import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.Waste;
import ntnu.idatt2016.v233.SmartMat.service.group.WasteService;
import ntnu.idatt2016.v233.SmartMat.util.CategoryUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
......@@ -53,4 +54,19 @@ public class WasteController {
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}
/**
* Retrieves a list of Waste objects of a specific category from a group
*
* @param groupId the ID of the group to search for
* @param categoryNumber the number representing the category to search for
* @return a ResponseEntity containing a list of Waste objects if successful, or a not found ResponseEntity if the data is not found
*/
@GetMapping("/group/{groupId}/category/{categoryNumber}")
public ResponseEntity<List<Waste>> getWasteOfCategoryByGroupId(@PathVariable("groupId") long groupId,
@PathVariable("categoryNumber") int categoryNumber){
return wasteService.getWasteOfCategoryByGroupId(groupId, CategoryUtil.getCategoryName(categoryNumber)).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
}
......@@ -3,9 +3,25 @@ package ntnu.idatt2016.v233.SmartMat.repository.group;
import java.util.List;
import ntnu.idatt2016.v233.SmartMat.entity.Waste;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.Optional;
public interface WasteRepository extends JpaRepository<Waste, Long> {
Optional<List<Waste>> findByGroupId(long groupId);
/**
* Returns a list of all waste items of a given category from a specific group.
*
* @param groupId the ID of the group
* @param categoryName the name of the category
* @return an Optional containing a List of Waste objects if at least one waste item is found,
* or an empty Optional if no waste items are found
*/
@Query(value = "SELECT * FROM wastes WHERE group_id = :groupId AND ean IN (SELECT ean FROM product WHERE category_name = :categoryName)", nativeQuery = true)
Optional<List<Waste>> findAllWasteOfOneCategoryFromGroup(@Param("groupId") long groupId,
@Param("categoryName") String categoryName);
}
......@@ -41,4 +41,17 @@ public class WasteService {
public Optional<List<Waste>> getWasteByGroupId(long groupId) {
return wasteRepository.findByGroupId(groupId);
}
/**
* Returns an Optional containing a List of Waste objects for a specific category and group id,
* or an empty Optional if there are no wastes for that category and group id.
*
* @param groupId the ID of the group to search in
* @param categoryName the name of the category to search for
* @return an Optional containing a List of Waste objects, or an empty Optional if there are no wastes for that category and group id
*/
public Optional<List<Waste>> getWasteOfCategoryByGroupId(long groupId, String categoryName){
return wasteRepository.findAllWasteOfOneCategoryFromGroup(groupId,categoryName);
}
}
......@@ -31,4 +31,22 @@ public class CategoryUtil {
if(Arrays.stream(FRUIT_AND_VEGETABLES).anyMatch(response::contains)) return "fruit and vegetables";
return "other";
}
/**
* Returns the category name corresponding to the given category number.
*
* @param categoryNumber the category number
* @return the category name
* @throws IllegalArgumentException if the given category number is invalid
*/
public static String getCategoryName(int categoryNumber) {
return switch (categoryNumber) {
case 1 -> "meat, fish and chicken";
case 2 -> "baked goods and grains";
case 3 -> "dairy and egg";
case 4 -> "other";
case 5 -> "fruit and vegetables";
default -> throw new IllegalArgumentException("Invalid category number: " + categoryNumber);
};
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment