Initial commit
This commit is contained in:
commit
c0cb826917
63 changed files with 2069 additions and 0 deletions
68
internal/domain/entities/post.go
Normal file
68
internal/domain/entities/post.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue