feat: sorting for all entities

This commit is contained in:
2025-03-12 16:24:28 +07:00
parent 8cf6d7ed73
commit 7e140bd2ce
4 changed files with 11 additions and 10 deletions

View File

@@ -61,7 +61,6 @@ 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{})
}

View File

@@ -6,7 +6,6 @@ import (
"app/internal/models"
"app/internal/utils"
"errors"
"gorm.io/gen/field"
"gorm.io/gorm"
)
@@ -17,20 +16,18 @@ type Comment = models.Comment
func (service *CommentService) Create(item Comment) (Comment, error) {
utils.ReplaceEmptySlicesWithNil(&item)
err := dal.Comment.Preload(field.Associations).Create(&item)
err := dal.Comment.Create(&item)
if err != nil {
return item, err
}
err = utils.AppendAssociations(database.GetInstance(), &item)
return item, err
}
func (service *CommentService) GetAll() ([]*Comment, error) {
var comments []*Comment
comments, err := dal.Comment.Preload(field.Associations).Find()
return comments, err
}
func (service *CommentService) GetById(id uint) (*Comment, error) {
item, err := dal.Comment.Preload(field.Associations).Where(dal.Comment.Id.Eq(id)).First()
if err != nil {
@@ -42,14 +39,12 @@ func (service *CommentService) GetById(id uint) (*Comment, error) {
}
return item, nil
}
func (service *CommentService) Update(item Comment) (Comment, error) {
utils.ReplaceEmptySlicesWithNil(&item)
err := dal.Comment.Preload(field.Associations).Save(&item)
_, err := dal.Author.Updates(&item)
if err != nil {
return item, err
}
err = utils.UpdateAssociations(database.GetInstance(), &item)
if err != nil {
@@ -58,13 +53,14 @@ func (service *CommentService) Update(item Comment) (Comment, error) {
return item, err
}
func (service *CommentService) Delete(id uint) error {
_, err := dal.Comment.Unscoped().Where(dal.Comment.Id.Eq(id)).Delete()
return err
}
func (service *CommentService) Count() (int64, error) {
amount, err := dal.Comment.Count()
return amount, err
}
func (service *CommentService) SortedByOrder(fieldsSortOrder map[string]string) ([]*Comment, error) {
return utils.SortByOrder(fieldsSortOrder, Comment{})
}

View File

@@ -75,3 +75,6 @@ func (service *PostService) ExportToExcel() {
dialogs.ErrorDialog("Ошибка экспорта", fmt.Sprintf("Ошибка при экспорте данных: %s", err))
}
}
func (service *PostService) SortedByOrder(fieldsSortOrder map[string]string) ([]*Post, error) {
return utils.SortByOrder(fieldsSortOrder, Post{})
}

View File

@@ -91,3 +91,6 @@ func (service *PostTypeService) ImportFromExcel() error {
}
return nil
}
func (service *PostTypeService) SortedByOrder(fieldsSortOrder map[string]string) ([]*PostType, error) {
return utils.SortByOrder(fieldsSortOrder, PostType{})
}