Add removing indicator

This commit is contained in:
Anton Romanov 2025-01-29 09:25:39 +04:00
parent 2ef33f1b68
commit 8b8d64bd00
6 changed files with 41 additions and 12 deletions

View File

@ -1,6 +1,8 @@
package ru.ulstu.admin.model; package ru.ulstu.admin.model;
public class IndicatorForm { public class IndicatorForm {
private Integer id;
private String name; private String name;
public String getName() { public String getName() {
@ -10,4 +12,12 @@ public class IndicatorForm {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
} }

View File

@ -33,10 +33,15 @@ public class AdminController {
return "editIndicator"; return "editIndicator";
} }
@PostMapping("saveIndicator") @PostMapping(value = "saveIndicator", params = "save")
public String saveIndicator(IndicatorForm indicatorForm, Model model) { public String saveIndicator(IndicatorForm indicatorForm, Model model) {
adminService.saveIndicator(indicatorForm); adminService.saveIndicator(indicatorForm);
model.addAttribute("indicators", adminService.getIndicators()); return "redirect:/admin/indicators";
return "indicatorsList"; }
@PostMapping(value = "saveIndicator", params = "delete")
public String deleteIndicator(IndicatorForm indicatorForm, Model model) {
adminService.deleteIndicator(indicatorForm);
return "redirect:/admin/indicators";
} }
} }

View File

@ -20,15 +20,18 @@ public class AdminService {
} }
public void saveIndicator(IndicatorForm indicatorForm) { public void saveIndicator(IndicatorForm indicatorForm) {
Indicator indicator = indicatorService.findByName(indicatorForm.getName()); Indicator indicator = indicatorForm.getId() == null
if (indicator == null) { ? new Indicator(indicatorForm.getName())
indicatorService.save(new Indicator(indicatorForm.getName())); : indicatorService.getIndicatorById(indicatorForm.getId());
} else { indicator.setName(indicatorForm.getName());
// TODO: update other fields indicatorService.save(indicator);
}
} }
public Indicator getIndicatorById(Integer id) { public Indicator getIndicatorById(Integer id) {
return indicatorService.getIndicatorById(id); return indicatorService.getIndicatorById(id);
} }
public void deleteIndicator(IndicatorForm indicatorForm) {
indicatorService.deleteIndicator(indicatorForm);
}
} }

View File

@ -1,6 +1,7 @@
package ru.ulstu.indicator.service; package ru.ulstu.indicator.service;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.admin.model.IndicatorForm;
import ru.ulstu.indicator.model.Indicator; import ru.ulstu.indicator.model.Indicator;
import ru.ulstu.indicator.repository.IndicatorRepository; import ru.ulstu.indicator.repository.IndicatorRepository;
@ -29,4 +30,8 @@ public class IndicatorService {
public Indicator getIndicatorById(Integer id) { public Indicator getIndicatorById(Integer id) {
return indicatorRepository.findById(id).orElseThrow(() -> new RuntimeException("Indicator not found")); return indicatorRepository.findById(id).orElseThrow(() -> new RuntimeException("Indicator not found"));
} }
public void deleteIndicator(IndicatorForm indicatorForm) {
indicatorRepository.deleteById(indicatorForm.getId());
}
} }

View File

@ -15,8 +15,14 @@
Не может быть пустым</p> Не может быть пустым</p>
</div> </div>
<button type="submit" class="btn btn-outline-dark">Сохранить</button> <button name="save" type="submit" class="btn btn-outline-dark">Сохранить</button>
<a href="javascript:history.back()" class="btn btn-outline-dark">Отмена</a> <button name="delete"
type="submit"
class="btn btn-outline-dark"
onclick="return confirm('Удалить показатель?')">
Удалить
</button>
<a href="/admin/indicators" class="btn btn-outline-dark">Отмена</a>
</form> </form>
<link rel="stylesheet" href="/webjars/font-awesome/4.7.0/css/font-awesome.min.css"/> <link rel="stylesheet" href="/webjars/font-awesome/4.7.0/css/font-awesome.min.css"/>
<link rel="stylesheet" href="/webjars/bootstrap-glyphicons/bdd2cbfba0/css/bootstrap-glyphicons.css"/> <link rel="stylesheet" href="/webjars/bootstrap-glyphicons/bdd2cbfba0/css/bootstrap-glyphicons.css"/>

View File

@ -8,7 +8,7 @@
<ul> <ul>
<li th:each="i : ${indicators}"> <li th:each="i : ${indicators}">
<span th:text="${i.name}"></span> <a th:href="@{'/admin/editIndicator/' + ${i.id}}"><span th:text="${i.name}"></span></a>
</li> </li>
</ul> </ul>
</div> </div>