65 lines
2.6 KiB
Plaintext
65 lines
2.6 KiB
Plaintext
package com.healthcare.ohctech.controller;
|
|
|
|
import com.healthcare.ohctech.dto.AilmentDto;
|
|
import com.healthcare.ohctech.entity.Ailment;
|
|
import com.healthcare.ohctech.service.impl.AilmentServiceImpl;
|
|
import com.healthcare.ohctech.service.impl.AuthServiceImpl;
|
|
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("/ailments")
|
|
public class AilmentController {
|
|
|
|
@Autowired
|
|
private AilmentServiceImpl ailmentServiceImpl;
|
|
|
|
@Autowired
|
|
private AuthServiceImpl authServiceImpl;
|
|
|
|
@GetMapping("/{ailmentId}")
|
|
public ResponseEntity<?> getAilmentById(@PathVariable Long ailmentId) {
|
|
Ailment ailment = ailmentServiceImpl.getAilmentById(ailmentId);
|
|
return new ResponseEntity<>(ailment, HttpStatus.OK);
|
|
}
|
|
|
|
@GetMapping
|
|
public ResponseEntity<?> getAllAilments(@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<Ailment> ailmentPage = ailmentServiceImpl.getAllAilments(pageable);
|
|
Map<String, Object> response = PaginationUtil.getPageResponse(ailmentPage);
|
|
return new ResponseEntity<>(ailmentPage, HttpStatus.OK);
|
|
}
|
|
|
|
@PostMapping
|
|
public ResponseEntity<?> addAilment(@Valid @RequestBody AilmentDto ailmentDto) {
|
|
Long userId = authServiceImpl.getCurrentUserId();
|
|
ailmentServiceImpl.addAilment(ailmentDto,userId);
|
|
return new ResponseEntity<>(HttpStatus.CREATED);
|
|
}
|
|
|
|
@PutMapping("/{ailmentId}")
|
|
public ResponseEntity<?> updateAilment(@Valid @RequestBody AilmentDto ailmentDto) {
|
|
Long userId = authServiceImpl.getCurrentUserId();
|
|
ailmentServiceImpl.updateAilment(ailmentDto,userId);
|
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
}
|
|
|
|
@DeleteMapping("/{ailmentId}")
|
|
public ResponseEntity<?> deleteAilment(@PathVariable Long ailmentId) {
|
|
ailmentServiceImpl.deleteAilment(ailmentId);
|
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
}
|
|
}
|