fix: move some logic to domain entities
This commit is contained in:
@@ -2,6 +2,8 @@ package entity
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
||||
)
|
||||
|
||||
//Book data
|
||||
@@ -14,3 +16,24 @@ type Book struct {
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
//NewBook create a new book
|
||||
func NewBook(title string, author string, pages int, quantity int) (*Book, error) {
|
||||
b := &Book{
|
||||
ID: NewID(),
|
||||
Title: title,
|
||||
Author: author,
|
||||
Pages: pages,
|
||||
Quantity: quantity,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
//Validate validate book
|
||||
func (b *Book) Validate() error {
|
||||
if b.Title == "" || b.Author == "" || b.Pages <= 0 {
|
||||
return domain.ErrInvalidEntity
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user