Skip to content

Commit e4f881e

Browse files
authored
Merge pull request #37 from FourPointSevenFive/fix-be/user-favorite-toggle
feature(be) - implement API to get total favorite counts by location id
2 parents 13c85d3 + f8f7aae commit e4f881e

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

backend/src/main/java/com/hallyugo/hallyugo/favorite/controller/FavoriteController.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.springframework.http.ResponseEntity;
1111
import org.springframework.web.bind.annotation.DeleteMapping;
1212
import org.springframework.web.bind.annotation.GetMapping;
13-
import org.springframework.web.bind.annotation.PathVariable;
1413
import org.springframework.web.bind.annotation.PostMapping;
1514
import org.springframework.web.bind.annotation.RequestMapping;
1615
import org.springframework.web.bind.annotation.RequestParam;
@@ -50,21 +49,22 @@ public ResponseEntity<Void> decreaseFavoriteCount(
5049
return ResponseEntity.ok().build();
5150
}
5251

53-
@GetMapping(value = "favorite", params = "location_id")
52+
@GetMapping(value = "/favorite", params = "location_id")
5453
public ResponseEntity<String> checkFavoriteClicked(
5554
@AuthUser User user,
5655
@RequestParam(name = "location_id") Long locationId
5756
) {
58-
String result;
57+
JSONObject result = new JSONObject();
5958
boolean isClicked = favoriteService.checkFavoriteClicked(user, locationId);
59+
long total = favoriteService.getTotalFavoriteCountByLocation(locationId);
6060

6161
if (isClicked) {
62-
result = new JSONObject().put("result", true).toString();
62+
result.put("result", true);
6363
} else {
64-
result = new JSONObject().put("result", false).toString();
65-
64+
result.put("result", false);
6665
}
6766

68-
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(result);
67+
result.put("total", total);
68+
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(result.toString());
6969
}
7070
}

backend/src/main/java/com/hallyugo/hallyugo/favorite/repository/FavoriteRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ public interface FavoriteRepository extends JpaRepository<Favorite, Long> {
1010
boolean existsByUserIdAndLocationId(Long userId, Long locationId);
1111

1212
void deleteByUserIdAndLocationId(Long userId, Long locationId);
13+
14+
long countByLocationId(Long locationId);
1315
}

backend/src/main/java/com/hallyugo/hallyugo/favorite/service/FavoriteService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,8 @@ public void decreaseFavoriteCountAndDelete(User user, Long locationId) {
104104
public boolean checkFavoriteClicked(User user, Long locationId) {
105105
return favoriteRepository.existsByUserIdAndLocationId(user.getId(), locationId);
106106
}
107+
108+
public long getTotalFavoriteCountByLocation(Long locationId) {
109+
return favoriteRepository.countByLocationId(locationId);
110+
}
107111
}

0 commit comments

Comments
 (0)