manipulate slots
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package de.jottyfan.bico.modules.slot;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
||||
import de.jottyfan.bico.modules.CommonController;
|
||||
import de.jottyfan.bico.modules.slot.model.SlotBean;
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jotty
|
||||
*
|
||||
*/
|
||||
@Controller
|
||||
public class SlotController extends CommonController {
|
||||
|
||||
@Autowired
|
||||
private SlotService service;
|
||||
|
||||
@GetMapping("/slot")
|
||||
public String generate(Model model) {
|
||||
return load(null, model);
|
||||
}
|
||||
|
||||
@GetMapping("/slot/{id}")
|
||||
public String load(@PathVariable Integer id, Model model) {
|
||||
model.addAttribute("bean", id == null ? new SlotBean() : service.loadSlot(id));
|
||||
return "/slot/item";
|
||||
}
|
||||
|
||||
@GetMapping("/slot/{id}/delete")
|
||||
public String loadEnsurance(@PathVariable Integer id, Model model) {
|
||||
model.addAttribute("bean", service.loadDeletableSlot(id));
|
||||
return "/slot/delete";
|
||||
}
|
||||
|
||||
@GetMapping("/slot/{id}/destroy")
|
||||
public String destroy(@PathVariable Integer id, Model model) {
|
||||
service.removeSlot(id);
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
@PostMapping("/slot/save")
|
||||
public String save(@Valid @ModelAttribute("bean") SlotBean bean, BindingResult bindingResult, Model model) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
return "/slot/item";
|
||||
}
|
||||
service.saveSlot(bean);
|
||||
return "redirect:/sheet";
|
||||
}
|
||||
}
|
||||
129
src/main/java/de/jottyfan/bico/modules/slot/SlotRepository.java
Normal file
129
src/main/java/de/jottyfan/bico/modules/slot/SlotRepository.java
Normal file
@@ -0,0 +1,129 @@
|
||||
package de.jottyfan.bico.modules.slot;
|
||||
|
||||
import static de.jottyfan.bico.db.Tables.T_LESSON;
|
||||
import static de.jottyfan.bico.db.Tables.T_SLOT;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.DeleteConditionStep;
|
||||
import org.jooq.InsertResultStep;
|
||||
import org.jooq.SelectConditionStep;
|
||||
import org.jooq.UpdateConditionStep;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import de.jottyfan.bico.db.tables.records.TLessonRecord;
|
||||
import de.jottyfan.bico.db.tables.records.TSlotRecord;
|
||||
import de.jottyfan.bico.modules.slot.model.SlotBean;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jotty
|
||||
*
|
||||
*/
|
||||
@Repository
|
||||
public class SlotRepository {
|
||||
private static final Logger LOGGER = LogManager.getLogger(SlotRepository.class);
|
||||
|
||||
@Autowired
|
||||
private DSLContext jooq;
|
||||
|
||||
/**
|
||||
* get the slot referenced by id
|
||||
*
|
||||
* @param id the pkSlot value
|
||||
* @return the slot or null
|
||||
*/
|
||||
public SlotBean getSlot(Integer id) {
|
||||
SelectConditionStep<TSlotRecord> sql = jooq
|
||||
// @formatter:off
|
||||
.selectFrom(T_SLOT)
|
||||
.where(T_SLOT.PK_SLOT.eq(id));
|
||||
// @formatter:on
|
||||
LOGGER.trace(sql);
|
||||
Iterator<TSlotRecord> i = sql.fetch().iterator();
|
||||
SlotBean bean = null;
|
||||
while (i.hasNext()) {
|
||||
TSlotRecord r = i.next();
|
||||
bean = new SlotBean();
|
||||
bean.setPkSlot(r.getPkSlot());
|
||||
bean.setSlotDay(r.getSlotDay());
|
||||
bean.setNote(r.getNote());
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
/**
|
||||
* add a slot and return is new ID
|
||||
*
|
||||
* @param slot the slot
|
||||
* @return the ID of the slot
|
||||
*/
|
||||
public Integer addSlot(SlotBean slot) {
|
||||
InsertResultStep<TSlotRecord> sql = jooq
|
||||
// @formatter:off
|
||||
.insertInto(T_SLOT,
|
||||
T_SLOT.SLOT_DAY,
|
||||
T_SLOT.NOTE)
|
||||
.values(slot.getSlotDay(), slot.getNote())
|
||||
.returning(T_SLOT.PK_SLOT);
|
||||
// @formatter:on
|
||||
LOGGER.trace(sql);
|
||||
return sql.fetchOne().getPkSlot();
|
||||
}
|
||||
|
||||
/**
|
||||
* update the slot
|
||||
*
|
||||
* @param slot the slot
|
||||
*/
|
||||
public void changeSlot(SlotBean slot) {
|
||||
UpdateConditionStep<TSlotRecord> sql = jooq
|
||||
// @formatter:off
|
||||
.update(T_SLOT)
|
||||
.set(T_SLOT.SLOT_DAY, slot.getSlotDay())
|
||||
.set(T_SLOT.NOTE, slot.getNote())
|
||||
.where(T_SLOT.PK_SLOT.eq(slot.getPkSlot()));
|
||||
// @formatter:on
|
||||
LOGGER.trace(sql);
|
||||
sql.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* remove slot
|
||||
*
|
||||
* @param id the ID of the slot
|
||||
*/
|
||||
public void deleteSlot(Integer id) {
|
||||
DeleteConditionStep<TSlotRecord> sql = jooq
|
||||
// @formatter:off
|
||||
.deleteFrom(T_SLOT)
|
||||
.where(T_SLOT.PK_SLOT.eq(id));
|
||||
// @formatter:on
|
||||
LOGGER.trace(sql);
|
||||
sql.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* get the slot only if it is not yet used by another table
|
||||
*
|
||||
* @param id the ID of the slot
|
||||
* @return the slot or null
|
||||
*/
|
||||
public SlotBean getSlotIfDeletable(Integer id) {
|
||||
SelectConditionStep<TLessonRecord> sql = jooq
|
||||
// @formatter:off
|
||||
.selectFrom(T_LESSON)
|
||||
.where(T_LESSON.FK_SLOT.eq(id));
|
||||
// @formatter:on
|
||||
LOGGER.trace(sql);
|
||||
if (sql.fetchOne() == null) {
|
||||
return getSlot(id);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
39
src/main/java/de/jottyfan/bico/modules/slot/SlotService.java
Normal file
39
src/main/java/de/jottyfan/bico/modules/slot/SlotService.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package de.jottyfan.bico.modules.slot;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import de.jottyfan.bico.modules.slot.model.SlotBean;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jotty
|
||||
*
|
||||
*/
|
||||
@Service
|
||||
public class SlotService {
|
||||
|
||||
@Autowired
|
||||
private SlotRepository repository;
|
||||
|
||||
public SlotBean loadSlot(Integer id) {
|
||||
return repository.getSlot(id);
|
||||
}
|
||||
|
||||
public Integer saveSlot(SlotBean slot) {
|
||||
if (slot.getPkSlot() == null) {
|
||||
return repository.addSlot(slot);
|
||||
} else {
|
||||
repository.changeSlot(slot);
|
||||
return slot.getPkSlot();
|
||||
}
|
||||
}
|
||||
|
||||
public void removeSlot(Integer id) {
|
||||
repository.deleteSlot(id);
|
||||
}
|
||||
|
||||
public SlotBean loadDeletableSlot(Integer id) {
|
||||
return repository.getSlotIfDeletable(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package de.jottyfan.bico.modules.slot.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jotty
|
||||
*
|
||||
*/
|
||||
// TODO: not exists validator that checks for another pkSlot not to have that slotDay
|
||||
public class SlotBean implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Integer pkSlot;
|
||||
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
@NotNull(message = "Das Datum muss ausgefüllt werden.")
|
||||
private LocalDate slotDay;
|
||||
|
||||
private String note;
|
||||
|
||||
public SlotBean withNote(String note) {
|
||||
this.note = note;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the pkSlot
|
||||
*/
|
||||
public Integer getPkSlot() {
|
||||
return pkSlot;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param pkSlot the pkSlot to set
|
||||
*/
|
||||
public void setPkSlot(Integer pkSlot) {
|
||||
this.pkSlot = pkSlot;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the slotDay
|
||||
*/
|
||||
public LocalDate getSlotDay() {
|
||||
return slotDay;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param slotDay the slotDay to set
|
||||
*/
|
||||
public void setSlotDay(LocalDate slotDay) {
|
||||
this.slotDay = slotDay;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the note
|
||||
*/
|
||||
public String getNote() {
|
||||
return note;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param note the note to set
|
||||
*/
|
||||
public void setNote(String note) {
|
||||
this.note = note;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user