Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Bikin Interface namanya Group1 di package model
- package apap.tugas.sikoperasi.model;
- public interface Group1 {
- }
- PinjamanModel(Di tanggalPengajuan sama jumlahPeminjaman ada tambahan (groups = {Group1.class}))
- package apap.tugas.sikoperasi.model;
- import java.io.Serializable;
- import java.sql.Date;
- import java.time.LocalDate;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
- import javax.validation.constraints.NotNull;
- import com.fasterxml.jackson.annotation.JsonIgnore;
- import org.hibernate.annotations.OnDelete;
- import org.hibernate.annotations.OnDeleteAction;
- import org.springframework.format.annotation.DateTimeFormat;
- @Entity
- @Table(name = "pinjaman")
- public class PinjamanModel implements Serializable{
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Long idPinjaman;
- public Long getIdPinjaman() {
- return this.idPinjaman;
- }
- public void setIdPinjaman(Long idPinjaman) {
- this.idPinjaman = idPinjaman;
- }
- @NotNull(groups = {Group1.class})
- @DateTimeFormat(pattern = "yyyy-MM-dd")
- @Column(name = "tanggalPengajuan", nullable = false)
- private LocalDate tanggalPengajuan;
- public LocalDate getTanggalPengajuan() {
- return this.tanggalPengajuan;
- }
- public void setTanggalPengajuan(LocalDate tanggalPengajuan) {
- this.tanggalPengajuan = tanggalPengajuan;
- }
- @DateTimeFormat(pattern = "yyyy-MM-dd")
- @Column(name = "tanggalDisetujui")
- private LocalDate tanggalDisetujui;
- public LocalDate getTanggalDisetujui() {
- return this.tanggalDisetujui;
- }
- public void setTanggalDisetujui(LocalDate tanggalDisetujui) {
- this.tanggalDisetujui = tanggalDisetujui;
- }
- @DateTimeFormat(pattern = "yyyy-MM-dd")
- @Column(name = "tanggalPengembalian")
- private LocalDate tanggalPengembalian;
- public LocalDate getTanggalPengembalian() {
- return this.tanggalPengembalian;
- }
- public void setTanggalPengembalian(LocalDate tanggalPengembalian) {
- this.tanggalPengembalian = tanggalPengembalian;
- }
- @NotNull(groups = {Group1.class})
- @Column(name = "jumlahPinjaman", nullable = false)
- private Integer jumlahPinjaman;
- public Integer getJumlahPinjaman() {
- return this.jumlahPinjaman;
- }
- public void setJumlahPinjaman(Integer jumlahPinjaman) {
- this.jumlahPinjaman = jumlahPinjaman;
- }
- @NotNull
- @Column(name = "jumlahPengembalian", nullable = false)
- private Integer jumlahPengembalian;
- public Integer getJumlahPengembalian() {
- return this.jumlahPengembalian;
- }
- public void setJumlahPengembalian(Integer jumlahPengembalian) {
- this.jumlahPengembalian = jumlahPengembalian;
- }
- @NotNull
- @Column(name = "status", nullable = false)
- private Integer status;
- public Integer getStatus() {
- return this.status;
- }
- public void setStatus(Integer status) {
- this.status = status;
- }
- @ManyToOne(fetch = FetchType.EAGER, optional = false)
- @JoinColumn(name = "idAnggota", referencedColumnName = "id", nullable = false)
- @OnDelete(action = OnDeleteAction.CASCADE)
- @JsonIgnore
- private AnggotaModel anggota;
- public AnggotaModel getAnggota() {
- return this.anggota;
- }
- public void setAnggota(AnggotaModel anggota) {
- this.anggota = anggota;
- }
- }
- PinjamanRestService (@Valid diganti jadi @Validator)
- package apap.tugas.sikoperasi.restcontroller;
- import java.util.Map;
- import java.util.NoSuchElementException;
- import javax.validation.Valid;
- import apap.tugas.sikoperasi.model.Group1;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpStatus;
- import org.springframework.validation.BindingResult;
- import org.springframework.validation.annotation.Validated;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.server.ResponseStatusException;
- import apap.tugas.sikoperasi.model.PinjamanModel;
- import apap.tugas.sikoperasi.rest.BaseResponse;
- import apap.tugas.sikoperasi.service.PinjamanRestService;
- @RestController
- @RequestMapping("/api/v1")
- public class PinjamanRestController {
- @Autowired
- private PinjamanRestService pinjamanRestService;
- @PostMapping(value = "/pinjaman")
- private BaseResponse<PinjamanModel> createPinjaman(@Validated({Group1.class}) @RequestBody PinjamanModel pinjaman, BindingResult bindingResult) {
- if(bindingResult.hasFieldErrors()){
- throw new ResponseStatusException(
- HttpStatus.BAD_REQUEST, "Request body has invalid type or missing field");
- }
- // BaseResponse<PinjamanModel> response = new BaseResponse<>();
- //
- // try{
- // response.setStatus(200);
- // response.setMessage("success");
- // response.setResult(pinjamanRestService.createPinjaman(pinjaman));
- // return response;
- // } catch (NoSuchElementException e) {
- // throw new ResponseStatusException(
- // HttpStatus.NOT_FOUND, "Request body has invalid type or missing field");
- // }
- // }
- else {
- BaseResponse<PinjamanModel> response = new BaseResponse<>();
- try{
- response.setStatus(200);
- response.setMessage("success");
- response.setResult(pinjamanRestService.createPinjaman(pinjaman));
- return response;
- } catch (NoSuchElementException e) {
- throw new ResponseStatusException(
- HttpStatus.NOT_FOUND, "Request body has invalid type or missing field");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement