ohctechv3/.svn/pristine/41/41e1cd1bf67f2f3b271cfbf9488a9665419e9cb8.svn-base

68 lines
2.9 KiB
Plaintext
Raw Permalink Normal View History

2024-10-28 15:03:36 +05:30
package com.healthcare.ohctech.controller;
import com.healthcare.ohctech.dto.HealthAdviceDto;
import com.healthcare.ohctech.entity.HealthAdvice;
import com.healthcare.ohctech.service.impl.AuthServiceImpl;
import com.healthcare.ohctech.service.impl.HealthAdviceServiceImpl;
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("/health-advice")
public class HealthAdviceController {
@Autowired
private HealthAdviceServiceImpl healthAdviceServiceImpl;
@Autowired
private AuthServiceImpl authServiceImpl;
@GetMapping("/{id}")
public ResponseEntity<?> getHealthAdviceById(@PathVariable Long id) {
HealthAdvice healthAdvice = healthAdviceServiceImpl.getHealthAdviceById(id);
if (healthAdvice != null) {
return new ResponseEntity<>(healthAdvice, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@GetMapping
public ResponseEntity<?> getAllHealthAdvice(@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<HealthAdvice> healthAdvicePage = healthAdviceServiceImpl.getAllHealthAdvices(pageable);
Map<String, Object> response = PaginationUtil.getPageResponse(healthAdvicePage);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@PostMapping
public ResponseEntity<?> addHealthAdvice(@Valid @RequestBody HealthAdviceDto healthAdviceDto) {
Long userId = authServiceImpl.getCurrentUserId();
healthAdviceServiceImpl.addHealthAdvice(healthAdviceDto,userId);
return new ResponseEntity<>("Saved Successfully", HttpStatus.CREATED);
}
@PutMapping("/{id}")
public ResponseEntity<?> updateHealthAdvice(@Valid @RequestBody HealthAdviceDto healthAdviceDto) {
Long userId = authServiceImpl.getCurrentUserId();
healthAdviceServiceImpl.updateHealthAdvice(healthAdviceDto,userId);
return new ResponseEntity<>("Updated Successfully", HttpStatus.OK);
}
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteHealthAdvice(@PathVariable Long id) {
healthAdviceServiceImpl.deleteHealthAdvice(id);
return new ResponseEntity<>(HttpStatus.OK);
}
}