| | 19 | === Пријава учество на партија и кандидати |
| | 20 | |
| | 21 | Администраторот пристапува на адреса /admin/participations каде му се појавуа формулар за внесување податоци за опис на кандидатура, кандидат, име на пратија, реализација на избори и општина во која се кандидира. Под истиот се листаат сите претходни кандидатури кои биле внесени. За ова е одговорен следниот контролер: |
| | 22 | |
| | 23 | {{{#!java |
| | 24 | @GetMapping("/admin/participations") |
| | 25 | public String candidacies(Model m) { |
| | 26 | m.addAttribute("replaceTemplate", "elections_participation"); |
| | 27 | m.addAttribute("electionsRealizations", candidatesElectionRealizationService.findAll()); |
| | 28 | m.addAttribute("municipalities", municipalityService.findAll()); |
| | 29 | m.addAttribute("parties", partyService.findAll()); |
| | 30 | m.addAttribute("candidates", candidateService.findAll()); |
| | 31 | m.addAttribute("candidacy", new Candidacy()); |
| | 32 | m.addAttribute("candidacies", candidacyService.findAll()); |
| | 33 | return "admin"; |
| | 34 | } |
| | 35 | }}} |
| | 36 | |
| | 37 | По внесување на податоците и клик на копчето „Запиши“ се праќа POST барање до контролерот кој ги обработува. |
| | 38 | |
| | 39 | {{{#!java |
| | 40 | @PostMapping("/admin/participations") |
| | 41 | public String addCandidacy(@RequestParam(required = false) Long id, |
| | 42 | @RequestParam String description, |
| | 43 | @RequestParam Long candidate, |
| | 44 | @RequestParam Long candidatesElectionRealization, |
| | 45 | @RequestParam(required = false) Long municipality, |
| | 46 | @RequestParam Long party, |
| | 47 | Model m) { |
| | 48 | candidacyService.update(id, description, candidate, party, candidatesElectionRealization, municipality); |
| | 49 | return "redirect:/admin/participations"; |
| | 50 | } |
| | 51 | }}} |
| | 52 | |
| | 53 | Контролерот повикува функција од candidacyService за додавање и ажурирање (ажурирањето сѐ уште не е имплементирано во view-то) на кандидатури каде се креира нова инстанца од објектот Candidacy, и за истиот се полнат добиентите податоци од формуларот. Оттаму се повикува функцијата save од candidacyRepository кој наследува од JpaRepository. Таа служи за зачувување на новиот објект во базата. |
| | 54 | |
| | 55 | {{{#!java |
| | 56 | public Candidacy update(Long id, String description, Long candidateId, Long partyId, Long candidatesElectionRealizationId, Long municipalityId) { |
| | 57 | Candidacy candidacy = new Candidacy(); |
| | 58 | if (id != null){ |
| | 59 | candidacy = findById(id); |
| | 60 | } |
| | 61 | |
| | 62 | candidacy.setMunicipality(municipalityService.findById(municipalityId)); |
| | 63 | candidacy.setDescription(description); |
| | 64 | candidacy.setCandidate(candidateService.findById(candidateId)); |
| | 65 | candidacy.setParty(partyService.findById(partyId)); |
| | 66 | candidacy.setCandidatesElectionRealization(candidatesElectionRealizationService.findById(candidatesElectionRealizationId)); |
| | 67 | return candidacyRepository.save(candidacy); |
| | 68 | } |
| | 69 | }}} |