File size: 1,299 Bytes
bb5169d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package com.krishna.controller;

import com.krishna.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@CrossOrigin(origins = {"http://localhost:8080", "http://localhost:8081", "http://127.0.0.1:5500", "http://localhost:5173/", "https://my-media-718.netlify.app"})
@RestController
@RequestMapping("/likes")
public class LikeController {

    @Autowired
    private LikeService likeService;

    @PostMapping("/toggle")
    public ResponseEntity<String> toggleLike(
            @RequestParam UUID userId,
            @RequestParam Long postId
    ) {
        boolean liked = likeService.toggleLike(userId, postId);
        return ResponseEntity.ok(liked ? "Post liked successfully" : "Post unliked");
    }

    @GetMapping("/count/{postId}")
    public Long getLikeCount(@PathVariable Long postId) {
        return likeService.getLikeCount(postId);
    }

    @GetMapping("/status")
    public ResponseEntity<Boolean> checkIfUserLikedPost(
            @RequestParam UUID userId,
            @RequestParam Long postId
    ) {
        boolean liked = likeService.hasUserLikedPost(userId, postId);
        return ResponseEntity.ok(liked);
    }
}