ohctechv3/.svn/pristine/ce/ce13bed7330dca0dbfa0bc65eeee8a8f0e2470db.svn-base

86 lines
3.7 KiB
Plaintext
Raw Normal View History

2024-10-28 15:03:36 +05:30
package com.healthcare.ohctech.controller;
import com.healthcare.ohctech.dto.SubSectionDto;
import com.healthcare.ohctech.entity.SubSection;
import com.healthcare.ohctech.service.impl.AuthServiceImpl;
import com.healthcare.ohctech.service.impl.SubSectionServiceImpl;
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("/sub-sections")
public class SubSectionController {
@Autowired
private SubSectionServiceImpl subSectionServiceImpl;
@Autowired
private AuthServiceImpl authServiceImpl;
@GetMapping("/{subSectionId}")
public ResponseEntity<?> getSubSectionById(@PathVariable Long subSectionId) {
SubSection subSection = subSectionServiceImpl.getSubSectionById(subSectionId);
SubSectionDto subSectionDto = new SubSectionDto(
subSection.getId(),
subSection.getBusinessUnit().getId(),
subSection.getSection().getId(),
subSection.getDepartment().getId(),
subSection.getSubSectionName(),
subSection.getSubSectionHeadName(),
subSection.getSubSectionEmail(),
subSection.getModifiedBy(),
subSection.getLastModified()
);
return new ResponseEntity<>(subSectionDto, HttpStatus.OK);
}
@GetMapping
public ResponseEntity<?> getAllSubSections(@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<SubSection> subSectionPage = subSectionServiceImpl.getAllSubSections(pageable);
Page<SubSectionDto> subSectionDtoPage = subSectionPage.map(subSection -> new SubSectionDto(
subSection.getId(),
subSection.getBusinessUnit().getId(),
subSection.getSection().getId(),
subSection.getDepartment().getId(),
subSection.getSubSectionName(),
subSection.getSubSectionHeadName(),
subSection.getSubSectionEmail(),
subSection.getModifiedBy(),
subSection.getLastModified()
));
Map<String, Object> response = PaginationUtil.getPageResponse(subSectionDtoPage);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@PostMapping
public ResponseEntity<?> addSubSection(@Valid @RequestBody SubSectionDto subSectionDto) {
Long userId = authServiceImpl.getCurrentUserId();
subSectionServiceImpl.addSubSection(subSectionDto, userId);
return new ResponseEntity<>(HttpStatus.CREATED);
}
@PutMapping("/{subSectionId}")
public ResponseEntity<?> updateSubSection(@Valid @RequestBody SubSectionDto subSectionDto) {
Long userId = authServiceImpl.getCurrentUserId();
subSectionServiceImpl.updateSubSection(subSectionDto, userId);
return new ResponseEntity<>(HttpStatus.OK);
}
@DeleteMapping("/{subSectionId}")
public ResponseEntity<?> deleteSubSection(@PathVariable Long subSectionId) {
subSectionServiceImpl.deleteSubSection(subSectionId);
return new ResponseEntity<>(HttpStatus.OK);
}
}