feat: sorting for primitive fields
# Conflicts: # frontend/src/App.vue
This commit is contained in:
@@ -4,31 +4,30 @@ import (
|
||||
"app/internal/dal"
|
||||
"app/internal/database"
|
||||
"app/internal/models"
|
||||
"app/internal/utils"
|
||||
"errors"
|
||||
|
||||
"gorm.io/gen/field"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type AuthorService struct{}
|
||||
type AuthorService struct {
|
||||
}
|
||||
type Author = models.Author
|
||||
|
||||
func (service *AuthorService) Create(item Author) (Author, error) {
|
||||
ReplaceEmptySlicesWithNil(&item)
|
||||
utils.ReplaceEmptySlicesWithNil(&item)
|
||||
err := dal.Author.Create(&item)
|
||||
if err != nil {
|
||||
return item, err
|
||||
}
|
||||
err = AppendAssociations(database.GetInstance(), &item)
|
||||
err = utils.AppendAssociations(database.GetInstance(), &item)
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (service *AuthorService) GetAll() ([]*Author, error) {
|
||||
var authors []*Author
|
||||
authors, err := dal.Author.Preload(field.Associations).Find()
|
||||
return authors, err
|
||||
}
|
||||
|
||||
func (service *AuthorService) GetById(id uint) (*Author, error) {
|
||||
item, err := dal.Author.Preload(field.Associations).Where(dal.Author.Id.Eq(id)).First()
|
||||
if err != nil {
|
||||
@@ -40,16 +39,13 @@ func (service *AuthorService) GetById(id uint) (*Author, error) {
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func (service *AuthorService) Update(item Author) (Author, error) {
|
||||
ReplaceEmptySlicesWithNil(&item)
|
||||
|
||||
utils.ReplaceEmptySlicesWithNil(&item)
|
||||
_, err := dal.Author.Updates(&item)
|
||||
if err != nil {
|
||||
return item, err
|
||||
}
|
||||
|
||||
err = UpdateAssociations(database.GetInstance(), &item)
|
||||
err = utils.UpdateAssociations(database.GetInstance(), &item)
|
||||
|
||||
if err != nil {
|
||||
return item, err
|
||||
@@ -57,13 +53,15 @@ func (service *AuthorService) Update(item Author) (Author, error) {
|
||||
|
||||
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()
|
||||
return amount, err
|
||||
}
|
||||
|
||||
func (service *AuthorService) SortedByOrder(fieldsSortOrder map[string]string) ([]*Author, error) {
|
||||
return utils.SortByOrder(fieldsSortOrder, Author{})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user