add master data api
This commit is contained in:
@@ -1,69 +1,76 @@
|
||||
package com.iconplus.smartproc.controller;
|
||||
|
||||
import com.iconplus.smartproc.model.entity.StrategiPengadaan;
|
||||
import com.iconplus.smartproc.exception.ResourceNotFoundException;
|
||||
import com.iconplus.smartproc.repository.StrategiPengadaanRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import com.iconplus.smartproc.helper.model.EmptyResponse;
|
||||
import com.iconplus.smartproc.model.request.StrategiPengadaanRequest;
|
||||
import com.iconplus.smartproc.model.response.GetListStrategiPengadaanResponse;
|
||||
import com.iconplus.smartproc.model.response.StrategiPengadaanResponse;
|
||||
import com.iconplus.smartproc.service.strategipengadaan.*;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
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/strategipengadaan")
|
||||
public class StrategiPengadaanController {
|
||||
@Autowired
|
||||
private StrategiPengadaanRepository strategipengadaanRepository;
|
||||
|
||||
//get all data
|
||||
@GetMapping
|
||||
public List<StrategiPengadaan> getAllstrategipengadaans(){
|
||||
return strategipengadaanRepository.findAll();
|
||||
}
|
||||
private final DeleteStrategiPengadaanService deleteStrategiPengadaanService;
|
||||
private final GetListStrategiPengadaanService getListStrategiPengadaanService;
|
||||
private final GetStrategiPengadaanByIdService getStrategiPengadaanByIdService;
|
||||
private final PostCreateStrategiPengadaanService postCreateStrategiPengadaanService;
|
||||
private final PutUpdateStrategiPengadaanService putUpdateStrategiPengadaanService;
|
||||
|
||||
// create
|
||||
@PostMapping
|
||||
public StrategiPengadaan createStrategipengadaan(@RequestBody StrategiPengadaan strategipengadaan) {
|
||||
return strategipengadaanRepository.save(strategipengadaan);
|
||||
}
|
||||
public StrategiPengadaanController(DeleteStrategiPengadaanService deleteStrategiPengadaanService,
|
||||
GetListStrategiPengadaanService getListStrategiPengadaanService,
|
||||
GetStrategiPengadaanByIdService getStrategiPengadaanByIdService,
|
||||
PostCreateStrategiPengadaanService postCreateStrategiPengadaanService,
|
||||
PutUpdateStrategiPengadaanService putUpdateStrategiPengadaanService) {
|
||||
this.deleteStrategiPengadaanService = deleteStrategiPengadaanService;
|
||||
this.getListStrategiPengadaanService = getListStrategiPengadaanService;
|
||||
this.getStrategiPengadaanByIdService = getStrategiPengadaanByIdService;
|
||||
this.postCreateStrategiPengadaanService = postCreateStrategiPengadaanService;
|
||||
this.putUpdateStrategiPengadaanService = putUpdateStrategiPengadaanService;
|
||||
|
||||
// get strategipengadaan by id rest api
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<StrategiPengadaan> getStrategipengadaanById(@PathVariable Long id) {
|
||||
StrategiPengadaan strategipengadaan = strategipengadaanRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Strategipengadaan not exist with id :" + id));
|
||||
return ResponseEntity.ok(strategipengadaan);
|
||||
}
|
||||
|
||||
// update strategipengadaan rest api
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<StrategiPengadaan> updateStrategipengadaan(@PathVariable Long id, @RequestBody StrategiPengadaan strategiPengadaanDetails){
|
||||
StrategiPengadaan strategipengadaan = strategipengadaanRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Strategipengadaan not exist with id :" + id));
|
||||
|
||||
strategipengadaan.setStrategiPengadaan(strategiPengadaanDetails.getStrategiPengadaan());
|
||||
strategipengadaan.setKeterangan(strategiPengadaanDetails.getKeterangan());
|
||||
|
||||
StrategiPengadaan updatedStrategiPengadaan = strategipengadaanRepository.save(strategipengadaan);
|
||||
return ResponseEntity.ok(updatedStrategiPengadaan);
|
||||
}
|
||||
|
||||
// delete strategipengadaan rest api
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Map<String, Boolean>> deleteStrategipengadaan(@PathVariable Long id){
|
||||
StrategiPengadaan strategipengadaan = strategipengadaanRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Strategipengadaan not exist with id :" + id));
|
||||
|
||||
strategipengadaanRepository.delete(strategipengadaan);
|
||||
Map<String, Boolean> response = new HashMap<>();
|
||||
response.put("deleted", Boolean.TRUE);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@GetMapping
|
||||
public GetListStrategiPengadaanResponse getListStrategiPengadaan(@RequestParam(name = "search", required = false) String search,
|
||||
@RequestParam(name = "page", defaultValue = "1") Integer page,
|
||||
@RequestParam(name = "size", defaultValue = "5") Integer size){
|
||||
|
||||
Pageable pageable = PageRequest.of((page - 1), size);
|
||||
StrategiPengadaanRequest strategiPengadaanRequest = StrategiPengadaanRequest.builder()
|
||||
.search(search)
|
||||
.pageable(pageable)
|
||||
.build();
|
||||
|
||||
return getListStrategiPengadaanService.execute(strategiPengadaanRequest);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public StrategiPengadaanResponse createStrategiPengadaan(@RequestBody StrategiPengadaanRequest strategiPengadaanRequest) {
|
||||
return postCreateStrategiPengadaanService.execute(strategiPengadaanRequest);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public StrategiPengadaanResponse getStrategiPengadaan(@PathVariable Long id) {
|
||||
return getStrategiPengadaanByIdService.execute(StrategiPengadaanRequest.builder()
|
||||
.id(id)
|
||||
.build());
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public StrategiPengadaanResponse updateStrategiPengadaan(@PathVariable Long id,
|
||||
@RequestBody StrategiPengadaanRequest strategiPengadaanRequest){
|
||||
strategiPengadaanRequest.setId(id);
|
||||
return putUpdateStrategiPengadaanService.execute(strategiPengadaanRequest);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public EmptyResponse deleteStrategiPengadaan(@PathVariable Long id) {
|
||||
return deleteStrategiPengadaanService.execute(StrategiPengadaanRequest.builder()
|
||||
.id(id)
|
||||
.build());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user