Skip to content
Snippets Groups Projects
Commit 3b25b8ae authored by Stian Lyng Stræte's avatar Stian Lyng Stræte
Browse files

Merge branch 'feature/42-implement-allergies-controller' into 'main'

implement allergy controller

See merge request idatt2106-v23-03/backend!108
parents d7d7e6b4 8d1470c9
No related branches found
No related tags found
No related merge requests found
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);
}
}
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;
}
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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment