Add removing indicator
This commit is contained in:
parent
2ef33f1b68
commit
8b8d64bd00
@ -1,6 +1,8 @@
|
||||
package ru.ulstu.admin.model;
|
||||
|
||||
public class IndicatorForm {
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
@ -10,4 +12,12 @@ public class IndicatorForm {
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
@ -33,10 +33,15 @@ public class AdminController {
|
||||
return "editIndicator";
|
||||
}
|
||||
|
||||
@PostMapping("saveIndicator")
|
||||
@PostMapping(value = "saveIndicator", params = "save")
|
||||
public String saveIndicator(IndicatorForm indicatorForm, Model model) {
|
||||
adminService.saveIndicator(indicatorForm);
|
||||
model.addAttribute("indicators", adminService.getIndicators());
|
||||
return "indicatorsList";
|
||||
return "redirect:/admin/indicators";
|
||||
}
|
||||
|
||||
@PostMapping(value = "saveIndicator", params = "delete")
|
||||
public String deleteIndicator(IndicatorForm indicatorForm, Model model) {
|
||||
adminService.deleteIndicator(indicatorForm);
|
||||
return "redirect:/admin/indicators";
|
||||
}
|
||||
}
|
||||
|
@ -20,15 +20,18 @@ public class AdminService {
|
||||
}
|
||||
|
||||
public void saveIndicator(IndicatorForm indicatorForm) {
|
||||
Indicator indicator = indicatorService.findByName(indicatorForm.getName());
|
||||
if (indicator == null) {
|
||||
indicatorService.save(new Indicator(indicatorForm.getName()));
|
||||
} else {
|
||||
// TODO: update other fields
|
||||
}
|
||||
Indicator indicator = indicatorForm.getId() == null
|
||||
? new Indicator(indicatorForm.getName())
|
||||
: indicatorService.getIndicatorById(indicatorForm.getId());
|
||||
indicator.setName(indicatorForm.getName());
|
||||
indicatorService.save(indicator);
|
||||
}
|
||||
|
||||
public Indicator getIndicatorById(Integer id) {
|
||||
return indicatorService.getIndicatorById(id);
|
||||
}
|
||||
|
||||
public void deleteIndicator(IndicatorForm indicatorForm) {
|
||||
indicatorService.deleteIndicator(indicatorForm);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package ru.ulstu.indicator.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.admin.model.IndicatorForm;
|
||||
import ru.ulstu.indicator.model.Indicator;
|
||||
import ru.ulstu.indicator.repository.IndicatorRepository;
|
||||
|
||||
@ -29,4 +30,8 @@ public class IndicatorService {
|
||||
public Indicator getIndicatorById(Integer id) {
|
||||
return indicatorRepository.findById(id).orElseThrow(() -> new RuntimeException("Indicator not found"));
|
||||
}
|
||||
|
||||
public void deleteIndicator(IndicatorForm indicatorForm) {
|
||||
indicatorRepository.deleteById(indicatorForm.getId());
|
||||
}
|
||||
}
|
||||
|
@ -15,8 +15,14 @@
|
||||
Не может быть пустым</p>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-outline-dark">Сохранить</button>
|
||||
<a href="javascript:history.back()" class="btn btn-outline-dark">Отмена</a>
|
||||
<button name="save" type="submit" class="btn btn-outline-dark">Сохранить</button>
|
||||
<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>
|
||||
<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"/>
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
<ul>
|
||||
<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>
|
||||
</ul>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user