sakthivikram/geo-location
0
1package com.georeport.controller;2 3import com.georeport.dto.ApiResponse;4import com.georeport.entity.User;5import com.georeport.service.AuthService;6import com.georeport.service.VoteService;7import org.springframework.beans.factory.annotation.Autowired;8import org.springframework.http.ResponseEntity;9import org.springframework.web.bind.annotation.*;10 11import java.util.Map;12 13/**14 * REST controller for vote/endorsement operations.15 */16@RestController17@RequestMapping("/api/issues")18@CrossOrigin(origins = "*")19public class VoteController {20 21 @Autowired22 private VoteService voteService;23 24 @Autowired25 private AuthService authService;26 27 /**28 * Toggle vote on an issue29 * POST /api/issues/{id}/vote30 */31 @PostMapping("/{id}/vote")32 public ResponseEntity<ApiResponse<Map<String, Object>>> toggleVote(@PathVariable Long id) {33 User user = authService.getCurrentUser();34 Map<String, Object> result = voteService.toggleVote(id, user);35 36 boolean hasVoted = (boolean) result.get("hasVoted");37 String message = hasVoted ? "Vote added" : "Vote removed";38 39 return ResponseEntity.ok(ApiResponse.success(message, result));40 }41 42 /**43 * Get vote status for current user44 * GET /api/issues/{id}/vote45 */46 @GetMapping("/{id}/vote")47 public ResponseEntity<ApiResponse<Map<String, Object>>> getVoteStatus(@PathVariable Long id) {48 User user = authService.getCurrentUser();49 Map<String, Object> result = voteService.getVoteStatus(id, user);50 return ResponseEntity.ok(ApiResponse.success(result));51 }52 53 /**54 * Get vote count (public)55 * GET /api/issues/{id}/votes/count56 */57 @GetMapping("/{id}/votes/count")58 public ResponseEntity<ApiResponse<Long>> getVoteCount(@PathVariable Long id) {59 long count = voteService.getVoteCount(id);60 return ResponseEntity.ok(ApiResponse.success(count));61 }62}63 