feat: entities

This commit is contained in:
2025-03-09 21:51:16 +07:00
parent bd4be08c37
commit baf4c67cfb
21 changed files with 2171 additions and 62 deletions

View File

@@ -0,0 +1,61 @@
package services
import (
"app/internal/dal"
"app/internal/database"
"app/internal/models"
"errors"
"gorm.io/gen/field"
"gorm.io/gorm"
)
type PostTypeService struct {
}
type PostType = models.PostType
func (service *PostTypeService) Create(item PostType) (PostType, error) {
ReplaceEmptySlicesWithNil(&item)
err := dal.PostType.Preload(field.Associations).Create(&item)
if err != nil {
return item, err
}
err = AppendAssociations(database.GetInstance(), &item)
return item, err
}
func (service *PostTypeService) GetAll() ([]*PostType, error) {
var posttypes []*PostType
posttypes, err := dal.PostType.Preload(field.Associations).Find()
return posttypes, err
}
func (service *PostTypeService) GetById(id uint) (*PostType, error) {
item, err := dal.PostType.Preload(field.Associations).Where(dal.PostType.Id.Eq(id)).First()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
} else {
return nil, err
}
}
return item, nil
}
func (service *PostTypeService) Update(item PostType) (PostType, error) {
ReplaceEmptySlicesWithNil(&item)
err := dal.PostType.Preload(field.Associations).Save(&item)
if err != nil {
return item, err
}
err = UpdateAssociations(database.GetInstance(), &item)
if err != nil {
return item, err
}
return item, err
}
func (service *PostTypeService) Delete(id uint) error {
_, err := dal.PostType.Unscoped().Where(dal.PostType.Id.Eq(id)).Delete()
return err
}
func (service *PostTypeService) Count() (int64, error) {
amount, err := dal.PostType.Count()
return amount, err
}