This is lazy push
This commit is contained in:
parent
9aa5cbc12a
commit
c8967df573
36
enshi_back/ABAC/PostVotesPolicies/PostVotePolicies.go
Normal file
36
enshi_back/ABAC/PostVotesPolicies/PostVotePolicies.go
Normal file
@ -0,0 +1,36 @@
|
||||
package postvotespolicies
|
||||
|
||||
import (
|
||||
postvoterules "enshi/ABAC/PostVotesPolicies/PostVoteRules"
|
||||
"enshi/ABAC/rules"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
DELETE_VOTE = "delete_vote"
|
||||
CREATE_VOTE = "create_vote"
|
||||
READ_VOTE = "read_vote"
|
||||
)
|
||||
|
||||
func PostVotePolicies(c *gin.Context) (bool, []error) {
|
||||
target, exists := c.Get("target")
|
||||
if !exists {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Permit if one permit
|
||||
switch target {
|
||||
case DELETE_VOTE:
|
||||
return rules.CheckRule(c, postvoterules.PostVoteDeleteRule)
|
||||
|
||||
case CREATE_VOTE:
|
||||
return rules.CheckRule(c, postvoterules.PostVoteCreateRule)
|
||||
|
||||
case READ_VOTE:
|
||||
return rules.CheckRule(c, postvoterules.PostVoteReadRule)
|
||||
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package postvoterules
|
||||
|
||||
import (
|
||||
globalrules "enshi/ABAC/GlobalRules"
|
||||
"enshi/ABAC/rules"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func PostVoteCreateRule(c *gin.Context) (bool, []error) {
|
||||
rulesToCheck := []rules.RuleFunction{
|
||||
globalrules.AuthorizedRule,
|
||||
}
|
||||
|
||||
isAllowed, errors := rules.CheckRules(
|
||||
c,
|
||||
rulesToCheck,
|
||||
rules.ALL_RULES_MUST_BE_COMPLETED,
|
||||
)
|
||||
|
||||
return isAllowed, errors
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package postvoterules
|
||||
|
||||
import (
|
||||
globalrules "enshi/ABAC/GlobalRules"
|
||||
"enshi/ABAC/rules"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func PostVoteDeleteRule(c *gin.Context) (bool, []error) {
|
||||
rulesToCheck := []rules.RuleFunction{
|
||||
globalrules.AuthorizedRule,
|
||||
}
|
||||
|
||||
isAllowed, errors := rules.CheckRules(
|
||||
c,
|
||||
rulesToCheck,
|
||||
rules.ALL_RULES_MUST_BE_COMPLETED,
|
||||
)
|
||||
|
||||
return isAllowed, errors
|
||||
}
|
||||
22
enshi_back/ABAC/PostVotesPolicies/PostVoteRules/readRule.go
Normal file
22
enshi_back/ABAC/PostVotesPolicies/PostVoteRules/readRule.go
Normal file
@ -0,0 +1,22 @@
|
||||
package postvoterules
|
||||
|
||||
import (
|
||||
globalrules "enshi/ABAC/GlobalRules"
|
||||
"enshi/ABAC/rules"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func PostVoteReadRule(c *gin.Context) (bool, []error) {
|
||||
rulesToCheck := []rules.RuleFunction{
|
||||
globalrules.AuthorizedRule,
|
||||
}
|
||||
|
||||
isAllowed, errors := rules.CheckRules(
|
||||
c,
|
||||
rulesToCheck,
|
||||
rules.ALL_RULES_MUST_BE_COMPLETED,
|
||||
)
|
||||
|
||||
return isAllowed, errors
|
||||
}
|
||||
@ -13,6 +13,9 @@ const createPostVote = `-- name: CreatePostVote :one
|
||||
INSERT INTO public.post_votes
|
||||
(post_id, user_id, vote)
|
||||
VALUES($1, $2, $3)
|
||||
ON CONFLICT (user_id, post_id)
|
||||
DO UPDATE SET
|
||||
vote = $3
|
||||
RETURNING post_id, user_id, vote
|
||||
`
|
||||
|
||||
|
||||
@ -2,6 +2,9 @@
|
||||
INSERT INTO public.post_votes
|
||||
(post_id, user_id, vote)
|
||||
VALUES($1, $2, $3)
|
||||
ON CONFLICT (user_id, post_id)
|
||||
DO UPDATE SET
|
||||
vote = $3
|
||||
RETURNING *;
|
||||
|
||||
-- name: DeletePostVote :exec
|
||||
|
||||
33
enshi_back/middleware/postVotesMiddleware.go
Normal file
33
enshi_back/middleware/postVotesMiddleware.go
Normal file
@ -0,0 +1,33 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
postvotespolicies "enshi/ABAC/PostVotesPolicies"
|
||||
"enshi/ABAC/rules"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func PostVotesMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
switch c.Request.Method {
|
||||
case "DELETE":
|
||||
c.Set("target", postvotespolicies.DELETE_VOTE)
|
||||
|
||||
case "POST":
|
||||
c.Set("target", postvotespolicies.CREATE_VOTE)
|
||||
|
||||
case "GET":
|
||||
c.Set("target", postvotespolicies.READ_VOTE)
|
||||
}
|
||||
|
||||
isAllowed, errors := postvotespolicies.PostVotePolicies(c)
|
||||
|
||||
if rules.ShouldAbortRequest(c, isAllowed, errors) {
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@ -10,7 +10,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func CreatePost(c *gin.Context) {
|
||||
func CreateBookmark(c *gin.Context) {
|
||||
var bookmarkParams db_repo.CreateBookmarkParams
|
||||
|
||||
if err := c.BindJSON(&bookmarkParams); err != nil {
|
||||
|
||||
35
enshi_back/routes/bookmarksRoutes/deleteBookmark.go
Normal file
35
enshi_back/routes/bookmarksRoutes/deleteBookmark.go
Normal file
@ -0,0 +1,35 @@
|
||||
package bookmarksroutes
|
||||
|
||||
import (
|
||||
"context"
|
||||
rest_api_stuff "enshi/REST_API_stuff"
|
||||
db_repo "enshi/db/go_queries"
|
||||
"enshi/db_connection"
|
||||
"enshi/middleware/getters"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func DeleteBookmark(c *gin.Context) {
|
||||
var bookmarkParams db_repo.DeleteBookmarkParams
|
||||
|
||||
if err := c.BindJSON(&bookmarkParams); err != nil {
|
||||
rest_api_stuff.BadRequestAnswer(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := getters.GetUserIdFromContext(c)
|
||||
if err != nil {
|
||||
rest_api_stuff.BadRequestAnswer(c, err)
|
||||
return
|
||||
}
|
||||
bookmarkParams.UserID = userId
|
||||
|
||||
query := db_repo.New(db_connection.Dbx)
|
||||
if err := query.DeleteBookmark(context.Background(), bookmarkParams); err != nil {
|
||||
rest_api_stuff.InternalErrorAnswer(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
rest_api_stuff.OkAnswer(c, "Bookmark has been deleted!")
|
||||
}
|
||||
48
enshi_back/routes/bookmarksRoutes/getBookmark.go
Normal file
48
enshi_back/routes/bookmarksRoutes/getBookmark.go
Normal file
@ -0,0 +1,48 @@
|
||||
package bookmarksroutes
|
||||
|
||||
import (
|
||||
"context"
|
||||
rest_api_stuff "enshi/REST_API_stuff"
|
||||
db_repo "enshi/db/go_queries"
|
||||
"enshi/db_connection"
|
||||
"enshi/middleware/getters"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GetBookmark(c *gin.Context) {
|
||||
var bookmarkParams db_repo.GetBookmarkTimestampParams
|
||||
|
||||
if err := c.BindJSON(&bookmarkParams); err != nil {
|
||||
rest_api_stuff.BadRequestAnswer(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := getters.GetUserIdFromContext(c)
|
||||
if err != nil {
|
||||
rest_api_stuff.BadRequestAnswer(c, err)
|
||||
return
|
||||
}
|
||||
bookmarkParams.UserID = userId
|
||||
|
||||
query := db_repo.New(db_connection.Dbx)
|
||||
if timestamp, err := query.GetBookmarkTimestamp(context.Background(), bookmarkParams); err != nil {
|
||||
rest_api_stuff.InternalErrorAnswer(c, err)
|
||||
return
|
||||
} else {
|
||||
if timestamp.Valid {
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"isBookmarked": timestamp.Valid,
|
||||
"bookmarkedAt": timestamp.Time,
|
||||
})
|
||||
return
|
||||
} else {
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"isBookmarked": timestamp.Valid,
|
||||
"bookmarkedAt": time.Unix(1<<63-1, 0).UTC(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -8,6 +8,7 @@ import (
|
||||
bookmarksroutes "enshi/routes/bookmarksRoutes"
|
||||
"enshi/routes/postsRoutes"
|
||||
"enshi/routes/userProfileRoutes"
|
||||
voteroutes "enshi/routes/voteRoutes"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@ -123,6 +124,34 @@ func SetupRotes(g *gin.Engine) error {
|
||||
bookmarksroutes.CreateBookmark,
|
||||
)
|
||||
|
||||
bookmarksGroup.DELETE(
|
||||
"bookmarks/:post-id",
|
||||
bookmarksroutes.DeleteBookmark,
|
||||
)
|
||||
|
||||
bookmarksGroup.GET(
|
||||
"bookmarks/:post-id",
|
||||
bookmarksroutes.GetBookmark,
|
||||
)
|
||||
|
||||
postVoteGroup := g.Group("/")
|
||||
postVoteGroup.Use(middleware.PostVotesMiddleware())
|
||||
|
||||
postVoteGroup.POST(
|
||||
"post-votes/:post-id",
|
||||
voteroutes.CreateVote,
|
||||
)
|
||||
|
||||
postVoteGroup.DELETE(
|
||||
"post-votes/:post-id",
|
||||
voteroutes.DeleteVote,
|
||||
)
|
||||
|
||||
postVoteGroup.GET(
|
||||
"post-votes/:post-id",
|
||||
voteroutes.GetVote,
|
||||
)
|
||||
|
||||
// Admin group routes
|
||||
adminGroup := g.Group("/admin/")
|
||||
adminGroup.Use(middleware.AdminMiddleware())
|
||||
|
||||
35
enshi_back/routes/voteRoutes/createVote.go
Normal file
35
enshi_back/routes/voteRoutes/createVote.go
Normal file
@ -0,0 +1,35 @@
|
||||
package voteroutes
|
||||
|
||||
import (
|
||||
"context"
|
||||
rest_api_stuff "enshi/REST_API_stuff"
|
||||
db_repo "enshi/db/go_queries"
|
||||
"enshi/db_connection"
|
||||
"enshi/middleware/getters"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func CreateVote(c *gin.Context) {
|
||||
var postVoteParams db_repo.CreatePostVoteParams
|
||||
|
||||
if err := c.BindJSON(&postVoteParams); err != nil {
|
||||
rest_api_stuff.BadRequestAnswer(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := getters.GetUserIdFromContext(c)
|
||||
if err != nil {
|
||||
rest_api_stuff.BadRequestAnswer(c, err)
|
||||
return
|
||||
}
|
||||
postVoteParams.UserID = userId
|
||||
|
||||
query := db_repo.New(db_connection.Dbx)
|
||||
if _, err := query.CreatePostVote(context.Background(), postVoteParams); err != nil {
|
||||
rest_api_stuff.InternalErrorAnswer(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
rest_api_stuff.OkAnswer(c, "Vote has been created!")
|
||||
}
|
||||
35
enshi_back/routes/voteRoutes/deleteVote.go
Normal file
35
enshi_back/routes/voteRoutes/deleteVote.go
Normal file
@ -0,0 +1,35 @@
|
||||
package voteroutes
|
||||
|
||||
import (
|
||||
"context"
|
||||
rest_api_stuff "enshi/REST_API_stuff"
|
||||
db_repo "enshi/db/go_queries"
|
||||
"enshi/db_connection"
|
||||
"enshi/middleware/getters"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func DeleteVote(c *gin.Context) {
|
||||
var postVoteParams db_repo.DeletePostVoteParams
|
||||
|
||||
if err := c.BindJSON(&postVoteParams); err != nil {
|
||||
rest_api_stuff.BadRequestAnswer(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := getters.GetUserIdFromContext(c)
|
||||
if err != nil {
|
||||
rest_api_stuff.BadRequestAnswer(c, err)
|
||||
return
|
||||
}
|
||||
postVoteParams.UserID = userId
|
||||
|
||||
query := db_repo.New(db_connection.Dbx)
|
||||
if err := query.DeletePostVote(context.Background(), postVoteParams); err != nil {
|
||||
rest_api_stuff.InternalErrorAnswer(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
rest_api_stuff.OkAnswer(c, "Vote has been deleted!")
|
||||
}
|
||||
38
enshi_back/routes/voteRoutes/getVote.go
Normal file
38
enshi_back/routes/voteRoutes/getVote.go
Normal file
@ -0,0 +1,38 @@
|
||||
package voteroutes
|
||||
|
||||
import (
|
||||
"context"
|
||||
rest_api_stuff "enshi/REST_API_stuff"
|
||||
db_repo "enshi/db/go_queries"
|
||||
"enshi/db_connection"
|
||||
"enshi/middleware/getters"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GetVote(c *gin.Context) {
|
||||
var postVoteParams db_repo.GetPostVoteParams
|
||||
|
||||
if err := c.BindJSON(&postVoteParams); err != nil {
|
||||
rest_api_stuff.BadRequestAnswer(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := getters.GetUserIdFromContext(c)
|
||||
if err != nil {
|
||||
rest_api_stuff.BadRequestAnswer(c, err)
|
||||
return
|
||||
}
|
||||
postVoteParams.UserID = userId
|
||||
|
||||
query := db_repo.New(db_connection.Dbx)
|
||||
if voteData, err := query.GetPostVote(context.Background(), postVoteParams); err != nil {
|
||||
rest_api_stuff.InternalErrorAnswer(c, err)
|
||||
return
|
||||
} else {
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"vote": voteData,
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user