Skip to content

Commit cfb1d80

Browse files
author
Hattinger04
committed
writing tremendously better deserialize
1 parent 69ab97b commit cfb1d80

File tree

8 files changed

+51
-77
lines changed

8 files changed

+51
-77
lines changed

src/main/java/io/github/Hattinger04/RestServices.java

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.fasterxml.jackson.core.JsonProcessingException;
66
import com.fasterxml.jackson.databind.ObjectMapper;
77

8-
import io.github.Hattinger04.hamster.model.Hamster;
98
import io.github.Hattinger04.user.model.User;
109

1110
@Service
@@ -14,45 +13,18 @@ public class RestServices {
1413
private ObjectMapper objectMapper = new ObjectMapper();
1514

1615
/**
17-
* Convert JSON-String to User object
16+
* Convert JSON-String to any wanted object
1817
*
1918
* @param json
2019
* @return
21-
*/
22-
public User deserializeUser(String json) {
20+
*/
21+
public Object deserialize(Class<?> c, String json) {
2322
try {
24-
return objectMapper.readValue(json, User.class);
23+
System.out.println(c == User.class);
24+
return objectMapper.readValue(json, c);
2525
} catch (JsonProcessingException e) {
26-
return null;
27-
}
28-
}
29-
30-
/**
31-
* Convert JSON-String to Hamster object
32-
*
33-
* @param json
34-
* @return
35-
*/
36-
public Hamster deserializeHamster(String json) {
37-
try {
38-
return objectMapper.readValue(json, Hamster.class);
39-
} catch (JsonProcessingException e) {
40-
return null;
41-
}
42-
}
43-
44-
/**
45-
* Convert JSON-String to UserRole object
46-
*
47-
* @param json
48-
* @return
49-
*/
50-
public UserRole deserializeUserRole(String json) {
51-
try {
52-
return objectMapper.readValue(json, UserRole.class);
53-
} catch (JsonProcessingException e) {
54-
return null;
55-
}
26+
return null;
27+
}
5628
}
5729

5830
}

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public CourseService(CourseRepository courseRepository, ExerciseRepository exerc
2828
}
2929

3030

31-
public Course createCourse(String name) {
32-
Course course = new Course(name);
33-
return courseRepository.save(course);
34-
}
35-
36-
public void deleteCourse(String name) {
37-
courseRepository.delete(courseRepository.findByName(name));
38-
}
31+
// public Course createCourse(String name) {
32+
// Course course = new Course(name);
33+
// return courseRepository.save(course);
34+
// }
35+
//
36+
// public void deleteCourse(String name) {
37+
// courseRepository.delete(courseRepository.findByName(name));
38+
// }
3939

4040
public Course getCourseByID(int id) {
4141
return courseRepository.findById(id);
@@ -64,10 +64,10 @@ public void removeStudentsFromCourse(Course course, Set<User> student) {
6464

6565

6666
// wont work like that ofc
67-
public Exercise createExercise(Integer course_id, String name) {
68-
Exercise exercise = new Exercise(course_id, name);
69-
return exerciseRepository.save(exercise);
70-
}
67+
// public Exercise createExercise(Integer course_id, String name) {
68+
// Exercise exercise = new Exercise(course_id, name);
69+
// return exerciseRepository.save(exercise);
70+
// }
7171

7272
public Exercise getExerciseByID(int id) {
7373
return exerciseRepository.findById(id);
@@ -83,15 +83,15 @@ public Exercise getExerciseByCourse(int course_id, String name) {
8383
return exerciseRepository.findByCourse(course_id, name);
8484
}
8585

86-
public void deleteExercise(Integer course_id, String name) {
87-
exerciseRepository.delete(exerciseRepository.findByCourse(course_id, name));
88-
}
89-
90-
91-
public Solution createSolution(int exercise_id, String text) {
92-
Solution solution = new Solution(exercise_id, text);
93-
return solutionRepository.save(solution);
94-
}
86+
// public void deleteExercise(Integer course_id, String name) {
87+
// exerciseRepository.delete(exerciseRepository.findByCourse(course_id, name));
88+
// }
89+
//
90+
//
91+
// public Solution createSolution(int exercise_id, String text) {
92+
// Solution solution = new Solution(exercise_id, text);
93+
// return solutionRepository.save(solution);
94+
// }
9595

9696
public Solution getSolutionByID(int id) {
9797
return solutionRepository.findById(id);
@@ -107,8 +107,8 @@ public Solution getSolutionByExercise(int exercise_id, String name) {
107107
return solutionRepository.findByExercise(exercise_id, name);
108108
}
109109

110-
public void deleteSolution(int id) {
111-
solutionRepository.delete(solutionRepository.findById(id));
112-
}
110+
// public void deleteSolution(int id) {
111+
// solutionRepository.delete(solutionRepository.findById(id));
112+
// }
113113
// TODO: teacher correcting students work
114114
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import org.springframework.data.jpa.repository.JpaRepository;
44
import org.springframework.stereotype.Repository;
55

6-
@Repository
7-
public interface CourseRepository extends JpaRepository<Course, Long>{
8-
6+
//@Repository
7+
//public interface CourseRepository extends JpaRepository<Course, Long>{
8+
public interface CourseRepository {
99
Course findById(int id);
1010
Course findByName(String name);
1111
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import org.springframework.data.repository.query.Param;
66
import org.springframework.stereotype.Repository;
77

8-
@Repository
9-
public interface ExerciseRepository extends JpaRepository<Exercise, Long>{
10-
8+
//@Repository
9+
//public interface ExerciseRepository extends JpaRepository<Exercise, Long>{
10+
public interface ExerciseRepository {
1111
Exercise findById(int id);
1212
// TODO: SQL not tested yet!
1313
@Query(value = "SELECT * FROM EXERCISE e JOIN course c USING (course_id) where course_id=:course_id and c.name=:name", nativeQuery = true)

src/main/java/io/github/Hattinger04/course/model/solution/SolutionRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import org.springframework.data.repository.query.Param;
66
import org.springframework.stereotype.Repository;
77

8-
@Repository
9-
public interface SolutionRepository extends JpaRepository<Solution, Long>{
10-
8+
//@Repository
9+
//public interface SolutionRepository extends JpaRepository<Solution, Long>{
10+
public interface SolutionRepository {
1111
Solution findById(int id);
1212
// TODO: SQL not tested yet!
1313
@Query(value = "SELECT * FROM SOLUTION s JOIN exercise e USING (exercise_id) where exercise_id=:exercise_id and e.name=:name", nativeQuery = true)

src/main/java/io/github/Hattinger04/hamster/HamsterController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private boolean writeTextToFile(File file, String program) {
6262
@PostMapping("/defaultTerrain")
6363
@ResponseBody
6464
public ResponseEntity<?> defaultTerrain(@RequestBody String json) {
65-
Hamster hamster = restServices.deserializeHamster(json);
65+
Hamster hamster = (Hamster) restServices.deserialize(Hamster.class, json);
6666
String path = String.format("src/main/resources/hamster/%s/%s/%s.ham", SecurityContextHolder.getContext().getAuthentication().getName(), hamster.getProgramName(), hamster.getProgramName());
6767
createNewFile(path);
6868
writeTextToFile(new File(path), hamster.getProgram());
@@ -75,7 +75,7 @@ public ResponseEntity<?> defaultTerrain(@RequestBody String json) {
7575
@PostMapping("/existingTerrain")
7676
@ResponseBody
7777
public ResponseEntity<?> exisitingTerrain(@RequestBody String json) {
78-
Hamster hamster = restServices.deserializeHamster(json);
78+
Hamster hamster = (Hamster) restServices.deserialize(Hamster.class, json);
7979
String hamsterPath = String.format("src/main/resources/hamster/%s/%s/%s.ham", SecurityContextHolder.getContext().getAuthentication().getName(), hamster.getProgramName(), hamster.getProgramName());
8080
String terrainPath = String.format("src/main/resources/hamster/%s/%s/%s.ter", SecurityContextHolder.getContext().getAuthentication().getName(), hamster.getProgramName(), hamster.getTerrainName());
8181
createNewFile(hamsterPath);
@@ -90,7 +90,7 @@ public ResponseEntity<?> exisitingTerrain(@RequestBody String json) {
9090
@PostMapping("/newTerrain")
9191
@ResponseBody
9292
public ResponseEntity<?> newTerrain(@RequestBody String json) {
93-
Hamster hamster = restServices.deserializeHamster(json);
93+
Hamster hamster = (Hamster) restServices.deserialize(Hamster.class, json);
9494
String hamsterPath = String.format("src/main/resources/hamster/%s/%s/%s.ham", SecurityContextHolder.getContext().getAuthentication().getName(), hamster.getProgramName(), hamster.getProgramName());
9595
String terrainPath = String.format("src/main/resources/hamster/%s/%s/%s.ter", SecurityContextHolder.getContext().getAuthentication().getName(), hamster.getProgramName(), hamster.getTerrainName());
9696
createNewFile(hamsterPath);

src/main/java/io/github/Hattinger04/user/UserController.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public ResponseEntity<?> getAllUsers() {
5353
@PostMapping("/createUser")
5454
@ResponseBody
5555
public ResponseEntity<?> createUser(@RequestBody String json) {
56-
User user = restServices.deserializeUser(json);
56+
User user = (User) restServices.deserialize(User.class, json);
5757
if(user == null) {
5858
return new ResponseEntity<>("Could not save user -> wrong data", HttpStatus.BAD_REQUEST);
5959
}
@@ -72,7 +72,7 @@ public ResponseEntity<?> createUser(@RequestBody String json) {
7272
@PostMapping("/updateUser")
7373
@PreAuthorize("hasAuthority('DEV')")
7474
public ResponseEntity<?> updateUser(@RequestBody String json) {
75-
User user = restServices.deserializeUser(json);
75+
User user = (User) restServices.deserialize(User.class, json);
7676
if(userService.updateUser(user)) {
7777
return new ResponseEntity<>("Could not update user!", HttpStatus.NOT_FOUND);
7878
}
@@ -89,7 +89,7 @@ public ResponseEntity<?> updateUser(@RequestBody String json) {
8989
@PostMapping("/deleteUser")
9090
@PreAuthorize("hasAuthority('DEV')")
9191
public ResponseEntity<?> deleteUser(@RequestBody String json) {
92-
UserRole user = restServices.deserializeUserRole(json);
92+
UserRole user = (UserRole) restServices.deserialize(UserRole.class, json);
9393
if(!userService.deleteUser(user.getUser_id())) {
9494
return new ResponseEntity<>("Could not delete user!", HttpStatus.NOT_FOUND);
9595
}
@@ -107,7 +107,7 @@ public ResponseEntity<?> deleteUser(@RequestBody String json) {
107107
@PostMapping("/addRole")
108108
@PreAuthorize("hasAuthority('DEV')")
109109
public ResponseEntity<?> addRole(@RequestBody String json) {
110-
UserRole userRole = restServices.deserializeUserRole(json);
110+
UserRole userRole = (UserRole) restServices.deserialize(UserRole.class, json);
111111
if(!userService.insertUserRole(userRole.getUser_id(), userRole.getRole_id())) {
112112
return new ResponseEntity<>("Could not insert new Role!", HttpStatus.NOT_FOUND);
113113
}
@@ -124,7 +124,7 @@ public ResponseEntity<?> addRole(@RequestBody String json) {
124124
@PostMapping("/removeRole")
125125
@PreAuthorize("hasAuthority('DEV')")
126126
public ResponseEntity<?> removeRole(@RequestBody String json) {
127-
UserRole userRole = restServices.deserializeUserRole(json);
127+
UserRole userRole = (UserRole) restServices.deserialize(UserRole.class, json);
128128
if(!userService.removeUserRole(userRole.getUser_id(), userRole.getRole_id())) {
129129
return new ResponseEntity<>("Could not remove Role!", HttpStatus.NOT_FOUND);
130130
}
@@ -142,7 +142,7 @@ public ResponseEntity<?> removeRole(@RequestBody String json) {
142142
@PostMapping("/getUser")
143143
@PreAuthorize("hasAuthority('DEV')")
144144
public ResponseEntity<?> getUser(@RequestBody String json) {
145-
User user= userService.findUserByUsername(restServices.deserializeUser(json).getUsername());
145+
User user = userService.findUserByUsername(((User) (restServices.deserialize(User.class, json))).getUsername());
146146
if(user == null) {
147147
return new ResponseEntity<>("Couldn't find user!", HttpStatus.NOT_FOUND);
148148
}

src/main/resources/allLogs.log

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,5 @@ Juli 09, 2022 1:12:57 PM io.github.Hattinger04.aop.LogAspect loginLog
292292
FEIN: User(id=null, username=asdhkla, password=hjkldfsajöd, active=null, roles=null) - [logged in]
293293
Juli 09, 2022 1:14:40 PM io.github.Hattinger04.aop.LogAspect loginLog
294294
FEIN: User(id=null, username=asdhkla, password=hjkldfsajöd, active=null, roles=null) - [logged in]
295+
Aug. 27, 2022 8:05:52 PM io.github.Hattinger04.aop.LogAspect loginLog
296+
FEIN: User(id=null, username=admin, password=admin, active=null, roles=null) - [logged in]

0 commit comments

Comments
 (0)