70 lines
2.4 KiB
Java
70 lines
2.4 KiB
Java
package com.iconplus.smartproc.controller;
|
|
|
|
import com.iconplus.smartproc.model.entity.Lokasi;
|
|
import com.iconplus.smartproc.exception.ResourceNotFoundException;
|
|
import com.iconplus.smartproc.repository.LokasiRepository;
|
|
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/lokasi")
|
|
public class LokasiController {
|
|
@Autowired
|
|
private LokasiRepository lokasiRepository;
|
|
|
|
//get all data
|
|
@GetMapping
|
|
public List<Lokasi> getAllLokasis(){
|
|
return lokasiRepository.findAll();
|
|
}
|
|
|
|
// create
|
|
@PostMapping
|
|
public Lokasi createLokasi(@RequestBody Lokasi lokasi) {
|
|
return lokasiRepository.save(lokasi);
|
|
}
|
|
|
|
// get lokasi by id rest api
|
|
@GetMapping("/{id}")
|
|
public ResponseEntity<Lokasi> getLokasiById(@PathVariable Long id) {
|
|
Lokasi lokasi = lokasiRepository.findById(id)
|
|
.orElseThrow(() -> new ResourceNotFoundException("Lokasi not exist with id :" + id));
|
|
return ResponseEntity.ok(lokasi);
|
|
}
|
|
|
|
// update lokasi rest api
|
|
@PutMapping("/{id}")
|
|
public ResponseEntity<Lokasi> updateSumberdana(@PathVariable Long id, @RequestBody Lokasi lokasiDetails){
|
|
Lokasi lokasi = lokasiRepository.findById(id)
|
|
.orElseThrow(() -> new ResourceNotFoundException("Lokasi not exist with id :" + id));
|
|
|
|
lokasi.setLokasi(lokasiDetails.getLokasi());
|
|
lokasi.setKeterangan(lokasiDetails.getKeterangan());
|
|
|
|
Lokasi updatedLokasi = lokasiRepository.save(lokasi);
|
|
return ResponseEntity.ok(updatedLokasi);
|
|
}
|
|
|
|
// delete lokasi rest api
|
|
@DeleteMapping("/{id}")
|
|
public ResponseEntity<Map<String, Boolean>> deleteLokasi(@PathVariable Long id){
|
|
Lokasi lokasi = lokasiRepository.findById(id)
|
|
.orElseThrow(() -> new ResourceNotFoundException("Lokasi not exist with id :" + id));
|
|
|
|
lokasiRepository.delete(lokasi);
|
|
Map<String, Boolean> response = new HashMap<>();
|
|
response.put("deleted", Boolean.TRUE);
|
|
return ResponseEntity.ok(response);
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|