public PesertaController() { Peserta p = new Peserta(); p.setNomerPeserta("ABC-123"); p.setNama("Endy Muhardin"); p.setTanggalLahir(new Date()); service.simpan(p); }
@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.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 = "/import", method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) public void simpanBulk( @RequestBody List<Map<String, Object>> daftarPeserta, HttpServletRequest request, HttpServletResponse response) { for (Map<String, Object> peserta : daftarPeserta) { service.simpan(PesertaConverter.fromMap(peserta)); } String requestUrl = request.getContextPath() + "/rest/peserta/"; response.setHeader("Location", requestUrl); }
@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; }
@RequestMapping(value = "/{id}", method = RequestMethod.GET) @ResponseBody public Peserta findPesertaById(@PathVariable Integer id) { return service.findPesertaById(id); }