81 lines
3.6 KiB
Plaintext
81 lines
3.6 KiB
Plaintext
package com.healthcare.ohctech.controller;
|
|
|
|
import com.healthcare.ohctech.dto.NutrientMasterDto;
|
|
import com.healthcare.ohctech.entity.NutrientMaster;
|
|
import com.healthcare.ohctech.service.impl.NutrientMasterServiceImpl;
|
|
import com.healthcare.ohctech.util.PaginationUtil;
|
|
import jakarta.validation.Valid;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.Map;
|
|
|
|
@RestController
|
|
@RequestMapping("/nutrients")
|
|
public class NutrientMasterController {
|
|
|
|
@Autowired
|
|
private NutrientMasterServiceImpl nutrientMasterServiceImpl;
|
|
|
|
@GetMapping("/{nutrientId}")
|
|
public ResponseEntity<?> getNutrientById(@PathVariable Long nutrientId) {
|
|
NutrientMaster nutrientMaster = nutrientMasterServiceImpl.getNutrientById(nutrientId);
|
|
NutrientMasterDto nutrientMasterDto = new NutrientMasterDto(
|
|
nutrientMaster.getId(),
|
|
nutrientMaster.getFoodMaster().getId(),
|
|
nutrientMaster.getCalories(),
|
|
nutrientMaster.getAddedSugar(),
|
|
nutrientMaster.getMaida(),
|
|
nutrientMaster.getQuantityInGrams(),
|
|
nutrientMaster.getProteins(),
|
|
nutrientMaster.getDeepFried(),
|
|
nutrientMaster.getSaturatedFats()
|
|
);
|
|
return new ResponseEntity<>(nutrientMasterDto, HttpStatus.OK);
|
|
}
|
|
|
|
@GetMapping
|
|
public ResponseEntity<?> getAllNutrients(@RequestParam(required = false) Integer page,
|
|
@RequestParam(required = false) Integer size,
|
|
@RequestParam(required = false) String sortBy,
|
|
@RequestParam(required = false) String sortOrder) {
|
|
Pageable pageable = PaginationUtil.getPageableWithDefaults(page, size, sortBy, sortOrder);
|
|
Page<NutrientMaster> nutrientPage = nutrientMasterServiceImpl.getAllNutrients(pageable);
|
|
Page<NutrientMasterDto> nutrientMasterDtoPage = nutrientPage.map(nutrientMaster -> new NutrientMasterDto(
|
|
nutrientMaster.getId(),
|
|
nutrientMaster.getFoodMaster().getId(),
|
|
nutrientMaster.getCalories(),
|
|
nutrientMaster.getAddedSugar(),
|
|
nutrientMaster.getMaida(),
|
|
nutrientMaster.getQuantityInGrams(),
|
|
nutrientMaster.getProteins(),
|
|
nutrientMaster.getDeepFried(),
|
|
nutrientMaster.getSaturatedFats()
|
|
));
|
|
Map<String, Object> response = PaginationUtil.getPageResponse(nutrientMasterDtoPage);
|
|
return new ResponseEntity<>(response, HttpStatus.OK);
|
|
}
|
|
|
|
@PostMapping
|
|
public ResponseEntity<?> addNutrient(@Valid @RequestBody NutrientMasterDto nutrientMasterDto) {
|
|
nutrientMasterServiceImpl.addNutrient(nutrientMasterDto);
|
|
return new ResponseEntity<>(HttpStatus.CREATED);
|
|
}
|
|
|
|
@PutMapping("/{nutrientId}")
|
|
public ResponseEntity<?> updateNutrient(@Valid @RequestBody NutrientMasterDto nutrientMasterDto) {
|
|
nutrientMasterServiceImpl.updateNutrient(nutrientMasterDto);
|
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
}
|
|
|
|
@DeleteMapping("/{nutrientId}")
|
|
public ResponseEntity<?> deleteNutrient(@PathVariable Long nutrientId) {
|
|
nutrientMasterServiceImpl.deleteNutrient(nutrientId);
|
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
}
|
|
}
|