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

83 lines
3.5 KiB
Plaintext

package com.healthcare.ohctech.controller;
import com.healthcare.ohctech.dto.DepartmentDto;
import com.healthcare.ohctech.entity.Department;
import com.healthcare.ohctech.service.impl.AuthServiceImpl;
import com.healthcare.ohctech.service.impl.DepartmentServiceImpl;
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("/departments")
public class DepartmentController {
@Autowired
private DepartmentServiceImpl departmentServiceImpl;
@Autowired
private AuthServiceImpl authServiceImpl;
@GetMapping("/{departmentId}")
public ResponseEntity<?> getDepartmentById(@PathVariable Long departmentId) {
Department department = departmentServiceImpl.getDepartmentById(departmentId);
DepartmentDto departmentDto = new DepartmentDto(
department.getId(),
department.getBusinessUnit().getId(),
department.getDepartmentName(),
department.getDepartmentHeadName(),
department.getDepartmentEmail(),
department.getModifiedBy(),
department.getLastModified()
);
return new ResponseEntity<>(departmentDto, HttpStatus.OK);
}
@GetMapping
public ResponseEntity<?> getAllDepartments(@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<Department> departmentPage = departmentServiceImpl.getAllDepartments(pageable);
Page<DepartmentDto> departmentDtoPage = departmentPage.map(department -> new DepartmentDto(
department.getId(),
department.getBusinessUnit().getId(),
department.getDepartmentName(),
department.getDepartmentHeadName(),
department.getDepartmentEmail(),
department.getModifiedBy(),
department.getLastModified()
));
Map<String, Object> response = PaginationUtil.getPageResponse(departmentDtoPage);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@PostMapping
public ResponseEntity<?> addDepartment(@Valid @RequestBody DepartmentDto departmentDto) {
Long userId = authServiceImpl.getCurrentUserId();
departmentServiceImpl.addDepartment(departmentDto,userId);
return new ResponseEntity<>(HttpStatus.CREATED);
}
@PutMapping("/{departmentId}")
public ResponseEntity<?> updateDepartment(@Valid @RequestBody DepartmentDto departmentDto) {
Long userId = authServiceImpl.getCurrentUserId();
departmentServiceImpl.updateDepartment(departmentDto,userId);
return new ResponseEntity<>(HttpStatus.OK);
}
@DeleteMapping("/{departmentId}")
public ResponseEntity<?> deleteDepartment(@PathVariable Long departmentId) {
departmentServiceImpl.deleteDepartment(departmentId);
return new ResponseEntity<>(HttpStatus.OK);
}
}