diff --git a/src/main/java/ru/ulstu/admin/controller/AdminAspirantController.java b/src/main/java/ru/ulstu/admin/controller/AdminAspirantController.java new file mode 100644 index 0000000..fe32327 --- /dev/null +++ b/src/main/java/ru/ulstu/admin/controller/AdminAspirantController.java @@ -0,0 +1,50 @@ +package ru.ulstu.admin.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import ru.ulstu.admin.model.AspirantForm; +import ru.ulstu.admin.service.AdminAspirantService; +import ru.ulstu.indicator.model.Course; +import ru.ulstu.indicator.model.Indicator; + +@Controller +@RequestMapping("admin") +public class AdminAspirantController { + private final AdminAspirantService adminAspirantService; + + public AdminAspirantController(AdminAspirantService adminAspirantService) { + this.adminAspirantService = adminAspirantService; + } + + @GetMapping("aspirants") + public String getListOfAspirants(Model model) { + model.addAttribute("aspirants", adminAspirantService.getAspirants()); + return "admin/aspirantsList"; + } + + @GetMapping("/editAspirant/{aspirantId}") + public String editAspirant(@PathVariable(value = "aspirantId") Integer id, Model model) { + model.addAttribute("aspirant", + (id != null && id != 0) + ? adminAspirantService.getAspirantById(id) + : new Indicator()); + model.addAttribute("courses", Course.values()); + return "admin/editAspirant"; + } + + @PostMapping(value = "saveAspirant", params = "save") + public String saveAspirant(AspirantForm aspirantForm, Model model) { + adminAspirantService.saveAspirant(aspirantForm); + return "redirect:/admin/aspirants"; + } + + @PostMapping(value = "saveAspirant", params = "delete") + public String deleteIndicator(AspirantForm aspirantForm, Model model) { + adminAspirantService.deleteAspirant(aspirantForm); + return "redirect:/admin/aspirants"; + } +} diff --git a/src/main/java/ru/ulstu/admin/service/AdminController.java b/src/main/java/ru/ulstu/admin/controller/AdminIndicatorController.java similarity index 68% rename from src/main/java/ru/ulstu/admin/service/AdminController.java rename to src/main/java/ru/ulstu/admin/controller/AdminIndicatorController.java index 5c68519..d188d44 100644 --- a/src/main/java/ru/ulstu/admin/service/AdminController.java +++ b/src/main/java/ru/ulstu/admin/controller/AdminIndicatorController.java @@ -1,4 +1,4 @@ -package ru.ulstu.admin.service; +package ru.ulstu.admin.controller; import org.springframework.security.access.annotation.Secured; import org.springframework.stereotype.Controller; @@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import ru.ulstu.admin.model.IndicatorForm; +import ru.ulstu.admin.service.AdminIndicatorService; import ru.ulstu.indicator.model.Course; import ru.ulstu.indicator.model.Indicator; import ru.ulstu.model.UserRoleConstants; @@ -15,17 +16,17 @@ import ru.ulstu.model.UserRoleConstants; @Controller @RequestMapping("/admin") @Secured({UserRoleConstants.ADMIN}) -public class AdminController { - private final AdminService adminService; +public class AdminIndicatorController { + private final AdminIndicatorService adminIndicatorService; - public AdminController(AdminService adminService) { - this.adminService = adminService; + public AdminIndicatorController(AdminIndicatorService adminIndicatorService) { + this.adminIndicatorService = adminIndicatorService; } @GetMapping("indicators") public String getListOfIndicators(Model model) { - model.addAttribute("indicators", adminService.getIndicators()); - return "indicatorsList"; + model.addAttribute("indicators", adminIndicatorService.getIndicators()); + return "admin/indicatorsList"; } @GetMapping("/editIndicator/{indicatorId}") @@ -33,21 +34,21 @@ public class AdminController { public String editIndicator(@PathVariable(value = "indicatorId") Integer id, Model model) { model.addAttribute("indicator", (id != null && id != 0) - ? adminService.getIndicatorById(id) + ? adminIndicatorService.getIndicatorById(id) : new Indicator()); model.addAttribute("courses", Course.values()); - return "editIndicator"; + return "admin/editIndicator"; } @PostMapping(value = "saveIndicator", params = "save") public String saveIndicator(IndicatorForm indicatorForm, Model model) { - adminService.saveIndicator(indicatorForm); + adminIndicatorService.saveIndicator(indicatorForm); return "redirect:/admin/indicators"; } @PostMapping(value = "saveIndicator", params = "delete") public String deleteIndicator(IndicatorForm indicatorForm, Model model) { - adminService.deleteIndicator(indicatorForm); + adminIndicatorService.deleteIndicator(indicatorForm); return "redirect:/admin/indicators"; } } diff --git a/src/main/java/ru/ulstu/admin/service/AdminAspirantService.java b/src/main/java/ru/ulstu/admin/service/AdminAspirantService.java new file mode 100644 index 0000000..2765a8e --- /dev/null +++ b/src/main/java/ru/ulstu/admin/service/AdminAspirantService.java @@ -0,0 +1,45 @@ +package ru.ulstu.admin.service; + +import org.springframework.stereotype.Service; +import ru.ulstu.admin.model.AspirantForm; +import ru.ulstu.aspirant.model.Aspirant; +import ru.ulstu.aspirant.service.AspirantService; + +import java.util.List; + +@Service +public class AdminAspirantService { + private final AspirantService aspirantService; + + public AdminAspirantService(AspirantService aspirantService) { + this.aspirantService = aspirantService; + } + + public List getAspirants() { + return aspirantService.getAspirants(); + } + + public Aspirant getAspirantById(Integer id) { + return aspirantService.getAspirantById(id); + } + + public void saveAspirant(AspirantForm aspirantForm) { + Aspirant aspirant = aspirantForm.getId() == null + ? new Aspirant(aspirantForm) + : aspirantService.getAspirantById(aspirantForm.getId()); + aspirant.setName(aspirantForm.getName()); + aspirant.setSurname(aspirantForm.getSurname()); + aspirant.setPatronymic(aspirantForm.getPatronymic()); + aspirant.setBase(aspirantForm.getBase()); + aspirant.setCourse(aspirantForm.getCourse()); + aspirant.setManager(aspirantForm.getManager()); + aspirant.setBirthDate(aspirantForm.getBirthDate()); + aspirant.setSpeciality(aspirantForm.getSpeciality()); + aspirant.setTheme(aspirantForm.getTheme()); + aspirantService.save(aspirant); + } + + public void deleteAspirant(AspirantForm aspirantForm) { + aspirantService.deleteAspirant(aspirantForm); + } +} diff --git a/src/main/java/ru/ulstu/admin/service/AdminService.java b/src/main/java/ru/ulstu/admin/service/AdminIndicatorService.java similarity index 92% rename from src/main/java/ru/ulstu/admin/service/AdminService.java rename to src/main/java/ru/ulstu/admin/service/AdminIndicatorService.java index 98159e0..dc38e2f 100644 --- a/src/main/java/ru/ulstu/admin/service/AdminService.java +++ b/src/main/java/ru/ulstu/admin/service/AdminIndicatorService.java @@ -8,10 +8,10 @@ import ru.ulstu.indicator.service.IndicatorService; import java.util.List; @Service -public class AdminService { +public class AdminIndicatorService { private final IndicatorService indicatorService; - public AdminService(IndicatorService indicatorService) { + public AdminIndicatorService(IndicatorService indicatorService) { this.indicatorService = indicatorService; } diff --git a/src/main/java/ru/ulstu/aspirant/repository/AspirantRepository.java b/src/main/java/ru/ulstu/aspirant/repository/AspirantRepository.java new file mode 100644 index 0000000..4c3f55b --- /dev/null +++ b/src/main/java/ru/ulstu/aspirant/repository/AspirantRepository.java @@ -0,0 +1,7 @@ +package ru.ulstu.aspirant.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.aspirant.model.Aspirant; + +public interface AspirantRepository extends JpaRepository { +} diff --git a/src/main/java/ru/ulstu/aspirant/service/AspirantService.java b/src/main/java/ru/ulstu/aspirant/service/AspirantService.java new file mode 100644 index 0000000..da93fc3 --- /dev/null +++ b/src/main/java/ru/ulstu/aspirant/service/AspirantService.java @@ -0,0 +1,33 @@ +package ru.ulstu.aspirant.service; + +import org.springframework.stereotype.Service; +import ru.ulstu.admin.model.AspirantForm; +import ru.ulstu.aspirant.model.Aspirant; +import ru.ulstu.aspirant.repository.AspirantRepository; + +import java.util.List; + +@Service +public class AspirantService { + private final AspirantRepository aspirantRepository; + + public AspirantService(AspirantRepository aspirantRepository) { + this.aspirantRepository = aspirantRepository; + } + + public List getAspirants() { + return aspirantRepository.findAll(); + } + + public Aspirant getAspirantById(Integer id) { + return aspirantRepository.findById(id).orElseThrow(() -> new RuntimeException("Aspirant not found by id")); + } + + public void save(Aspirant aspirant) { + aspirantRepository.save(aspirant); + } + + public void deleteAspirant(AspirantForm aspirantForm) { + aspirantRepository.deleteById(aspirantForm.getId()); + } +}