package de.kreth.clubhelper.model.controller; import static de.kreth.clubhelperbackend.utils.BoolUtils.not; import static java.time.temporal.ChronoUnit.MINUTES; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.repository.CrudRepository; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; 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.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import de.kreth.clubhelper.model.dao.ClubhelperDao; import de.kreth.clubhelper.model.data.BaseEntity; /** * Default Controller implementing all functionality for all {@link Data} types. * * @param * Data type */ //@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'STAFF')") public abstract class AbstractController> implements ClubController { @Autowired protected D dao; private Class elementClass; public AbstractController(Class element) { super(); this.elementClass = element; } @Override @GetMapping(value = "/{id}", produces = "application/json") public T getById(@PathVariable("id") long id) { return dao.findById(id).orElseThrow(() -> new IllegalArgumentException(elementClass.getName() + " with id=" + id + " not found")); } protected List iterableToList(Iterable in) { List result = new ArrayList<>(); in.forEach(result::add); return result; } @Override @GetMapping(value = {"/", ""}, produces = "application/json") public List getAll() { Iterable findAll = dao.findAll(); List result = new ArrayList<>(); findAll.forEach(result::add); return result; } @Override @GetMapping(value = "/for/{id}", produces = "application/json") public List getByParentId(@PathVariable("id") long id) { if (dao instanceof ClubhelperDao) { ClubhelperDao specialDao = (ClubhelperDao) dao; return specialDao.findByPersonId(id); } return Collections.emptyList(); } @Override @GetMapping(value = "/changed/{changed}", produces = "application/json") public List getChangedSince(@PathVariable("changed") long changed) { if (dao instanceof ClubhelperDao) { ClubhelperDao specialDao = (ClubhelperDao) dao; return specialDao.findByChangedGreaterThan(new Date(changed)); } return Collections.emptyList(); } @Override @PutMapping(value = "/{id}", produces = "application/json") public T put(@PathVariable("id") long id, @RequestBody T toUpdate) { LocalDateTime now = LocalDateTime.now(); LocalDateTime created = toUpdate.getCreated(); LocalDateTime changed = null; if (toUpdate.getChanged() != null) { changed = toUpdate.getChanged(); long minutes = MINUTES.between(created, changed); if (minutes < 1) { toUpdate.setChanged(now); } } else { toUpdate.setChanged(now); } dao.save(toUpdate); return toUpdate; } @Override @DeleteMapping(value = "/{id}", produces = "application/json") public ResponseEntity delete(@PathVariable("id") long id) { T byId = getById(id); if (not(byId.isDeleted())) { dao.delete(byId); } return ResponseEntity.ok(getById(id)); } @Override @PostMapping(value = "/", produces = "application/json") public T post(@RequestBody T toCreate) { return post(-1L, toCreate); } @Override @PostMapping(value = "/{id}", produces = "application/json") public T post(@PathVariable("id") Long id, @RequestBody T toCreate) { if (id == null) { id = -1L; } toCreate.setId(id); LocalDateTime now = LocalDateTime.now(); toCreate.setChanged(now); if (toCreate.getCreated() == null) { toCreate.setCreated(now); } if (toCreate.getId() < 0) { return dao.save(toCreate); } else { T byId = getById(toCreate.getId()); if (byId != null) { toCreate.setDeleted(null); return dao.save(toCreate); } else { return dao.save(toCreate); } } } }