This commit is contained in:
Jottyfan
2023-11-28 22:44:00 +01:00
parent 48525b7e20
commit 690f28f4fe
15 changed files with 300 additions and 35 deletions

View File

@ -0,0 +1,45 @@
package de.jottyfan.bico.modules.lesson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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 org.springframework.web.bind.annotation.RequestParam;
import de.jottyfan.bico.db.tables.records.TLessonRecord;
import de.jottyfan.bico.modules.CommonController;
/**
*
* @author jotty
*
*/
@Controller
public class LessonController extends CommonController {
@Autowired
private LessonService service;
@GetMapping("/lesson")
public String getPerson(@RequestParam("slotId") Integer slotId, Model model) {
model.addAttribute("bean", service.getLesson(slotId, true));
model.addAttribute("persons", service.getPersons());
model.addAttribute("slotDay", service.getSlotDay(slotId));
return "/lesson/item";
}
@PostMapping("/lesson/{id}")
public String updateLesson(@PathVariable("id") Integer lessonId, @ModelAttribute("bean") TLessonRecord bean, Model model) {
bean.setPkLesson(lessonId);
service.updateLesson(bean);
return "redirect:/sheet";
}
@GetMapping("/lesson/{id}/remove")
public String removeLesson(@PathVariable("id") Integer lessonId) {
service.removeLesson(lessonId);
return "redirect:/sheet";
}
}

View File

@ -0,0 +1,100 @@
package de.jottyfan.bico.modules.lesson;
import static de.jottyfan.bico.db.Tables.T_LESSON;
import static de.jottyfan.bico.db.Tables.T_PERSON;
import static de.jottyfan.bico.db.Tables.T_SLOT;
import java.time.LocalDate;
import java.util.List;
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.Record1;
import org.jooq.SelectConditionStep;
import org.jooq.SelectWhereStep;
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.TPersonRecord;
/**
*
* @author jotty
*
*/
@Repository
public class LessonRepository {
private final static Logger LOGGER = LogManager.getLogger(LessonRepository.class);
@Autowired
private DSLContext jooq;
public TLessonRecord getLesson(Integer slotId, Boolean createIfNecessary) {
SelectConditionStep<TLessonRecord> sql = jooq
// @formatter:off
.selectFrom(T_LESSON)
.where(T_LESSON.FK_SLOT.eq(slotId));
// @formatter:on
LOGGER.trace(sql);
TLessonRecord r = sql.fetchOne();
if (r == null && createIfNecessary) {
InsertResultStep<TLessonRecord> sql2 = jooq
// @formatter:off
.insertInto(T_LESSON,
T_LESSON.FK_SLOT)
.values(slotId)
.returning(T_LESSON.PK_LESSON);
// @formatter:on
LOGGER.trace(sql);
Integer pkLesson = sql2.fetchOne(T_LESSON.PK_LESSON);
r = new TLessonRecord();
r.setPkLesson(pkLesson);
r.setFkSlot(slotId);
}
return r;
}
public List<TPersonRecord> getPersons() {
SelectWhereStep<TPersonRecord> sql = jooq.selectFrom(T_PERSON);
LOGGER.trace(sql);
return sql.fetchStream().toList();
}
public LocalDate getSlotDay(Integer slotId) {
SelectConditionStep<Record1<LocalDate>> sql = jooq
// @formatter:off
.select(T_SLOT.SLOT_DAY)
.from(T_SLOT)
.where(T_SLOT.PK_SLOT.eq(slotId));
// @formatter:on
LOGGER.trace(sql);
return sql.fetchOne(T_SLOT.SLOT_DAY);
}
public void updateLesson(TLessonRecord bean) {
UpdateConditionStep<TLessonRecord> sql = jooq
// @formatter:off
.update(T_LESSON)
.set(T_LESSON.NOTES, bean.getNotes())
.set(T_LESSON.FK_PERSON, bean.getFkPerson())
.where(T_LESSON.PK_LESSON.eq(bean.getPkLesson()));
// @formatter:on
LOGGER.trace(sql);
sql.execute();
}
public void removeLesson(Integer lessonId) {
DeleteConditionStep<TLessonRecord> sql = jooq
// @formatter:off
.deleteFrom(T_LESSON)
.where(T_LESSON.PK_LESSON.eq(lessonId));
// @formatter:on
LOGGER.trace(sql);
sql.execute();
}
}

View File

@ -0,0 +1,42 @@
package de.jottyfan.bico.modules.lesson;
import java.time.LocalDate;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import de.jottyfan.bico.db.tables.records.TLessonRecord;
import de.jottyfan.bico.db.tables.records.TPersonRecord;
/**
*
* @author jotty
*
*/
@Service
public class LessonService {
@Autowired
private LessonRepository repository;
public TLessonRecord getLesson(Integer slotId, Boolean createIfNecessary) {
return repository.getLesson(slotId, createIfNecessary);
}
public List<TPersonRecord> getPersons() {
return repository.getPersons();
}
public LocalDate getSlotDay(Integer slotId) {
return repository.getSlotDay(slotId);
}
public void updateLesson(TLessonRecord bean) {
repository.updateLesson(bean);
}
public void removeLesson(Integer lessonId) {
repository.removeLesson(lessonId);
}
}

View File

@ -32,6 +32,7 @@ public class SlotController extends CommonController {
@GetMapping("/slot/{id}")
public String load(@PathVariable Integer id, Model model) {
model.addAttribute("bean", id == null ? new SlotBean() : service.loadSlot(id));
model.addAttribute("hasLesson", service.slotHasLesson(id));
return "/slot/item";
}

View File

@ -126,4 +126,20 @@ public class SlotRepository {
return null;
}
}
/**
* return true if this slot already has at least one lesson
*
* @param slotId the ID of the slot
* @return true or false
*/
public Boolean getHasLesson(Integer slotId) {
SelectConditionStep<TLessonRecord> sql = jooq
// @formatter:off
.selectFrom(T_LESSON)
.where(T_LESSON.FK_SLOT.eq(slotId));
// @formatter:on
LOGGER.trace(sql);
return sql.fetch().size() > 0;
}
}

View File

@ -36,4 +36,8 @@ public class SlotService {
public SlotBean loadDeletableSlot(Integer id) {
return repository.getSlotIfDeletable(id);
}
public Boolean slotHasLesson(Integer id) {
return id == null ? false : repository.getHasLesson(id);
}
}