ohctechv3/.svn/pristine/e7/e7376350944ed4ef07b91998cac90a3db47c4190.svn-base
2024-10-28 15:03:36 +05:30

68 lines
2.6 KiB
Plaintext

package com.healthcare.ohctech.controller;
import com.healthcare.ohctech.dto.PlantDto;
import com.healthcare.ohctech.entity.Plant;
import com.healthcare.ohctech.service.impl.AuthServiceImpl;
import com.healthcare.ohctech.service.impl.PlantServiceImpl;
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("/plants")
public class PlantController {
@Autowired
private PlantServiceImpl plantServiceImpl;
@Autowired
private AuthServiceImpl authServiceImpl;
@GetMapping("/{plantId}")
public ResponseEntity<?> getPlantById(@PathVariable Long plantId) {
Plant plant = plantServiceImpl.getPlantById(plantId);
if (plant != null) {
return new ResponseEntity<>(plant, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@GetMapping
public ResponseEntity<?> getAllPlants(@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<Plant> plantPage = plantServiceImpl.getAllPlants(pageable);
Map<String, Object> response = PaginationUtil.getPageResponse(plantPage);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@PostMapping
public ResponseEntity<?> addPlant(@Valid @RequestBody PlantDto plantDto) {
Long userId = authServiceImpl.getCurrentUserId();
plantServiceImpl.addPlant(plantDto,userId);
return new ResponseEntity<>("Saved Successfully", HttpStatus.CREATED);
}
@PutMapping("/{plantId}")
public ResponseEntity<?> updatePlant(@Valid @RequestBody PlantDto plantDto) {
Long userId = authServiceImpl.getCurrentUserId();
plantServiceImpl.updatePlant(plantDto,userId);
return new ResponseEntity<>("Updated Successfully", HttpStatus.OK);
}
@DeleteMapping("/{plantId}")
public ResponseEntity<?> deletePlant(@PathVariable Long plantId) {
plantServiceImpl.deletePlant(plantId);
return new ResponseEntity<>(HttpStatus.OK);
}
}