Divide admin controllers and services

This commit is contained in:
Anton Romanov 2025-01-29 14:14:48 +04:00
parent 3b877050b7
commit a503a36e8b
6 changed files with 149 additions and 13 deletions

View File

@ -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";
}
}

View File

@ -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";
}
}

View File

@ -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<Aspirant> 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);
}
}

View File

@ -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;
}

View File

@ -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<Aspirant, Integer> {
}

View File

@ -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<Aspirant> 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());
}
}