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

66 lines
2.5 KiB
Plaintext

package com.healthcare.ohctech.controller;
import com.healthcare.ohctech.dto.RoleDto;
import com.healthcare.ohctech.entity.Role;
import com.healthcare.ohctech.service.impl.RoleServiceImpl;
import com.healthcare.ohctech.util.PaginationUtil;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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("/roles")
public class RoleController {
Logger logger = LoggerFactory.getLogger(RoleController.class);
@Autowired
private RoleServiceImpl roleServiceImpl;
@GetMapping("/{roleId}")
public ResponseEntity<?> getRoleById(@PathVariable Integer roleId) {
Role role = roleServiceImpl.getRoleById(roleId);
if (role != null) {
return new ResponseEntity<>(role, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@GetMapping
public ResponseEntity<?> getAllRoles(@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<Role> rolePage = roleServiceImpl.getAllRoles(pageable);
Map<String, Object> response = PaginationUtil.getPageResponse(rolePage);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@PostMapping
public ResponseEntity<?> addRole(@Valid @RequestBody RoleDto roleDto) {
roleServiceImpl.addRole(roleDto);
return new ResponseEntity<>(HttpStatus.CREATED);
}
@PutMapping("/{roleId}")
public ResponseEntity<?> updateRole(@Valid @RequestBody RoleDto roleDto) {
roleServiceImpl.updateRole(roleDto);
return new ResponseEntity<>(HttpStatus.OK);
}
@DeleteMapping("/{roleId}")
public ResponseEntity<?> deleteRole(@PathVariable Long roleId) {
roleServiceImpl.deleteRole(roleId);
return new ResponseEntity<>(HttpStatus.OK);
}
}