Initial commit
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package com.iconplus.smartproc.controller;
|
||||
|
||||
import com.iconplus.smartproc.entity.Roles;
|
||||
import com.iconplus.smartproc.exception.ResourceNotFoundException;
|
||||
import com.iconplus.smartproc.repository.RolesRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:8080", allowCredentials = "true")
|
||||
@RestController
|
||||
@RequestMapping("/api/roles")
|
||||
public class RolesController {
|
||||
@Autowired
|
||||
private RolesRepository rolesRepository;
|
||||
|
||||
//get all data
|
||||
@GetMapping
|
||||
public List<Roles> getAllRoless(){
|
||||
return rolesRepository.findAll();
|
||||
}
|
||||
|
||||
// create
|
||||
@PostMapping
|
||||
public Roles createRoles(@RequestBody Roles roles) {
|
||||
return rolesRepository.save(roles);
|
||||
}
|
||||
|
||||
// get roles by id rest api
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Roles> getRolesById(@PathVariable Long id) {
|
||||
Roles roles = rolesRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Roles not exist with id :" + id));
|
||||
return ResponseEntity.ok(roles);
|
||||
}
|
||||
|
||||
// update roles rest api
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<Roles> updateRoles(@PathVariable Long id, @RequestBody Roles rolesDetails){
|
||||
Roles roles = rolesRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Roles not exist with id :" + id));
|
||||
|
||||
roles.setRoles(rolesDetails.getRoles());
|
||||
roles.setKeterangan(rolesDetails.getKeterangan());
|
||||
roles.setIsactive(rolesDetails.getIsactive());
|
||||
|
||||
Roles updatedRoles = rolesRepository.save(roles);
|
||||
return ResponseEntity.ok(updatedRoles);
|
||||
}
|
||||
|
||||
// delete roles rest api
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Map<String, Boolean>> deleteRoles(@PathVariable Long id){
|
||||
Roles roles = rolesRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Supposmatrix not exist with id :" + id));
|
||||
|
||||
rolesRepository.delete(roles);
|
||||
Map<String, Boolean> response = new HashMap<>();
|
||||
response.put("deleted", Boolean.TRUE);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user