add master data api
This commit is contained in:
@@ -1,68 +1,79 @@
|
||||
package com.iconplus.smartproc.controller;
|
||||
|
||||
import com.iconplus.smartproc.model.entity.MetodePengadaan;
|
||||
import com.iconplus.smartproc.exception.ResourceNotFoundException;
|
||||
import com.iconplus.smartproc.repository.MetodePengadaanRepository;
|
||||
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;
|
||||
import com.iconplus.smartproc.helper.model.EmptyResponse;
|
||||
import com.iconplus.smartproc.model.request.MetodePengadaanRequest;
|
||||
import com.iconplus.smartproc.model.response.GetListMetodePengadaanResponse;
|
||||
import com.iconplus.smartproc.model.response.MetodePengadaanResponse;
|
||||
import com.iconplus.smartproc.repository.MetodePengadaanRepository;
|
||||
import com.iconplus.smartproc.service.metodepengadaan.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:8080", allowCredentials = "true")
|
||||
@RestController
|
||||
@RequestMapping("/api/metodepengadaan")
|
||||
public class MetodePengadaanController {
|
||||
@Autowired
|
||||
private MetodePengadaanRepository metodepengadaanRepository;
|
||||
|
||||
//get all data
|
||||
@GetMapping
|
||||
public List<MetodePengadaan> getAllMetodepengadaans(){
|
||||
return metodepengadaanRepository.findAll();
|
||||
}
|
||||
private final GetListMetodePengadaanService getListMetodePengadaanService;
|
||||
private final GetMetodePengadaanByIdService getMetodePengadaanByIdService;
|
||||
private final PostCreateMetodePengadaanService postCreateMetodePengadaanService;
|
||||
private final PutUpdateMetodePengadaanService putUpdateMetodePengadaanService;
|
||||
private final DeleteMetodePengadaanService deleteMetodePengadaanService;
|
||||
|
||||
// create
|
||||
@PostMapping
|
||||
public MetodePengadaan createMetodepengadaan(@RequestBody MetodePengadaan metodepengadaan) {
|
||||
return metodepengadaanRepository.save(metodepengadaan);
|
||||
}
|
||||
public MetodePengadaanController(GetListMetodePengadaanService getListMetodePengadaanService,
|
||||
GetMetodePengadaanByIdService getMetodePengadaanByIdService,
|
||||
PostCreateMetodePengadaanService postCreateMetodePengadaanService,
|
||||
PutUpdateMetodePengadaanService putUpdateMetodePengadaanService,
|
||||
DeleteMetodePengadaanService deleteMetodePengadaanService) {
|
||||
this.getListMetodePengadaanService = getListMetodePengadaanService;
|
||||
this.getMetodePengadaanByIdService = getMetodePengadaanByIdService;
|
||||
this.postCreateMetodePengadaanService = postCreateMetodePengadaanService;
|
||||
this.putUpdateMetodePengadaanService = putUpdateMetodePengadaanService;
|
||||
this.deleteMetodePengadaanService = deleteMetodePengadaanService;
|
||||
}
|
||||
|
||||
// get metodepengadaan by id rest api
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<MetodePengadaan> getMetodepengadaanById(@PathVariable Long id) {
|
||||
MetodePengadaan metodepengadaan = metodepengadaanRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Metodepengadaan not exist with id :" + id));
|
||||
return ResponseEntity.ok(metodepengadaan);
|
||||
}
|
||||
|
||||
// update metodepengadaan rest api
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<MetodePengadaan> updateMetodepengadaan(@PathVariable Long id, @RequestBody MetodePengadaan metodePengadaanDetails){
|
||||
MetodePengadaan metodepengadaan = metodepengadaanRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Metodepengadaan not exist with id :" + id));
|
||||
@GetMapping
|
||||
public GetListMetodePengadaanResponse getListMetodePengadaan(@RequestParam(name = "search", required = false) String search,
|
||||
@RequestParam(name = "page", defaultValue = "1") Integer page,
|
||||
@RequestParam(name = "size", defaultValue = "5") Integer size){
|
||||
|
||||
metodepengadaan.setMetodePengadaan(metodePengadaanDetails.getMetodePengadaan());
|
||||
metodepengadaan.setKeterangan(metodePengadaanDetails.getKeterangan());
|
||||
Pageable pageable = PageRequest.of((page - 1), size);
|
||||
MetodePengadaanRequest metodePengadaanRequest = MetodePengadaanRequest.builder()
|
||||
.search(search)
|
||||
.pageable(pageable)
|
||||
.build();
|
||||
|
||||
MetodePengadaan updatedMetodePengadaan = metodepengadaanRepository.save(metodepengadaan);
|
||||
return ResponseEntity.ok(updatedMetodePengadaan);
|
||||
}
|
||||
return getListMetodePengadaanService.execute(metodePengadaanRequest);
|
||||
}
|
||||
|
||||
// delete metodepengadaan rest api
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Map<String, Boolean>> deleteMetodepengadaan(@PathVariable Long id){
|
||||
MetodePengadaan metodepengadaan = metodepengadaanRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Metodepengadaan not exist with id :" + id));
|
||||
@PostMapping
|
||||
public MetodePengadaanResponse createMetodePengadaan(@RequestBody MetodePengadaanRequest metodePengadaanRequest) {
|
||||
return postCreateMetodePengadaanService.execute(metodePengadaanRequest);
|
||||
}
|
||||
|
||||
metodepengadaanRepository.delete(metodepengadaan);
|
||||
Map<String, Boolean> response = new HashMap<>();
|
||||
response.put("deleted", Boolean.TRUE);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
@GetMapping("/{id}")
|
||||
public MetodePengadaanResponse getMetodePengadaanById(@PathVariable Long id) {
|
||||
return getMetodePengadaanByIdService.execute(MetodePengadaanRequest.builder()
|
||||
.id(id)
|
||||
.build());
|
||||
}
|
||||
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public MetodePengadaanResponse updateMetodepengadaan(@PathVariable Long id,
|
||||
@RequestBody MetodePengadaanRequest metodePengadaanRequest){
|
||||
metodePengadaanRequest.setId(id);
|
||||
return putUpdateMetodePengadaanService.execute(metodePengadaanRequest);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public EmptyResponse deleteMetodePengadaan(@PathVariable Long id) {
|
||||
return deleteMetodePengadaanService.execute(MetodePengadaanRequest.builder()
|
||||
.id(id)
|
||||
.build());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user