schedule-refactor #45
@ -1,26 +1,27 @@
|
||||
package core.api.controller;
|
||||
|
||||
import core.api.models.auth.SimpleStatusResponse;
|
||||
import core.api.models.timemanager.taskSchedule.*;
|
||||
import core.entities.User;
|
||||
import core.entities.timemanager.BasicTaskSchedule;
|
||||
import core.entities.timemanager.ScheduleType;
|
||||
import core.entities.timemanager.Task;
|
||||
import core.repositories.UserRepository;
|
||||
import core.repositories.timemanager.BasicTaskScheduleRepository;
|
||||
import core.services.*;
|
||||
|
||||
import core.entities.timemanager.AbstractSchedule;
|
||||
import core.services.TaskScheduleService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@CrossOrigin(origins = "*", maxAge = 3600)
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class ScheduleController {
|
||||
|
||||
@Autowired private TaskScheduleService taskScheduleService;
|
||||
@GetMapping("/schedules")
|
||||
public ResponseEntity<?> loadAllSchedulesOfUser() {
|
||||
String username = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||
List<AbstractSchedule> schedules = taskScheduleService.getAllSchedulesOfUser(username);
|
||||
|
||||
return ResponseEntity.ok(schedules);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,77 @@
|
||||
package core.entities.timemanager;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorColumn(name="scheduleType",
|
||||
discriminatorType = DiscriminatorType.INTEGER)
|
||||
public abstract class AbstractSchedule {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long scheduleID;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(referencedColumnName = "taskID", name = "task")
|
||||
private Task task;
|
||||
|
||||
private LocalDateTime startTime;
|
||||
private LocalDateTime stopTime;
|
||||
|
||||
public AbstractSchedule() {
|
||||
}
|
||||
|
||||
public AbstractSchedule(Task task, LocalDateTime startTime, LocalDateTime stopTime) {
|
||||
this.task = task;
|
||||
this.startTime = startTime;
|
||||
this.stopTime = stopTime;
|
||||
}
|
||||
|
||||
public long getScheduleID() {
|
||||
return scheduleID;
|
||||
}
|
||||
|
||||
public void setScheduleID(long scheduleID) {
|
||||
this.scheduleID = scheduleID;
|
||||
}
|
||||
|
||||
public Task getTask() {
|
||||
return task;
|
||||
}
|
||||
|
||||
public void setTask(Task task) {
|
||||
this.task = task;
|
||||
}
|
||||
|
||||
public LocalDateTime getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public void setStartTime(LocalDateTime startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getStopTime() {
|
||||
return stopTime;
|
||||
}
|
||||
|
||||
public void setStopTime(LocalDateTime stopTime) {
|
||||
this.stopTime = stopTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
AbstractSchedule that = (AbstractSchedule) o;
|
||||
return scheduleID == that.scheduleID && Objects.equals(task, that.task) && Objects.equals(startTime, that.startTime) && Objects.equals(stopTime, that.stopTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(scheduleID, task, startTime, stopTime);
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package core.entities.timemanager;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import java.time.LocalDateTime;
|
||||
@Entity
|
||||
@DiscriminatorValue("1")
|
||||
public class AdvancedTaskSchedule extends AbstractSchedule {
|
||||
|
||||
private LocalDateTime scheduleStart;
|
||||
private LocalDateTime scheduleEnd;
|
||||
}
|
@ -6,51 +6,17 @@ import java.time.LocalDateTime;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "basic_schedules")
|
||||
public class BasicTaskSchedule {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long scheduleID;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(referencedColumnName = "taskID", name = "task")
|
||||
private Task task;
|
||||
@DiscriminatorValue("0")
|
||||
public class BasicTaskSchedule extends AbstractSchedule{
|
||||
|
||||
private LocalDate scheduleDate;
|
||||
private LocalDateTime startTime;
|
||||
private LocalDateTime finishedTime;
|
||||
|
||||
public BasicTaskSchedule(Task task, LocalDate scheduleDate) {
|
||||
this.task = task;
|
||||
this.scheduleDate = scheduleDate;
|
||||
}
|
||||
|
||||
public BasicTaskSchedule() {
|
||||
}
|
||||
|
||||
public void start() {
|
||||
this.startTime = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public void end() {
|
||||
this.finishedTime = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public long getScheduleID() {
|
||||
return scheduleID;
|
||||
}
|
||||
|
||||
public void setScheduleID(long scheduleID) {
|
||||
this.scheduleID = scheduleID;
|
||||
}
|
||||
|
||||
public Task getTask() {
|
||||
return task;
|
||||
}
|
||||
|
||||
public void setTask(Task task) {
|
||||
this.task = task;
|
||||
public BasicTaskSchedule(Task task, LocalDateTime startTime, LocalDateTime stopTime, LocalDate scheduleDate) {
|
||||
super(task, startTime, stopTime);
|
||||
this.scheduleDate = scheduleDate;
|
||||
}
|
||||
|
||||
public LocalDate getScheduleDate() {
|
||||
@ -60,37 +26,4 @@ public class BasicTaskSchedule {
|
||||
public void setScheduleDate(LocalDate scheduleDate) {
|
||||
this.scheduleDate = scheduleDate;
|
||||
}
|
||||
|
||||
public LocalDateTime getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public void setStartTime(LocalDateTime startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getFinishedTime() {
|
||||
return finishedTime;
|
||||
}
|
||||
|
||||
public void setFinishedTime(LocalDateTime finishedTime) {
|
||||
this.finishedTime = finishedTime;
|
||||
}
|
||||
|
||||
public boolean isActivateAble() {
|
||||
return startTime == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
BasicTaskSchedule that = (BasicTaskSchedule) o;
|
||||
return scheduleID == that.scheduleID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(scheduleID);
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ public class Task {
|
||||
private boolean finished;
|
||||
|
||||
@OneToMany(mappedBy = "task", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
private List<BasicTaskSchedule> basicTaskSchedules;
|
||||
private List<AbstractSchedule> basicTaskSchedules;
|
||||
|
||||
private int workTime;
|
||||
|
||||
@ -117,27 +117,14 @@ public class Task {
|
||||
this.taskID = taskID;
|
||||
}
|
||||
|
||||
public List<BasicTaskSchedule> getBasicTaskSchedules() {
|
||||
public List<AbstractSchedule> getBasicTaskSchedules() {
|
||||
return basicTaskSchedules;
|
||||
}
|
||||
|
||||
public void setBasicTaskSchedules(List<BasicTaskSchedule> basicTaskSchedules) {
|
||||
public void setBasicTaskSchedules(List<AbstractSchedule> basicTaskSchedules) {
|
||||
this.basicTaskSchedules = basicTaskSchedules;
|
||||
}
|
||||
|
||||
public boolean hasActiveSchedule() {
|
||||
for(BasicTaskSchedule basicTaskSchedule : basicTaskSchedules) {
|
||||
if(basicTaskSchedule.getStartTime() != null && basicTaskSchedule.getFinishedTime() == null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void increaseActiveTime(int minutesSpent) {
|
||||
this.workTime += minutesSpent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
|
@ -1,19 +0,0 @@
|
||||
package core.repositories.timemanager;
|
||||
|
||||
import core.entities.User;
|
||||
import core.entities.timemanager.BasicTaskSchedule;
|
||||
import core.entities.timemanager.Task;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface BasicTaskScheduleRepository extends CrudRepository<BasicTaskSchedule, Long> {
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package core.repositories.timemanager;
|
||||
|
||||
import core.entities.timemanager.AbstractSchedule;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface ScheduleRepository extends CrudRepository<AbstractSchedule, Long> {
|
||||
|
||||
@Query(value = "SELECT s FROM AbstractSchedule s WHERE s.task.taskgroup.user.username = ?1")
|
||||
List<AbstractSchedule> findAllByUsername(String username);
|
||||
|
||||
}
|
@ -1,9 +1,22 @@
|
||||
package core.services;
|
||||
|
||||
import core.entities.User;
|
||||
import core.entities.timemanager.AbstractSchedule;
|
||||
import core.repositories.UserRepository;
|
||||
import core.repositories.timemanager.ScheduleRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class TaskScheduleService {
|
||||
|
||||
@Autowired private ScheduleRepository scheduleRepository;
|
||||
@Autowired private UserRepository userRepository;
|
||||
|
||||
public List<AbstractSchedule> getAllSchedulesOfUser(String username) {
|
||||
return scheduleRepository.findAllByUsername(username);
|
||||
}
|
||||
}
|
||||
|
@ -2,17 +2,14 @@ package core.services;
|
||||
|
||||
import core.api.models.timemanager.tasks.TaskFieldInfo;
|
||||
import core.api.models.timemanager.tasks.TaskScope;
|
||||
import core.entities.timemanager.BasicTaskSchedule;
|
||||
import core.entities.timemanager.Task;
|
||||
import core.entities.timemanager.Taskgroup;
|
||||
import core.repositories.timemanager.BasicTaskScheduleRepository;
|
||||
import core.repositories.timemanager.TaskRepository;
|
||||
import core.repositories.timemanager.TaskgroupRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
@ -93,7 +90,7 @@ public class TaskService {
|
||||
}
|
||||
|
||||
public void deleteTask(Task task) {
|
||||
taskScheduleService.deleteScheduleByTask(task);
|
||||
//taskScheduleService.deleteScheduleByTask(task);
|
||||
taskRepository.deleteByTaskID(task.getTaskID());
|
||||
}
|
||||
|
||||
@ -106,7 +103,7 @@ public class TaskService {
|
||||
task.finish();
|
||||
taskRepository.save(task);
|
||||
|
||||
List<BasicTaskSchedule> removedBasicTaskSchedules = new LinkedList<>();
|
||||
/*List<BasicTaskSchedule> removedBasicTaskSchedules = new LinkedList<>();
|
||||
for(BasicTaskSchedule basicTaskSchedule : task.getBasicTaskSchedules()) {
|
||||
if(basicTaskSchedule.getStartTime() == null) {
|
||||
removedBasicTaskSchedules.add(basicTaskSchedule);
|
||||
@ -120,7 +117,7 @@ public class TaskService {
|
||||
task.getBasicTaskSchedules().remove(deletedTaskSchedule);
|
||||
taskRepository.save(task);
|
||||
taskScheduleService.deleteBasicSchedule(deletedTaskSchedule);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
public List<Task> loadAllTasks(String username, TaskScope scope) {
|
||||
|
@ -0,0 +1,35 @@
|
||||
package core.schedules;
|
||||
|
||||
import core.entities.User;
|
||||
import core.repositories.timemanager.ScheduleRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.test.context.jdbc.SqlGroup;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@DataJpaTest
|
||||
public class ScheduleRepositoryTest {
|
||||
|
||||
@Autowired private ScheduleRepository scheduleRepository;
|
||||
@Autowired private TestEntityManager entityManager;
|
||||
@Test
|
||||
@SqlGroup({
|
||||
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
|
||||
@Sql("classpath:taskRepositoryEntries.sql"),
|
||||
@Sql("classpath:basicScheduleEntries.sql")
|
||||
})
|
||||
void getAllSchedulesOfUser() {
|
||||
User referenceUser_1 = entityManager.find(User.class, 1L);
|
||||
User referenceUser_2 = entityManager.find(User.class, 2L);
|
||||
|
||||
assertEquals(0, scheduleRepository.findAllByUsername(referenceUser_2.getUsername()).size());
|
||||
assertEquals(2, scheduleRepository.findAllByUsername(referenceUser_1.getUsername()).size());
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package core.schedules;
|
||||
|
||||
import core.entities.timemanager.AbstractSchedule;
|
||||
import core.repositories.timemanager.TaskgroupRepository;
|
||||
import core.services.TaskScheduleService;
|
||||
import core.services.TaskgroupService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.test.context.jdbc.SqlGroup;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.hibernate.validator.internal.util.Contracts.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@SpringBootTest
|
||||
@Transactional
|
||||
public class ScheduleServiceTest {
|
||||
|
||||
|
||||
@Autowired
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Autowired private TaskScheduleService taskScheduleService;
|
||||
|
||||
private static final String username = "Testuser1";
|
||||
private static final String username2 = "Testuser2";
|
||||
|
||||
@Test
|
||||
@SqlGroup({
|
||||
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
|
||||
@Sql("classpath:taskRepositoryEntries.sql"),
|
||||
@Sql("classpath:basicScheduleEntries.sql")
|
||||
})
|
||||
void getAllSchedulesOfUser() {
|
||||
assertEquals(0, taskScheduleService.getAllSchedulesOfUser(username2).size());
|
||||
|
||||
List<AbstractSchedule> result_1 = taskScheduleService.getAllSchedulesOfUser(username);
|
||||
assertEquals(2, result_1.size());
|
||||
assertTrue(result_1.contains(entityManager.find(AbstractSchedule.class, 1L)));
|
||||
assertTrue(result_1.contains(entityManager.find(AbstractSchedule.class, 2L)));
|
||||
}
|
||||
}
|
3
backend/src/test/resources/basicScheduleEntries.sql
Normal file
3
backend/src/test/resources/basicScheduleEntries.sql
Normal file
@ -0,0 +1,3 @@
|
||||
INSERT INTO abstract_schedule (schedule_type, scheduleid, start_time, stop_time, schedule_date, task, schedule_end, schedule_start)
|
||||
VALUES (0, 1, null, null, '2023-11-11', 1, null, null),
|
||||
(0, 2, null, null, '2023-11-11', 2, null, null);
|
Loading…
Reference in New Issue
Block a user