-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathLikeController.java
More file actions
70 lines (57 loc) · 2.6 KB
/
LikeController.java
File metadata and controls
70 lines (57 loc) · 2.6 KB
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
package com.example.demo.ZeeVideoStreaming.CONTROLLER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.ZeeVideoStreaming.ENTITY.LikeEntity;
import com.example.demo.ZeeVideoStreaming.ENTITY.UserEntity;
import com.example.demo.ZeeVideoStreaming.ENTITY.VideoEntity;
import com.example.demo.ZeeVideoStreaming.SERVICE.LikeService;
import com.example.demo.ZeeVideoStreaming.SERVICE.UserService;
import com.example.demo.ZeeVideoStreaming.SERVICE.VideoService;
@RestController
public class LikeController {
@Autowired
LikeService ls;
@Autowired
VideoService vs;
@Autowired
UserService us;
@PostMapping("/api/videos/{videoId}/like")
public ResponseEntity<Object> createLike(@PathVariable("videoId") long vid, @RequestBody LikeEntity like) {
VideoEntity video = vs.getVideoFromDB(vid);
if (video == null) {
return ResponseHandler.generateResponse("your video DNE :(", HttpStatus.BAD_REQUEST, like, false);
}
LikeEntity likeFromDB = ls.getLikeByUserAndVideoFromDB(like.getUser().getId(), vid);
if (likeFromDB != null) {
return ResponseHandler.generateResponse("you have already liked the video", HttpStatus.BAD_REQUEST, like,
false);
}
like.setVideo(video);
UserEntity user = us.getUserFromDatabase(like.getUser().getId());
like.setUser(user);
ls.createLikeInDB(like);
return ResponseHandler.generateResponse("you liked the video", HttpStatus.CREATED, like, true);
}
@DeleteMapping("/api/videos/{videoId}/unlike")
public ResponseEntity<Object> deleteLike(@PathVariable("videoId") long vid, @RequestBody LikeEntity like) {
long uid = like.getUser().getId();
LikeEntity likeFromDB = ls.getLikeByUserAndVideoFromDB(uid, vid);
if (likeFromDB == null) {
return ResponseHandler.generateResponse("you didn't like the video previously", HttpStatus.BAD_REQUEST,
like, false);
}
ls.deleteLikeByUserAndVideoFromDB(uid, vid);
return ResponseHandler.generateResponse("you unliked the video", HttpStatus.OK, like, true);
}
@GetMapping("/api/likes")
public ResponseEntity<Object> getAllLikes() {
return ResponseHandler.generateResponse("likes so far", HttpStatus.OK, ls.getAllLikesFromDB(), true);
}
}