Added authorization
This commit is contained in:
parent
c3c3d65d32
commit
b96dd39795
50 changed files with 685 additions and 410 deletions
19
internal/infrastructure/auth.go
Normal file
19
internal/infrastructure/auth.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package infrastructure
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func AuthRequired(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
|
||||
if user := session.Get("user"); user == nil {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
|
|
@ -21,8 +21,8 @@ func CreatePostRepository(conn *db.Database) PostRepository {
|
|||
}
|
||||
|
||||
func (r *PostRepository) Create(entity *entities.Post) (*entities.Post, error) {
|
||||
query := "INSERT INTO " + entities.PostTable + " (id, userid, title, description, content, createdat, updatedat)" +
|
||||
"VALUES (:id, :userid, :title, :description, :content, :createdat, :updatedat)"
|
||||
query := "INSERT INTO " + entities.PostTable + " (id, userid, title, description, content, createdat, updatedat, category, tags)" +
|
||||
"VALUES (:id, :userid, :title, :description, :content, :createdat, :updatedat, :category, :tags)"
|
||||
_, err := r.conn.Conn.NamedExec(query, entity)
|
||||
|
||||
return entity, err
|
||||
|
|
@ -49,9 +49,23 @@ func (r *PostRepository) FindById(id uuid.UUID) (*entities.Post, error) {
|
|||
|
||||
func (r *PostRepository) FindAllByUserName(userName string) ([]*entities.Post, error) {
|
||||
var entity_list []*entities.Post
|
||||
var id string
|
||||
|
||||
user_query := "SELECT id FROM " + entities.UserTable + " WHERE username=?"
|
||||
user_query, args, err := sqlx.In(user_query, userName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
user_query = r.conn.Conn.Rebind(user_query)
|
||||
err = r.conn.Conn.Select(&id, user_query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query := "SELECT * FROM " + entities.PostTable + " WHERE userid=?"
|
||||
|
||||
query, args, err := sqlx.In(query, userName)
|
||||
query, args, err = sqlx.In(query, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ func (r *UserRepository) Create(entity *entities.User) (*entities.User, error) {
|
|||
}
|
||||
|
||||
func (r *UserRepository) FindById(id uuid.UUID) (*entities.User, error) {
|
||||
var entity *entities.User
|
||||
var entity entities.User
|
||||
|
||||
query := "SELECT * FROM " + entities.UserTable + " WHERE id=?"
|
||||
query, arg, err := sqlx.In(query, id)
|
||||
|
|
@ -40,9 +40,9 @@ func (r *UserRepository) FindById(id uuid.UUID) (*entities.User, error) {
|
|||
}
|
||||
|
||||
query = r.conn.Conn.Rebind(query)
|
||||
err = r.conn.Conn.Select(entity, query, arg...)
|
||||
err = r.conn.Conn.Get(&entity, query, arg...)
|
||||
|
||||
return entity, err
|
||||
return &entity, err
|
||||
}
|
||||
|
||||
func (r *UserRepository) FindByName(username string) (*entities.User, error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue