Skip to content

Commit 8587c71

Browse files
author
Hattinger04
committed
changing structure again + starting database sql, ...
1 parent 881f2da commit 8587c71

File tree

8 files changed

+122
-55
lines changed

8 files changed

+122
-55
lines changed

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

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,79 @@
22

33
import java.util.Set;
44

5+
import org.springframework.beans.factory.annotation.Autowired;
6+
7+
import io.github.Hattinger04.course.model.course.Course;
8+
import io.github.Hattinger04.course.model.course.CourseRepository;
9+
import io.github.Hattinger04.course.model.exercise.Exercise;
10+
import io.github.Hattinger04.course.model.exercise.ExerciseRepository;
11+
import io.github.Hattinger04.course.model.solution.SolutionRepository;
512
import io.github.Hattinger04.user.model.User;
13+
import io.github.Hattinger04.user.model.UserRepository;
614

715
public class CourseService {
816

17+
// TODO: Nothing(!) tested yet
18+
19+
private CourseRepository courseRepository;
20+
private ExerciseRepository exerciseRepository;
21+
private SolutionRepository solutionRepository;
22+
23+
@Autowired
24+
public CourseService(CourseRepository courseRepository, ExerciseRepository exerciseRepository, SolutionRepository solutionRepository) {
25+
this.courseRepository = courseRepository;
26+
this.exerciseRepository = exerciseRepository;
27+
this.solutionRepository = solutionRepository;
28+
}
29+
30+
931
public Course createCourse(String name) {
10-
// TODO: implementation in db
11-
return new Course(name);
32+
Course course = new Course(name);
33+
courseRepository.save(course);
34+
return course;
1235
}
1336

1437
public void deleteCourse(String name) {
15-
// TODO: implementation in db
38+
courseRepository.delete(courseRepository.findByName(name));
1639
}
1740

1841
public Course getCourseByID(int id) {
19-
// TODO: get course by id
20-
return null;
42+
return courseRepository.findById(id);
2143
}
2244

2345
public Course getCourseByName(String name) {
24-
// TODO: get course by name
25-
return null;
46+
return courseRepository.findByName(name);
2647
}
2748

2849
public void addStudentToCourse(Course course, User student) {
29-
course.getStudents().add(student); // Not sure if this works! - implementation in db!
50+
3051
}
3152

3253
public void addStudentsToCourse(Course course, Set<User> student) {
33-
course.getStudents().addAll(student); // Not sure if this works! - implementation in db!
54+
3455
}
3556

3657
public void removeStudentFromCourse(Course course, User student) {
37-
course.getStudents().remove(student); // Not sure if this works! - implementation in db!
3858

3959
}
4060

4161
public void removeStudentsFromCourse(Course course, Set<User> student) {
42-
course.getStudents().removeAll(student); // Not sure if this works! - implementation in db!
62+
4363
}
4464

65+
public Exercise getExerciseByID(int id) {
66+
return exerciseRepository.findById(id);
67+
}
68+
4569
// wont work like that ofc
46-
public Exercise createExercise(String name) {
47-
// TODO: implementation in db
48-
return new Exercise(name);
70+
public Exercise createExercise(Integer course_id, String name) {
71+
Exercise exercise = new Exercise(course_id, name);
72+
exerciseRepository.save(exercise);
73+
return exercise;
4974
}
5075

51-
public void deleteExercise(String name) {
52-
// TODO: implementation in db
76+
public void deleteExercise(Integer course_id, String name) {
77+
exerciseRepository.delete(exerciseRepository.findByCourse(course_id, name));
5378
}
5479

5580
// TODO: students submitting solution

src/main/java/io/github/Hattinger04/course/model/Course.java renamed to src/main/java/io/github/Hattinger04/course/model/course/Course.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.Hattinger04.course.model;
1+
package io.github.Hattinger04.course.model.course;
22

33
import java.util.Set;
44

@@ -19,6 +19,10 @@
1919
//@Table(name = "course")
2020
@AllArgsConstructor
2121
public class Course {
22+
23+
public Course(String name) {
24+
this.name = name;
25+
}
2226

2327
// @Id
2428
// @GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -28,15 +32,12 @@ public class Course {
2832

2933
@Getter @Setter
3034
private String name; // TODO: unique
31-
32-
@Getter @Setter
33-
private Set<User> teacher; // in case of multiple teacher in one group
34-
35-
@Getter @Setter
36-
private Set<User> students;
37-
38-
public Course(String name) {
39-
this.name = name;
40-
}
41-
35+
36+
/*
37+
Not tested!
38+
Connection between course and user (n - m solution)
39+
@ManyToMany(cascade = CascadeType.MERGE)
40+
@JoinTable(name = "user_course", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "course_id"))
41+
private Set<Course> courses;
42+
*/
4243
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.github.Hattinger04.course.model.course;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
@Repository
7+
public interface CourseRepository extends JpaRepository<Course, Long>{
8+
9+
Course findById(int id);
10+
Course findByName(String name);
11+
}

src/main/java/io/github/Hattinger04/course/model/Exercise.java renamed to src/main/java/io/github/Hattinger04/course/model/exercise/Exercise.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.Hattinger04.course.model;
1+
package io.github.Hattinger04.course.model.exercise;
22

33
import javax.persistence.Entity;
44
import javax.persistence.Table;
@@ -12,7 +12,8 @@
1212
@AllArgsConstructor
1313
public class Exercise {
1414

15-
public Exercise(String name) {
15+
public Exercise(Integer course_id, String name) {
16+
this.course_id = course_id;
1617
this.name = name;
1718
}
1819

@@ -26,20 +27,6 @@ public Exercise(String name) {
2627
private String text;
2728

2829
@Getter @Setter
29-
private Integer course_id;
30-
31-
@Getter @Setter
32-
private Integer teacher_id;
33-
34-
@Getter @Setter
35-
private Integer student_id;
36-
37-
/* TODO: best solution?
38-
* @Getter @Setter
39-
private Boolean finished;
40-
41-
@Getter @Setter
42-
private String answer;
43-
44-
*/
30+
private Integer course_id; // TODO: FK
31+
4532
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.github.Hattinger04.course.model.exercise;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.data.jpa.repository.Query;
5+
import org.springframework.data.repository.query.Param;
6+
import org.springframework.stereotype.Repository;
7+
8+
@Repository
9+
public interface ExerciseRepository extends JpaRepository<Exercise, Long>{
10+
11+
Exercise findById(int id);
12+
// 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)
14+
Exercise findByCourse(@Param("course_id") int course_id, @Param("name")String name);
15+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.github.Hattinger04.course.model.solution;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
//@Entity
8+
//@Table(name = "Solution")
9+
@AllArgsConstructor
10+
public class Solution {
11+
12+
public Solution(Integer exercise_id, String text) {
13+
this.exercise_id = exercise_id;
14+
this.text = text;
15+
}
16+
17+
@Getter @Setter
18+
private Integer id; // TODO: primary_key
19+
20+
@Getter @Setter
21+
private String text;
22+
23+
@Getter @Setter
24+
private Integer exercise_id; // TODO: FK
25+
26+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.github.Hattinger04.course.model.solution;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
@Repository
7+
public interface SolutionRepository extends JpaRepository<Solution, Long>{
8+
9+
Solution findById(int id);
10+
}

src/main/java/io/github/Hattinger04/user/model/User.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import org.hibernate.validator.constraints.Length;
1818

19-
import io.github.Hattinger04.course.model.Course;
2019
import io.github.Hattinger04.role.Role;
2120
import lombok.AllArgsConstructor;
2221
import lombok.Builder;
@@ -50,12 +49,5 @@ public class User {
5049
@ManyToMany(cascade = CascadeType.MERGE)
5150
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
5251
private Set<Role> roles;
53-
54-
/*
55-
Not tested!
56-
Connection between course and user (n - m solution)
57-
@ManyToMany(cascade = CascadeType.MERGE)
58-
@JoinTable(name = "user_course", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "course_id"))
59-
private Set<Course> courses;
60-
*/
52+
6153
}

0 commit comments

Comments
 (0)