Skip to content

Commit

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

feature(be) - implement API to check favorite button clicked in map page and detail page
  • Loading branch information
j1suk1m authored Dec 4, 2024
2 parents 7574fbf + 7d7b980 commit 5456263
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.hallyugo.hallyugo.favorite.service.FavoriteService;
import com.hallyugo.hallyugo.user.domain.User;
import lombok.RequiredArgsConstructor;
import net.minidev.json.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -47,4 +49,22 @@ public ResponseEntity<Void> decreaseFavoriteCount(
favoriteService.decreaseFavoriteCountAndDelete(user, locationId);
return ResponseEntity.ok().build();
}

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

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

}

return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,8 @@ public void decreaseFavoriteCountAndDelete(User user, Long locationId) {
favoriteRepository.deleteByUserIdAndLocationId(user.getId(), locationId);
}
}

public boolean checkFavoriteClicked(User user, Long locationId) {
return favoriteRepository.existsByUserIdAndLocationId(user.getId(), locationId);
}
}

0 comments on commit 5456263

Please sign in to comment.