-
Notifications
You must be signed in to change notification settings - Fork 0
/
one_to_many-grades.patch
332 lines (332 loc) · 10.9 KB
/
one_to_many-grades.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
Index: src/main/java/com/handson/basic/repo/StudentGradeService.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/repo/StudentGradeService.java b/src/main/java/com/handson/basic/repo/StudentGradeService.java
new file mode 100644
--- /dev/null (date 1642590659826)
+++ b/src/main/java/com/handson/basic/repo/StudentGradeService.java (date 1642590659826)
@@ -0,0 +1,33 @@
+package com.handson.basic.repo;
+
+
+import com.handson.basic.model.StudentGrade;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Optional;
+
+@Service
+public class StudentGradeService {
+
+ @Autowired
+ StudentGradeRepository repository;
+
+ public Iterable<StudentGrade> all() {
+ return repository.findAll();
+ }
+
+ public Optional<StudentGrade> findById(Long id) {
+ return repository.findById(id);
+ }
+
+
+ public StudentGrade save(StudentGrade studentGrade) {
+ return repository.save(studentGrade);
+ }
+
+ public void delete(StudentGrade studentGrade) {
+ repository.delete(studentGrade);
+ }
+
+}
Index: src/main/java/com/handson/basic/repo/StudentGradeRepository.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/repo/StudentGradeRepository.java b/src/main/java/com/handson/basic/repo/StudentGradeRepository.java
new file mode 100644
--- /dev/null (date 1642590953167)
+++ b/src/main/java/com/handson/basic/repo/StudentGradeRepository.java (date 1642590953167)
@@ -0,0 +1,9 @@
+package com.handson.basic.repo;
+
+
+import com.handson.basic.model.StudentGrade;
+import org.springframework.data.repository.CrudRepository;
+
+public interface StudentGradeRepository extends CrudRepository<StudentGrade,Long> {
+
+}
Index: src/main/java/com/handson/basic/controller/StudentsGradesController.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/controller/StudentsGradesController.java b/src/main/java/com/handson/basic/controller/StudentsGradesController.java
new file mode 100644
--- /dev/null (date 1642590953171)
+++ b/src/main/java/com/handson/basic/controller/StudentsGradesController.java (date 1642590953171)
@@ -0,0 +1,63 @@
+package com.handson.basic.controller;
+
+import com.handson.basic.model.GradeIn;
+import com.handson.basic.model.Student;
+import com.handson.basic.model.StudentGrade;
+import com.handson.basic.repo.StudentGradeService;
+import com.handson.basic.repo.StudentService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Optional;
+
+
+@RestController
+@RequestMapping("/api/students")
+public class StudentsGradesController {
+ @Autowired
+ StudentService studentService;
+
+ @Autowired
+ StudentGradeService studentGradeService;
+
+
+
+ @RequestMapping(value = "/{studentId}/grades", method = RequestMethod.POST)
+ public ResponseEntity<?> insertStudentGrade(Long studentId, @RequestBody GradeIn gradeIn)
+ {
+ var student = studentService.findById(studentId);
+ if (student.isEmpty()) throw new RuntimeException("Student:" + studentId +" not found");
+ StudentGrade studentGrade = gradeIn.toGrade(student.get());
+ studentGrade = studentGradeService.save(studentGrade);
+ return new ResponseEntity<>(studentGrade, HttpStatus.OK);
+ }
+
+ @RequestMapping(value = "/{studentId}/grades/{id}", method = RequestMethod.PUT)
+ public ResponseEntity<?> updateStudent(@PathVariable Long studentId, @PathVariable Long id, @RequestBody GradeIn gradeIn)
+ {
+ Optional<Student> dbStudent = studentService.findById(studentId);
+ if (dbStudent.isEmpty()) throw new RuntimeException("Student with id: " + studentId + " not found");
+
+ Optional<StudentGrade> dbStudentGrade = studentGradeService.findById(id);
+ if (dbStudentGrade.isEmpty()) throw new RuntimeException("Student grade with id: " + id + " not found");
+
+ gradeIn.updateStudentGrade(dbStudentGrade.get());
+ StudentGrade updatedStudentGrade = studentGradeService.save(dbStudentGrade.get());
+ return new ResponseEntity<>(updatedStudentGrade, HttpStatus.OK);
+ }
+
+ @RequestMapping(value = "/{studentId}/grades/{id}", method = RequestMethod.DELETE)
+ public ResponseEntity<?> deleteStudentGrade(@PathVariable Long studentId, @PathVariable Long id)
+ {
+ Optional<Student> dbStudent = studentService.findById(studentId);
+ if (dbStudent.isEmpty()) throw new RuntimeException("Student with id: " + studentId + " not found");
+
+ Optional<StudentGrade> dbStudentGrade = studentGradeService.findById(id);
+ if (dbStudentGrade.isEmpty()) throw new RuntimeException("Student grade with id: " + id + " not found");
+
+ studentGradeService.delete(dbStudentGrade.get());
+ return new ResponseEntity<>("DELETED", HttpStatus.OK);
+ }
+}
Index: src/main/java/com/handson/basic/model/StudentGrade.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/model/StudentGrade.java b/src/main/java/com/handson/basic/model/StudentGrade.java
new file mode 100644
--- /dev/null (date 1642590659821)
+++ b/src/main/java/com/handson/basic/model/StudentGrade.java (date 1642590659821)
@@ -0,0 +1,140 @@
+package com.handson.basic.model;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.handson.basic.util.Dates;
+import org.hibernate.validator.constraints.Length;
+import org.joda.time.LocalDateTime;
+
+import javax.persistence.*;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotEmpty;
+import javax.validation.constraints.NotNull;
+import java.io.Serializable;
+import java.util.Date;
+
+@Entity
+@Table(name="student_grade")
+public class StudentGrade implements Serializable {
+ private static final long serialVersionUID = 1L;
+ @Id
+ @GeneratedValue(strategy= GenerationType.AUTO)
+ private Long id;
+
+ @NotNull
+ @Column(nullable = false, updatable = false)
+ private Date createdAt = Dates.nowUTC();
+
+ @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
+ @JsonProperty("createdAt")
+ public LocalDateTime calcCreatedAt() {
+ return Dates.atLocalTime(createdAt);
+ }
+
+ @JsonIgnore
+ @NotNull
+ @ManyToOne(optional = false)
+ @JoinColumn(name = "studentId")
+ private Student student;
+
+ @NotEmpty
+ @Length(max = 60)
+ private String courseName;
+
+
+ @Min(10)
+ @Max(100)
+ private Integer courseScore;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public Date getCreatedAt() {
+ return createdAt;
+ }
+
+ public void setCreatedAt(Date createdAt) {
+ this.createdAt = createdAt;
+ }
+
+ public Student getStudent() {
+ return student;
+ }
+
+ public void setStudent(Student student) {
+ this.student = student;
+ }
+
+ public String getCourseName() {
+ return courseName;
+ }
+
+ public void setCourseName(String courseName) {
+ this.courseName = courseName;
+ }
+
+ public Integer getCourseScore() {
+ return courseScore;
+ }
+
+ public void setCourseScore(Integer courseScore) {
+ this.courseScore = courseScore;
+ }
+
+ public static final class StudentGradeBuilder {
+ private Long id;
+ private Date createdAt = Dates.nowUTC();
+ private Student student;
+ private String courseName;
+ private Integer courseScore;
+
+ private StudentGradeBuilder() {
+ }
+
+ public static StudentGradeBuilder aStudentGrade() {
+ return new StudentGradeBuilder();
+ }
+
+ public StudentGradeBuilder id(Long id) {
+ this.id = id;
+ return this;
+ }
+
+ public StudentGradeBuilder createdAt(Date createdAt) {
+ this.createdAt = createdAt;
+ return this;
+ }
+
+ public StudentGradeBuilder student(Student student) {
+ this.student = student;
+ return this;
+ }
+
+ public StudentGradeBuilder courseName(String courseName) {
+ this.courseName = courseName;
+ return this;
+ }
+
+ public StudentGradeBuilder courseScore(Integer courseScore) {
+ this.courseScore = courseScore;
+ return this;
+ }
+
+ public StudentGrade build() {
+ StudentGrade studentGrade = new StudentGrade();
+ studentGrade.student = this.student;
+ studentGrade.courseName = this.courseName;
+ studentGrade.courseScore = this.courseScore;
+ studentGrade.id = this.id;
+ studentGrade.createdAt = this.createdAt;
+ return studentGrade;
+ }
+ }
+}
Index: src/main/java/com/handson/basic/model/GradeIn.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/model/GradeIn.java b/src/main/java/com/handson/basic/model/GradeIn.java
new file mode 100644
--- /dev/null (date 1642590953173)
+++ b/src/main/java/com/handson/basic/model/GradeIn.java (date 1642590953173)
@@ -0,0 +1,37 @@
+package com.handson.basic.model;
+
+import org.hibernate.validator.constraints.Length;
+
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotEmpty;
+
+import static com.handson.basic.model.StudentGrade.StudentGradeBuilder.aStudentGrade;
+
+public class GradeIn {
+
+ @NotEmpty
+ @Length(max = 60)
+ private String courseName;
+
+ @Min(10)
+ @Max(100)
+ private Integer courseScore;
+
+ public StudentGrade toGrade(Student student) {
+ return aStudentGrade().student(student).courseName(courseName).courseScore(courseScore).build();
+ }
+
+ public void updateStudentGrade(StudentGrade studentGrade) {
+ studentGrade.setCourseName(courseName);
+ studentGrade.setCourseScore(courseScore);
+ }
+
+ public String getCourseName() {
+ return courseName;
+ }
+
+ public Integer getCourseScore() {
+ return courseScore;
+ }
+}