Skip to content

Commit

Permalink
Merge pull request #37 from FourPointSevenFive/fix-be/user-favorite-t…
Browse files Browse the repository at this point in the history
…oggle

feature(be) - implement API to get total favorite counts by location id
  • Loading branch information
j1suk1m authored Dec 4, 2024
2 parents 13c85d3 + f8f7aae commit e4f881e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -50,21 +49,22 @@ public ResponseEntity<Void> decreaseFavoriteCount(
return ResponseEntity.ok().build();
}

@GetMapping(value = "favorite", params = "location_id")
@GetMapping(value = "/favorite", params = "location_id")
public ResponseEntity<String> checkFavoriteClicked(
@AuthUser User user,
@RequestParam(name = "location_id") Long locationId
) {
String result;
JSONObject result = new JSONObject();
boolean isClicked = favoriteService.checkFavoriteClicked(user, locationId);
long total = favoriteService.getTotalFavoriteCountByLocation(locationId);

if (isClicked) {
result = new JSONObject().put("result", true).toString();
result.put("result", true);
} else {
result = new JSONObject().put("result", false).toString();

result.put("result", false);
}

return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(result);
result.put("total", total);
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(result.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface FavoriteRepository extends JpaRepository<Favorite, Long> {
boolean existsByUserIdAndLocationId(Long userId, Long locationId);

void deleteByUserIdAndLocationId(Long userId, Long locationId);

long countByLocationId(Long locationId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,8 @@ public void decreaseFavoriteCountAndDelete(User user, Long locationId) {
public boolean checkFavoriteClicked(User user, Long locationId) {
return favoriteRepository.existsByUserIdAndLocationId(user.getId(), locationId);
}

public long getTotalFavoriteCountByLocation(Long locationId) {
return favoriteRepository.countByLocationId(locationId);
}
}

0 comments on commit e4f881e

Please sign in to comment.