Added authorization

This commit is contained in:
KamilM1205 2025-09-25 09:01:00 +04:00
parent c3c3d65d32
commit b96dd39795
50 changed files with 685 additions and 410 deletions

View file

@ -5,21 +5,24 @@ import (
"time"
"github.com/google/uuid"
"github.com/lib/pq"
)
const PostTable = "post"
type Post struct {
Id uuid.UUID `db:"id"`
UserId uuid.UUID `db:"userid"`
Title string `db:"title"`
Description string `db:"description"`
Content string `db:"content"`
CreatedAt time.Time `db:"createdat"`
UpdatedAt time.Time `db:"updatedat"`
Id uuid.UUID `db:"id"`
UserId uuid.UUID `db:"userid"`
Title string `db:"title"`
Description string `db:"description"`
Content string `db:"content"`
CreatedAt time.Time `db:"createdat"`
UpdatedAt time.Time `db:"updatedat"`
Category string `db:"category"`
Tags pq.StringArray `db:"tags"` // TODO: rewrite it to many2many
}
func CreatePost(userId uuid.UUID, title string, description string, content string) (post Post, err error) {
func CreatePost(userId uuid.UUID, title string, description string, content string, category string, tags []string) (post Post, err error) {
post = Post{
Id: uuid.New(),
UserId: userId,
@ -28,6 +31,8 @@ func CreatePost(userId uuid.UUID, title string, description string, content stri
Content: content,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Category: category,
Tags: tags,
}
err = post.Validate()

View file

@ -1,43 +0,0 @@
package entities
import (
"errors"
"github.com/google/uuid"
)
const PostsTable = "posts"
type Posts struct {
Id uuid.UUID `db:"id"`
UserId uuid.UUID `db:"user_id"`
PostId uuid.UUID `db:"post_id"`
}
func CreatePosts(userId uuid.UUID, postId uuid.UUID) (posts Posts, err error) {
posts = Posts{
Id: uuid.New(),
UserId: userId,
PostId: postId,
}
err = posts.Validate()
return
}
func (p *Posts) Validate() error {
if err := uuid.Validate(p.Id.String()); err != nil {
return errors.New("Invalid posts.id")
}
if err := uuid.Validate(p.UserId.String()); err != nil {
return errors.New("Invalid posts.userId")
}
if err := uuid.Validate(p.PostId.String()); err != nil {
return errors.New("Invalid posts.postId")
}
return nil
}

View file

@ -1,17 +0,0 @@
package repository
import (
"58team_blog/internal/domain/entities"
"github.com/google/uuid"
)
type PostsRepository interface {
Create(*entities.Posts) (*entities.Posts, error)
FindById(uuid.UUID) (*entities.Posts, error)
FindByPostId(uuid.UUID) (*entities.Posts, error)
FindByUserId(uuid.UUID) (*entities.Posts, error)
FindAllByUserId(uuid.UUID) ([]*entities.Posts, error)
GetAll() ([]*entities.Posts, error)
Delete(uuid.UUID) error
}