Skip to content
Snippets Groups Projects
Commit 6af75d29 authored by Anders Montsko Austlid's avatar Anders Montsko Austlid
Browse files

Merge branch 'feature/wastecontroller-groupid-endpoint' into 'main'

Added WasteController endpoint to get waste by groupId

Closes #127

See merge request idatt2106-v23-03/backend!95
parents 1fb067c3 68e73949
No related branches found
No related tags found
No related merge requests found
...@@ -33,10 +33,23 @@ public class WasteController { ...@@ -33,10 +33,23 @@ public class WasteController {
* @param wasteId the id of the waste * @param wasteId the id of the waste
* @return a ResponseEntity containing the waste if it exists, or a 404 if it doesn't * @return a ResponseEntity containing the waste if it exists, or a 404 if it doesn't
*/ */
@GetMapping("/waste/{wasteId}") @GetMapping("/{wasteId}")
public ResponseEntity<Waste> getWasteById(@PathVariable("wasteId") long wasteId) { public ResponseEntity<Waste> getWasteById(@PathVariable("wasteId") long wasteId) {
return wasteService.getWasteById(wasteId) return wasteService.getWasteById(wasteId)
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build()); .orElseGet(() -> ResponseEntity.notFound().build());
} }
/**
* Gets a waste by its group id
*
* @param groupId the id of the group
* @return a ResponseEntity containing the waste if it exists, or a 404 if it doesn't
*/
@GetMapping("/group/{groupId}")
public ResponseEntity<Waste> getWasteByGroupId(@PathVariable("groupId") long groupId) {
return wasteService.getWasteByGroupId(groupId)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}
} }
...@@ -3,6 +3,8 @@ package ntnu.idatt2016.v233.SmartMat.repository.group; ...@@ -3,6 +3,8 @@ package ntnu.idatt2016.v233.SmartMat.repository.group;
import ntnu.idatt2016.v233.SmartMat.entity.Waste; import ntnu.idatt2016.v233.SmartMat.entity.Waste;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
public interface WasteRepository extends JpaRepository<Waste, Long> { import java.util.Optional;
public interface WasteRepository extends JpaRepository<Waste, Long> {
Optional<Waste> findByGroupId(long groupId);
} }
...@@ -30,4 +30,14 @@ public class WasteService { ...@@ -30,4 +30,14 @@ public class WasteService {
public Optional<Waste> getWasteById(long id) { public Optional<Waste> getWasteById(long id) {
return wasteRepository.findById(id); return wasteRepository.findById(id);
} }
/**
* Gets a waste by its group id
*
* @param groupId the id of the group
* @return an optional containing the waste if it exists
*/
public Optional<Waste> getWasteByGroupId(long groupId) {
return wasteRepository.findByGroupId(groupId);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment