BlogBackend/internal/domain/entities/post.go
2025-09-16 13:26:27 +04:00

68 lines
1.3 KiB
Go

package entities
import (
"errors"
"time"
"github.com/google/uuid"
)
const PostTable = "post"
type Post struct {
Id uuid.UUID `db:"id"`
UserId uuid.UUID `db:"user_id"`
Title string `db:"title"`
Description string `db:"description"`
Content string `db:"content"`
CreatedAt time.Time `db:"createdAt"`
UpdatedAt time.Time `db:"updatedAt"`
}
func CreatePost(userId uuid.UUID, title string, description string, content string) (post Post, err error) {
post = Post{
Id: uuid.New(),
UserId: userId,
Title: title,
Description: description,
Content: content,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
err = post.Validate()
return
}
func (p *Post) Validate() error {
if err := uuid.Validate(p.Id.String()); err != nil {
return errors.New("Empty post.id")
}
if err := uuid.Validate(p.UserId.String()); err != nil {
return errors.New("Empty post.userId")
}
if p.Title == "" {
return errors.New("Empty post.title")
}
if p.Description == "" {
return errors.New("Empty post.description")
}
if p.Content == "" {
return errors.New("Empty post.content path")
}
if p.CreatedAt.IsZero() {
return errors.New("Empty post.createdAt")
}
if p.UpdatedAt.IsZero() {
return errors.New("Empty post.updatedAt")
}
return nil
}