forked from denysvitali/rings-social-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create post vote endpoint (#2)
- Loading branch information
1 parent
d1f3548
commit e437dff
Showing
13 changed files
with
308 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package models | ||
|
||
import "time" | ||
|
||
type PostAction struct { | ||
Username string `json:"username" gorm:"primaryKey"` | ||
User User `json:"user,omitempty" gorm:"foreignKey:Username"` | ||
Post Post `json:"post,omitempty"` | ||
PostId uint `json:"post_id" gorm:"primaryKey"` | ||
Action string `json:"action" gorm:"type:post_action;index"` | ||
CreatedAt time.Time | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package server | ||
|
||
import "backend/pkg/models" | ||
|
||
func (s *Server) repoGetVote(username string, postId uint) (*models.PostAction, error) { | ||
var postAction models.PostAction | ||
tx := s.db.First(&postAction, "username = ? AND post_id = ?", username, postId) | ||
if tx.Error != nil { | ||
return nil, tx.Error | ||
} | ||
|
||
return &postAction, nil | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package server | ||
|
||
import ( | ||
"backend/pkg/models" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
func (s *Server) innerRouteVotePost(c *gin.Context, action models.VoteAction) { | ||
postId, done := parsePostId(c) | ||
if done { | ||
return | ||
} | ||
|
||
username := c.GetString("username") | ||
err := s.repoVoteAction(action, username, postId) | ||
if err != nil { | ||
if err.Error() == ErrUnableToVoteUserAlreadyVoted { | ||
c.JSON(http.StatusOK, gin.H{"error": "user already voted"}) | ||
return | ||
} | ||
s.logger.Errorf("unable to vote post: %v", err) | ||
c.JSON(500, gin.H{"error": "unable to vote post"}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusAccepted, gin.H{"message": "voted"}) | ||
} | ||
|
||
func (s *Server) routeUpvotePost(c *gin.Context) { | ||
s.innerRouteVotePost(c, models.ActionUpvote) | ||
} | ||
|
||
func (s *Server) routeDownvotePost(c *gin.Context) { | ||
s.innerRouteVotePost(c, models.ActionDownvote) | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.