smartproc-be/src/main/java/com/iconplus/smartproc/controller/StrategiPengadaanController.java
2023-05-26 10:39:31 +07:00

77 lines
3.8 KiB
Java

package com.iconplus.smartproc.controller;
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.*;
@CrossOrigin(origins = "http://localhost:8080", allowCredentials = "true")
@RestController
@RequestMapping("/api/strategipengadaan")
public class StrategiPengadaanController {
private final DeleteStrategiPengadaanService deleteStrategiPengadaanService;
private final GetListStrategiPengadaanService getListStrategiPengadaanService;
private final GetStrategiPengadaanByIdService getStrategiPengadaanByIdService;
private final PostCreateStrategiPengadaanService postCreateStrategiPengadaanService;
private final PutUpdateStrategiPengadaanService putUpdateStrategiPengadaanService;
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;
}
@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());
}
}