Skip to content

Commit 6d4ee46

Browse files
author
Hattinger04
committed
adding / correcting function
1 parent 8587c71 commit 6d4ee46

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

src/main/java/io/github/Hattinger04/course/model/CourseService.java

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import io.github.Hattinger04.course.model.course.CourseRepository;
99
import io.github.Hattinger04.course.model.exercise.Exercise;
1010
import io.github.Hattinger04.course.model.exercise.ExerciseRepository;
11+
import io.github.Hattinger04.course.model.solution.Solution;
1112
import io.github.Hattinger04.course.model.solution.SolutionRepository;
1213
import io.github.Hattinger04.user.model.User;
13-
import io.github.Hattinger04.user.model.UserRepository;
1414

1515
public class CourseService {
1616

@@ -30,8 +30,7 @@ public CourseService(CourseRepository courseRepository, ExerciseRepository exerc
3030

3131
public Course createCourse(String name) {
3232
Course course = new Course(name);
33-
courseRepository.save(course);
34-
return course;
33+
return courseRepository.save(course);
3534
}
3635

3736
public void deleteCourse(String name) {
@@ -46,6 +45,7 @@ public Course getCourseByName(String name) {
4645
return courseRepository.findByName(name);
4746
}
4847

48+
// TODO: working with student / teacher table
4949
public void addStudentToCourse(Course course, User student) {
5050

5151
}
@@ -62,21 +62,53 @@ public void removeStudentsFromCourse(Course course, Set<User> student) {
6262

6363
}
6464

65-
public Exercise getExerciseByID(int id) {
66-
return exerciseRepository.findById(id);
67-
}
68-
65+
6966
// wont work like that ofc
7067
public Exercise createExercise(Integer course_id, String name) {
7168
Exercise exercise = new Exercise(course_id, name);
72-
exerciseRepository.save(exercise);
73-
return exercise;
69+
return exerciseRepository.save(exercise);
70+
}
71+
72+
public Exercise getExerciseByID(int id) {
73+
return exerciseRepository.findById(id);
74+
}
75+
/**
76+
* Get exercise by course_id and course name
77+
*
78+
* @param course_id
79+
* @param name
80+
* @return
81+
*/
82+
public Exercise getExerciseByCourse(int course_id, String name) {
83+
return exerciseRepository.findByCourse(course_id, name);
7484
}
7585

7686
public void deleteExercise(Integer course_id, String name) {
7787
exerciseRepository.delete(exerciseRepository.findByCourse(course_id, name));
7888
}
7989

80-
// TODO: students submitting solution
90+
91+
public Solution createSolution(int exercise_id, String text) {
92+
Solution solution = new Solution(exercise_id, text);
93+
return solutionRepository.save(solution);
94+
}
95+
96+
public Solution getSolutionByID(int id) {
97+
return solutionRepository.findById(id);
98+
}
99+
/**
100+
* Get solution by exercise_id and exercise name
101+
*
102+
* @param exercise_id
103+
* @param name
104+
* @return
105+
*/
106+
public Solution getSolutionByExercise(int exercise_id, String name) {
107+
return solutionRepository.findByExercise(exercise_id, name);
108+
}
109+
110+
public void deleteSolution(int id) {
111+
solutionRepository.delete(solutionRepository.findById(id));
112+
}
81113
// TODO: teacher correcting students work
82114
}

src/main/java/io/github/Hattinger04/course/model/exercise/ExerciseRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ public interface ExerciseRepository extends JpaRepository<Exercise, Long>{
1010

1111
Exercise findById(int id);
1212
// TODO: SQL not tested yet!
13-
@Query(value = "SELECT * FROM EXERCISE e JOIN course c USING (course_id) where course_id=:course_id and name=:name", nativeQuery = true)
13+
@Query(value = "SELECT * FROM EXERCISE e JOIN course c USING (course_id) where course_id=:course_id and c.name=:name", nativeQuery = true)
1414
Exercise findByCourse(@Param("course_id") int course_id, @Param("name")String name);
1515
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package io.github.Hattinger04.course.model.solution;
22

33
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.data.jpa.repository.Query;
5+
import org.springframework.data.repository.query.Param;
46
import org.springframework.stereotype.Repository;
57

68
@Repository
79
public interface SolutionRepository extends JpaRepository<Solution, Long>{
810

911
Solution findById(int id);
12+
// TODO: SQL not tested yet!
13+
@Query(value = "SELECT * FROM SOLUTION s JOIN exercise e USING (exercise_id) where exercise_id=:exercise_id and e.name=:name", nativeQuery = true)
14+
Solution findByExercise(@Param("exercise_id")int exercise_id, @Param("name")String name);
1015
}

0 commit comments

Comments
 (0)