fix: delete method
This commit is contained in:
@@ -28,6 +28,7 @@ func newPost(db *gorm.DB, opts ...gen.DOOption) post {
|
||||
_post.ALL = field.NewAsterisk(tableName)
|
||||
_post.Id = field.NewUint(tableName, "id")
|
||||
_post.Text = field.NewString(tableName, "text")
|
||||
_post.CreatedAt = field.NewInt64(tableName, "created_at")
|
||||
|
||||
_post.fillFieldMap()
|
||||
|
||||
@@ -37,9 +38,10 @@ func newPost(db *gorm.DB, opts ...gen.DOOption) post {
|
||||
type post struct {
|
||||
postDo
|
||||
|
||||
ALL field.Asterisk
|
||||
Id field.Uint
|
||||
Text field.String
|
||||
ALL field.Asterisk
|
||||
Id field.Uint
|
||||
Text field.String
|
||||
CreatedAt field.Int64
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
@@ -58,6 +60,7 @@ func (p *post) updateTableName(table string) *post {
|
||||
p.ALL = field.NewAsterisk(table)
|
||||
p.Id = field.NewUint(table, "id")
|
||||
p.Text = field.NewString(table, "text")
|
||||
p.CreatedAt = field.NewInt64(table, "created_at")
|
||||
|
||||
p.fillFieldMap()
|
||||
|
||||
@@ -74,9 +77,10 @@ func (p *post) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
}
|
||||
|
||||
func (p *post) fillFieldMap() {
|
||||
p.fieldMap = make(map[string]field.Expr, 2)
|
||||
p.fieldMap = make(map[string]field.Expr, 3)
|
||||
p.fieldMap["id"] = p.Id
|
||||
p.fieldMap["text"] = p.Text
|
||||
p.fieldMap["created_at"] = p.CreatedAt
|
||||
}
|
||||
|
||||
func (p post) clone(db *gorm.DB) post {
|
||||
|
||||
@@ -35,9 +35,9 @@ func (service *AuthorService) Update(item Author) (Author, error) {
|
||||
err := dal.Author.Preload(field.Associations).Save(&item)
|
||||
return item, err
|
||||
}
|
||||
func (service *AuthorService) Delete(item Author) (Author, error) {
|
||||
_, err := dal.Author.Unscoped().Preload(field.Associations).Delete(&item)
|
||||
return item, err
|
||||
func (service *AuthorService) Delete(id uint) error {
|
||||
_, err := dal.Author.Unscoped().Where(dal.Author.Id.Eq(id)).Delete()
|
||||
return err
|
||||
}
|
||||
func (service *AuthorService) Count() (int64, error) {
|
||||
amount, err := dal.Author.Count()
|
||||
|
||||
@@ -34,14 +34,17 @@ func (service *PostService) GetById(id uint) (*Post, error) {
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func (service *PostService) Update(item Post) (Post, error) {
|
||||
err := dal.Post.Preload(field.Associations).Save(&item)
|
||||
return item, err
|
||||
}
|
||||
func (service *PostService) Delete(item Post) (Post, error) {
|
||||
_, err := dal.Post.Unscoped().Preload(field.Associations).Delete(&item)
|
||||
return item, err
|
||||
|
||||
func (service *PostService) Delete(id uint) error {
|
||||
_, err := dal.Post.Unscoped().Where(dal.Post.Id.Eq(id)).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
func (service *PostService) Count() (int64, error) {
|
||||
amount, err := dal.Post.Count()
|
||||
return amount, err
|
||||
|
||||
@@ -5,6 +5,6 @@ type Service[T any] interface {
|
||||
GetById(id uint) (*T, error)
|
||||
Create(item T) (T, error)
|
||||
Update(item T) (T, error)
|
||||
Delete(item T) (T, error)
|
||||
Delete(id uint) error
|
||||
Count() (int64, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user