浏览代码

ratings added, comment & review updated

rjsmall90 6 年前
父节点
当前提交
d884370e7a

+ 10
- 0
src/main/java/com/passionproject/project_spitball20/controller/RatingController.java 查看文件

@@ -0,0 +1,10 @@
1
+package com.passionproject.project_spitball20.controller;
2
+
3
+
4
+import org.springframework.web.bind.annotation.CrossOrigin;
5
+import org.springframework.web.bind.annotation.RestController;
6
+
7
+@CrossOrigin(origins = "http://localhost:8100")
8
+@RestController
9
+public class RatingController {
10
+}

+ 10
- 4
src/main/java/com/passionproject/project_spitball20/controller/UserController.java 查看文件

@@ -10,9 +10,9 @@ import org.springframework.http.HttpStatus;
10 10
 import org.springframework.http.ResponseEntity;
11 11
 import org.springframework.web.bind.annotation.*;
12 12
 
13
-@CrossOrigin
13
+@CrossOrigin(origins = "http://localhost:8100")
14 14
 @RestController
15
-@RequestMapping("user_profile")
15
+//@RequestMapping("user_profile")
16 16
 public class UserController {
17 17
 
18 18
     @Autowired
@@ -21,11 +21,11 @@ public class UserController {
21 21
     public static final Logger logger = LoggerFactory.getLogger(UserController.class);
22 22
 
23 23
 
24
-    @RequestMapping(value = "/register", method = RequestMethod.POST)
24
+    @RequestMapping(value = "/register_user", method = RequestMethod.POST)
25 25
     public ResponseEntity<User> registerUser(@RequestBody User newUser) {
26 26
         User user = new User();
27 27
         HttpStatus status = HttpStatus.CONFLICT;
28
-        if(userService.findEmail(newUser.getEmail()).equals(null)) { //&& userService.findName(newUser.getDisplayName()).equals(null)) {
28
+        if(userService.findEmail(newUser.getEmail()) == null){ //&& userService.findName(newUser.getDisplayName()).equals(null)) {
29 29
             user = userService.save(newUser);
30 30
             status  = HttpStatus.CREATED;
31 31
         }
@@ -33,4 +33,10 @@ public class UserController {
33 33
         return new ResponseEntity<>(user, new HttpHeaders(), status);
34 34
 
35 35
     }
36
+
37
+    @RequestMapping(value = "/delete_profile", method = RequestMethod.DELETE)
38
+    public ResponseEntity<User> deleteMessage(@RequestBody User user) {
39
+        userService.delete(user);
40
+        return new ResponseEntity<>(HttpStatus.OK);
41
+    }
36 42
 }

+ 12
- 0
src/main/java/com/passionproject/project_spitball20/controller/messages/CommentController.java 查看文件

@@ -0,0 +1,12 @@
1
+package com.passionproject.project_spitball20.controller.messages;
2
+
3
+
4
+import org.springframework.web.bind.annotation.CrossOrigin;
5
+import org.springframework.web.bind.annotation.RequestMapping;
6
+import org.springframework.web.bind.annotation.RestController;
7
+
8
+@CrossOrigin
9
+@RestController
10
+@RequestMapping("comments")
11
+public class CommentController {
12
+}

+ 56
- 0
src/main/java/com/passionproject/project_spitball20/controller/messages/ReviewController.java 查看文件

@@ -0,0 +1,56 @@
1
+package com.passionproject.project_spitball20.controller.messages;
2
+
3
+
4
+import com.fasterxml.jackson.core.JsonProcessingException;
5
+import com.fasterxml.jackson.databind.ObjectMapper;
6
+import com.passionproject.project_spitball20.model.messages.Review;
7
+import com.passionproject.project_spitball20.service.messages.ReviewService;
8
+import org.springframework.beans.factory.annotation.Autowired;
9
+import org.springframework.http.HttpHeaders;
10
+import org.springframework.http.HttpStatus;
11
+import org.springframework.http.MediaType;
12
+import org.springframework.http.ResponseEntity;
13
+import org.springframework.web.bind.annotation.*;
14
+
15
+@CrossOrigin
16
+@RestController
17
+@RequestMapping("reviews")
18
+public class ReviewController {
19
+
20
+    @Autowired
21
+    ReviewService reviewService;
22
+
23
+    @RequestMapping(value = "/post", method = RequestMethod.POST)
24
+    public ResponseEntity<Review> postMessage(@RequestBody Review newReview) {
25
+        HttpStatus httpStatus = HttpStatus.CREATED;
26
+        Review content = this.reviewService.save(newReview);
27
+
28
+        return new ResponseEntity<>(content, new HttpHeaders(), httpStatus);
29
+    }
30
+
31
+    @RequestMapping(value = "/update", method = RequestMethod.PUT)
32
+    public ResponseEntity<Review> updateMessage(@RequestBody Review review) {
33
+        HttpStatus httpStatus = HttpStatus.CREATED;
34
+        review = (Review) this.reviewService.update(review);
35
+
36
+        return new ResponseEntity<>(review, new HttpHeaders(), httpStatus);
37
+    }
38
+
39
+    @RequestMapping(value = "/get_reviews", method = RequestMethod.GET)
40
+    public ResponseEntity<?> getAllMessages() throws JsonProcessingException {
41
+        ObjectMapper objectMapper = new ObjectMapper();
42
+        HttpStatus httpStatus = HttpStatus.CREATED;
43
+        HttpHeaders header = new HttpHeaders();
44
+
45
+        String messageJSON = objectMapper.writeValueAsString(this.reviewService.getAllReviews());
46
+        header.setContentType(MediaType.APPLICATION_JSON);
47
+
48
+        return new ResponseEntity<>(messageJSON, header, httpStatus);
49
+    }
50
+
51
+    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
52
+    public ResponseEntity<Review> deleteMessage(@RequestBody Review review) {
53
+        reviewService.delete(review);
54
+        return new ResponseEntity<>(HttpStatus.OK);
55
+    }
56
+}

+ 41
- 0
src/main/java/com/passionproject/project_spitball20/model/Rating.java 查看文件

@@ -0,0 +1,41 @@
1
+package com.passionproject.project_spitball20.model;
2
+
3
+import javax.persistence.*;
4
+
5
+
6
+@Entity
7
+public class Rating {
8
+
9
+    @Id
10
+    @GeneratedValue(strategy = GenerationType.AUTO)
11
+    private Long id;
12
+
13
+    Long teacherId;
14
+
15
+    Integer rating;
16
+
17
+    public Long getId() {
18
+        return id;
19
+    }
20
+
21
+    public void setId(Long id) {
22
+        this.id = id;
23
+    }
24
+
25
+    public Long getTeacherId() {
26
+        return teacherId;
27
+    }
28
+
29
+    public void setTeacherId(Long teacherId) {
30
+        this.teacherId = teacherId;
31
+    }
32
+
33
+    public Integer getRating() {
34
+        return rating;
35
+    }
36
+
37
+    public void setRating(Integer rating) {
38
+        this.rating = rating;
39
+    }
40
+
41
+}

+ 16
- 0
src/main/java/com/passionproject/project_spitball20/model/Teacher.java 查看文件

@@ -1,6 +1,9 @@
1 1
 package com.passionproject.project_spitball20.model;
2 2
 
3
+import org.hibernate.engine.internal.Cascade;
4
+
3 5
 import javax.persistence.*;
6
+import java.util.List;
4 7
 
5 8
 @Entity
6 9
 @Table
@@ -15,6 +18,11 @@ public class Teacher {
15 18
     String email;
16 19
     String password;
17 20
 
21
+    @OneToMany(cascade = CascadeType.ALL)
22
+    @JoinColumn(name = "teacherId")
23
+    private List<Rating> ratings;
24
+
25
+
18 26
     public Long getId() {
19 27
         return id;
20 28
     }
@@ -58,4 +66,12 @@ public class Teacher {
58 66
     public String getFullName() {
59 67
         return firstName + lastName;
60 68
     }
69
+
70
+    public List<Rating> getRatings() {
71
+        return ratings;
72
+    }
73
+
74
+    public void setRatings(List<Rating> ratings) {
75
+        this.ratings = ratings;
76
+    }
61 77
 }

+ 15
- 0
src/main/java/com/passionproject/project_spitball20/model/messages/Comment.java 查看文件

@@ -0,0 +1,15 @@
1
+package com.passionproject.project_spitball20.model.messages;
2
+
3
+
4
+import javax.persistence.*;
5
+
6
+
7
+@Entity
8
+@Table
9
+public class Comment extends Review {
10
+
11
+
12
+
13
+
14
+
15
+}

+ 62
- 0
src/main/java/com/passionproject/project_spitball20/model/messages/Review.java 查看文件

@@ -0,0 +1,62 @@
1
+package com.passionproject.project_spitball20.model.messages;
2
+
3
+import javax.persistence.*;
4
+import java.util.Date;
5
+
6
+@Entity
7
+@Table
8
+public class Review {
9
+
10
+    @Id
11
+    @GeneratedValue(strategy = GenerationType.AUTO)
12
+    private Long id;
13
+
14
+    private Long userId;
15
+
16
+    private Date created;
17
+
18
+    private Date updated;
19
+
20
+    String review;
21
+
22
+    public Long getId() {
23
+        return id;
24
+    }
25
+
26
+    public void setId(Long id) {
27
+        this.id = id;
28
+    }
29
+
30
+    public Long getUserId() {
31
+        return userId;
32
+    }
33
+
34
+    public void setUserId(Long userId) {
35
+        this.userId = userId;
36
+    }
37
+
38
+    public Date getCreated() {
39
+        return created;
40
+    }
41
+
42
+    public void setCreated(Date created) {
43
+        this.created = created;
44
+    }
45
+
46
+    public Date getUpdated() {
47
+        return updated;
48
+    }
49
+
50
+    public void setUpdated(Date updated) {
51
+        this.updated = updated;
52
+    }
53
+
54
+    public String getReview() {
55
+        return review;
56
+    }
57
+
58
+    public void setReview(String review) {
59
+        this.review = review;
60
+    }
61
+}
62
+

+ 10
- 0
src/main/java/com/passionproject/project_spitball20/repositories/RatingRepository.java 查看文件

@@ -0,0 +1,10 @@
1
+package com.passionproject.project_spitball20.repositories;
2
+
3
+import com.passionproject.project_spitball20.model.Rating;
4
+import org.springframework.data.jpa.repository.JpaRepository;
5
+import org.springframework.stereotype.Repository;
6
+
7
+
8
+@Repository
9
+public interface RatingRepository extends JpaRepository<Rating, Long> {
10
+}

+ 11
- 0
src/main/java/com/passionproject/project_spitball20/repositories/messages/CommentRepository.java 查看文件

@@ -0,0 +1,11 @@
1
+package com.passionproject.project_spitball20.repositories.messages;
2
+
3
+
4
+import com.passionproject.project_spitball20.model.messages.Comment;
5
+import org.springframework.data.jpa.repository.JpaRepository;
6
+import org.springframework.stereotype.Repository;
7
+
8
+@Repository
9
+public interface CommentRepository extends JpaRepository<Comment, Long> {
10
+
11
+}

+ 10
- 0
src/main/java/com/passionproject/project_spitball20/repositories/messages/ReviewRepository.java 查看文件

@@ -0,0 +1,10 @@
1
+package com.passionproject.project_spitball20.repositories.messages;
2
+
3
+
4
+import com.passionproject.project_spitball20.model.messages.Review;
5
+import org.springframework.data.jpa.repository.JpaRepository;
6
+import org.springframework.stereotype.Repository;
7
+
8
+@Repository
9
+public interface ReviewRepository extends JpaRepository<Review, Long> {
10
+}

+ 32
- 0
src/main/java/com/passionproject/project_spitball20/service/RatingService.java 查看文件

@@ -0,0 +1,32 @@
1
+package com.passionproject.project_spitball20.service;
2
+
3
+import com.passionproject.project_spitball20.model.Rating;
4
+import com.passionproject.project_spitball20.repositories.RatingRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.stereotype.Service;
7
+
8
+@Service
9
+public class RatingService {
10
+
11
+
12
+    @Autowired
13
+    RatingRepository ratingRepository;
14
+
15
+    public Rating save(Rating rating) {
16
+        return ratingRepository.saveAndFlush(rating);
17
+    }
18
+
19
+    public Rating update(Rating rating) {
20
+        return ratingRepository.save(rating);
21
+    }
22
+
23
+    public void delete(Rating rating) {
24
+        ratingRepository.delete(rating);
25
+    }
26
+
27
+    public Rating find(Long id) {
28
+        return ratingRepository.findById(id).get();
29
+    }
30
+
31
+
32
+}

+ 4
- 4
src/main/java/com/passionproject/project_spitball20/service/UserService.java 查看文件

@@ -20,14 +20,14 @@ public class UserService {
20 20
         return userRepository.save(user);
21 21
     }
22 22
 
23
+    public void delete(User user) {
24
+        userRepository.delete(user);
25
+    }
26
+
23 27
     public User findEmail(String email) {
24 28
         return userRepository.findByEmail(email);
25 29
     }
26 30
 
27
-//    public User findName(String displayName) {
28
-//        return userRepository.findByName(displayName);
29
-//    }
30
-
31 31
     public User find(Long id) {
32 32
         return userRepository.findById(id).get();
33 33
     }

+ 38
- 0
src/main/java/com/passionproject/project_spitball20/service/messages/CommentService.java 查看文件

@@ -0,0 +1,38 @@
1
+package com.passionproject.project_spitball20.service.messages;
2
+
3
+
4
+import com.passionproject.project_spitball20.model.messages.Comment;
5
+import com.passionproject.project_spitball20.repositories.messages.CommentRepository;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.stereotype.Service;
8
+
9
+import java.util.Date;
10
+
11
+@Service
12
+public class CommentService {
13
+
14
+    @Autowired
15
+    CommentRepository commentRepository;
16
+
17
+
18
+    public Comment save(Comment comment) {
19
+        comment.setCreated(new Date());
20
+        return commentRepository.save(comment);
21
+    }
22
+
23
+    public Comment update(Comment comment) {
24
+        Comment update = commentRepository.findById(comment.getId()).get();
25
+        update.setUpdated(new Date());
26
+        update.setReview(comment.getReview());
27
+        return commentRepository.save(update);
28
+
29
+    }
30
+
31
+    public Iterable<Comment> getAllComments() {
32
+        return commentRepository.findAll();
33
+    }
34
+
35
+    public void delete(Comment comment) {
36
+        commentRepository.delete(comment);
37
+    }
38
+}

+ 39
- 0
src/main/java/com/passionproject/project_spitball20/service/messages/ReviewService.java 查看文件

@@ -0,0 +1,39 @@
1
+package com.passionproject.project_spitball20.service.messages;
2
+
3
+
4
+import com.passionproject.project_spitball20.model.messages.Review;
5
+import com.passionproject.project_spitball20.repositories.messages.ReviewRepository;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.stereotype.Service;
8
+
9
+import java.util.Date;
10
+
11
+@Service
12
+public class ReviewService {
13
+
14
+
15
+    @Autowired
16
+    ReviewRepository reviewRepository;
17
+
18
+
19
+    public Review save(Review review) {
20
+        review.setCreated(new Date());
21
+        return reviewRepository.save(review);
22
+    }
23
+
24
+    public Review update(Review review) {
25
+        Review update = reviewRepository.findById(review.getId()).get();
26
+        update.setUpdated(new Date());
27
+        update.setReview(review.getReview());
28
+        return reviewRepository.save(update);
29
+
30
+    }
31
+
32
+    public Iterable<Review> getAllReviews() {
33
+        return reviewRepository.findAll();
34
+    }
35
+
36
+    public void delete(Review review) {
37
+        reviewRepository.delete(review);
38
+    }
39
+}