Implement post likes functionality and update documentation#79
Open
edigar wants to merge 4 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Implements the README TODO: "Register who liked a post, so that each user can only like each post once, in addition to having information on who liked each post."
Likes were a denormalized counter on
posts(likes int), incremented withUPDATE posts SET likes = likes + 1— with no idea who liked and no deduplication (a user could like the same post repeatedly and inflate the count; the like/unlike handlers didn't even read the authenticated user). This PR replaces that counter with a normalizedpost_likesjoin table, so each user can like a post at most once, the count is always accurate, and we can list who liked a post.Checklist
Related Issue
Closes the "Register who liked a post…" item in the README TODO. No tracking issue.
Proposed Changes
post_likes(post_id, user_id, created_at)join table mirroringfollowers— compositePRIMARY KEY (post_id, user_id)enforces one-like-per-user at the DB, FKsON DELETE CASCADE. Theposts.likescolumn is dropped. New goose migration +build/initdb.sqlupdated.PostgainsLikedByMe bool(json:"likedByMe");Likesis now a derived value.Like(INSERT ... ON CONFLICT DO NOTHING),Unlike,Exists,FetchLikers; read queries derivelikes(COUNT subquery) andliked_by_me(EXISTS subquery) instead of reading a stored counter.LikePost/UnLikePostkeyed by the authenticated user;GetLikers; newErrPostNotFound; current user threaded intoGetById/GetUserPosts.LikePost/UnlikePostnow extract the token user (this fixes the previously commented-out auth on like), map a missing post to 404, and return 204; newGET /api/post/{postId}/likes;GetPost/GetUserPostsreturnlikedByMe.API contract
/api/post/{postId}/like/api/post/{postId}/unlike/api/post/{postId}/likes[]UserPost reads (
GET /api/post,GET /api/post/{postId},GET /api/user/{userId}/posts) now include the derivedlikescount and a per-userlikedByMeflag. Like/unlike are idempotent (liking an already-liked post or unliking a not-liked post is a no-op → 204).Tests Performed
Run on Go 1.25 (PowerShell):
go build ./... # ok
go vet ./... # ok
go test ./... # all packages PASS
Usecase and controller layers are covered with mocks (success paths, 401/403/404/400/204/200 mapping,
ErrPostNotFound, likers listing,likedByMe). Consistent with the project's convention, the repository layer has no unit tests; its new SQL is verified by integration (bring updocker-compose, then like the same post twice → count stays 1; a second user → 2;GET .../likeslists both; unlike is idempotent; like a missing post → 404).Additional Notes
posts.likesis dropped and its (unreliable) values discarded; the count is derived frompost_likes. Like/unlike now require and use the authenticated user.GET /api/post/{postId}/likesrow to the README Usage table; makeusecase.GetByIdreturn the repository error instead of swallowing it; empty list reads serializenullrather than[](pre-existing pattern).