diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/AllergyController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/AllergyController.java new file mode 100644 index 0000000000000000000000000000000000000000..a8cbdf8c0b1d5c47161f586ccb3a5b2e5f5b1e08 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/AllergyController.java @@ -0,0 +1,55 @@ +package ntnu.idatt2016.v233.SmartMat.controller; + +import lombok.AllArgsConstructor; +import ntnu.idatt2016.v233.SmartMat.dto.response.AllergyResponse; +import ntnu.idatt2016.v233.SmartMat.entity.product.Allergy; +import ntnu.idatt2016.v233.SmartMat.service.AllergyService; + +import java.util.List; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +/** + * Controller for allergies + * @author Stian Lyng + * @version 1.0 + */ +@AllArgsConstructor +@RestController +@RequestMapping("/api/allergies") +public class AllergyController { + + /** + * Service for allergies + */ + private final AllergyService allergyService; + + /** + * Gets allergy by name, also includes products that contain the allergy + * + * @param name Name of allergy + * @return Allergies including products that contain the allergy + */ + @GetMapping("/id/{name}") + public ResponseEntity<Allergy> getAllergyByName(@PathVariable ("name") String name) { + return allergyService.getAllergyByName(name) + .map(ResponseEntity::ok) + .orElseGet(() -> ResponseEntity.notFound().build()); + } + + /** + * Gets list of all allergies, without products + * + * @return List of allergies, without products that contain the allergy + */ + @GetMapping("/all") + public ResponseEntity<List<AllergyResponse>> getAllAllergies() { + List<AllergyResponse> allergies = allergyService.getAllAllergies(); + if (allergies.isEmpty()) { + return ResponseEntity.notFound().build(); + } + return ResponseEntity.ok(allergies); + } + +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/response/AllergyResponse.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/response/AllergyResponse.java new file mode 100644 index 0000000000000000000000000000000000000000..66b2f728ae93a85bdf01a528305a2ce209adec33 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/response/AllergyResponse.java @@ -0,0 +1,18 @@ +package ntnu.idatt2016.v233.SmartMat.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * This class represents a response for a weekly menu. + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class AllergyResponse { + private String name; + private String description; +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/AllergyService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/AllergyService.java new file mode 100644 index 0000000000000000000000000000000000000000..168345c8a53d7e79a2bf83643a6bd88cc7557401 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/AllergyService.java @@ -0,0 +1,68 @@ +package ntnu.idatt2016.v233.SmartMat.service; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import ntnu.idatt2016.v233.SmartMat.dto.response.AllergyResponse; +import ntnu.idatt2016.v233.SmartMat.entity.product.Allergy; +import ntnu.idatt2016.v233.SmartMat.repository.AllergyRepository; + +/** + * Service for allergies + * @author Stian Lyng + * @version 1.0 + */ +@Service +public class AllergyService { + + /** + * Repository for allergies + */ + @Autowired + private AllergyRepository allergyRepository; + + /** + * Returns all allergies + * @return List of allergies + */ + public List<AllergyResponse> getAllAllergies() { + List<Allergy> allergies = allergyRepository.findAll(); + return allergies.stream() + .map(allergy -> AllergyResponse.builder() + .name(allergy.getName()) + .description(allergy.getDescription()) + .build()) + .collect(Collectors.toList()); + } + + /** + * Returns allergy by name + * + * @param name Name of allergy + * @return Optional of allergy + */ + public Optional<Allergy> getAllergyByName(String name) { + return allergyRepository.findById(name); + } + + /** + * Saves allergy + * @param allergy Allergy to save + */ + public void saveAllergy(Allergy allergy) { + allergyRepository.save(allergy); + } + + /** + * Deletes allergy by name + * + * @param name Name of allergy to delete + */ + public void deleteAllergyByName(String name) { + allergyRepository.deleteById(name); + } +}