ohctechv3/.svn/pristine/6b/6bf34bef3e92e346dc336d51f26e78c5f295b9be.svn-base
2024-10-28 15:03:36 +05:30

85 lines
3.5 KiB
Plaintext

package com.healthcare.ohctech.controller;
import com.healthcare.ohctech.dto.SectionDto;
import com.healthcare.ohctech.entity.Section;
import com.healthcare.ohctech.service.impl.AuthServiceImpl;
import com.healthcare.ohctech.service.impl.SectionServiceImpl;
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("/sections")
public class SectionController {
@Autowired
private SectionServiceImpl sectionServiceImpl;
@Autowired
private AuthServiceImpl authServiceImpl;
@GetMapping("/{sectionId}")
public ResponseEntity<?> getSectionById(@PathVariable Long sectionId) {
Section section = sectionServiceImpl.getSectionById(sectionId);
SectionDto sectionDto = new SectionDto(
section.getId(),
section.getBusinessUnit().getId(),
section.getDepartment().getId(),
section.getSectionName(),
section.getSectionHeadName(),
section.getSectionHeadEmail(),
section.getModifiedBy(),
section.getLastModified()
);
return new ResponseEntity<>(sectionDto, HttpStatus.OK);
}
@GetMapping
public ResponseEntity<?> getAllSections(@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<Section> sectionPage = sectionServiceImpl.getAllSections(pageable);
Page<SectionDto> sectionDtoPage = sectionPage.map(section -> new SectionDto(
section.getId(),
section.getBusinessUnit().getId(),
section.getDepartment().getId(),
section.getSectionName(),
section.getSectionHeadName(),
section.getSectionHeadEmail(),
section.getModifiedBy(),
section.getLastModified()
));
Map<String, Object> response = PaginationUtil.getPageResponse(sectionDtoPage);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@PostMapping
public ResponseEntity<?> addSection(@Valid @RequestBody SectionDto sectionDto) {
Long userId = authServiceImpl.getCurrentUserId();
sectionServiceImpl.addSection(sectionDto,userId);
return new ResponseEntity<>(HttpStatus.CREATED);
}
@PutMapping("/{sectionId}")
public ResponseEntity<?> updateSection(@Valid @RequestBody SectionDto sectionDto) {
Long userId = authServiceImpl.getCurrentUserId();
sectionServiceImpl.updateSection(sectionDto,userId);
return new ResponseEntity<>(HttpStatus.OK);
}
@DeleteMapping("/{sectionId}")
public ResponseEntity<?> deleteSection(@PathVariable Long sectionId) {
sectionServiceImpl.deleteSection(sectionId);
return new ResponseEntity<>(HttpStatus.OK);
}
}