public PesertaController() { Peserta p = new Peserta(); p.setNomerPeserta("ABC-123"); p.setNama("Endy Muhardin"); p.setTanggalLahir(new Date()); service.simpan(p); }
@RequestMapping(value = "/", method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) public void simpan( @RequestBody Peserta peserta, HttpServletRequest request, HttpServletResponse response) { service.simpan(peserta); String requestUrl = request.getRequestURL().toString(); URI uri = new UriTemplate("{requestUrl}/{id}").expand(requestUrl, peserta.getId()); response.setHeader("Location", uri.toASCIIString()); }
@RequestMapping(value = "/{id}", method = RequestMethod.PUT) @ResponseStatus(HttpStatus.NO_CONTENT) public void update(@PathVariable Integer id, @RequestBody Peserta peserta) { Peserta px = service.findPesertaById(id); px.setNama(peserta.getNama()); px.setNomerPeserta(peserta.getNomerPeserta()); px.setTanggalLahir(peserta.getTanggalLahir()); }
@RequestMapping(value = "/", method = RequestMethod.GET) @ResponseBody public List<Peserta> semuaPeserta(@RequestParam(required = false) String nama) { Integer start = 0; Integer rows = 10; List<Peserta> semua = service.findSemuaPeserta(start, rows); List<Peserta> hasil = new ArrayList<Peserta>(); // harusnya ini query ke db select * from peserta where nama like ? if (nama != null && !nama.isEmpty()) { for (Peserta peserta : semua) { if (peserta.getNama().contains(nama)) { hasil.add(peserta); } } } else { hasil.addAll(semua); } return hasil; }