From 9a7c5ca1fe586d2d32e4ec04a10ccc03e7f581be Mon Sep 17 00:00:00 2001 From: gogacoder Date: Sat, 15 Mar 2025 18:54:09 +0700 Subject: [PATCH] feat: search --- build/Taskfile.common.yml | 2 ++ internal/services/author.go | 3 +++ internal/services/comment.go | 7 +++++-- internal/services/post.go | 3 +++ internal/services/posttype.go | 3 +++ internal/utils/search.go | 22 +++++----------------- 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/build/Taskfile.common.yml b/build/Taskfile.common.yml index 07a9597..763ffc5 100644 --- a/build/Taskfile.common.yml +++ b/build/Taskfile.common.yml @@ -10,6 +10,7 @@ tasks: - ../dal/*.go cmds: - go run gen.go + method: none go:gen:crudgen: summary: Runs crudgen for crud generating @@ -21,6 +22,7 @@ tasks: - go:gen:dal cmds: - crudgen -p internal + method: none go:mod:tidy: summary: Runs `go mod tidy` diff --git a/internal/services/author.go b/internal/services/author.go index ad65f01..ff22f30 100644 --- a/internal/services/author.go +++ b/internal/services/author.go @@ -64,3 +64,6 @@ func (service *AuthorService) Count() (int64, error) { func (service *AuthorService) SortedByOrder(fieldsSortingOrder []utils.SortField) ([]*Author, error) { return utils.SortByOrder(fieldsSortingOrder, Author{}) } +func (service *AuthorService) SearchByAllTextFields(phrase string) ([]*Author, error) { + return utils.FindPhraseByStringFields[Author](phrase, Author{}) +} diff --git a/internal/services/comment.go b/internal/services/comment.go index 881c525..dac6f16 100644 --- a/internal/services/comment.go +++ b/internal/services/comment.go @@ -61,6 +61,9 @@ func (service *CommentService) Count() (int64, error) { amount, err := dal.Comment.Count() return amount, err } -func (service *CommentService) SortedByOrder(fieldsSortOrder []utils.SortField) ([]*Comment, error) { - return utils.SortByOrder(fieldsSortOrder, Comment{}) +func (service *CommentService) SortedByOrder(fieldsSortingOrder []utils.SortField) ([]*Comment, error) { + return utils.SortByOrder(fieldsSortingOrder, Comment{}) +} +func (service *CommentService) SearchByAllTextFields(phrase string) ([]*Comment, error) { + return utils.FindPhraseByStringFields[Comment](phrase, Comment{}) } diff --git a/internal/services/post.go b/internal/services/post.go index 0fd154f..3213803 100644 --- a/internal/services/post.go +++ b/internal/services/post.go @@ -64,3 +64,6 @@ func (service *PostService) Count() (int64, error) { func (service *PostService) SortedByOrder(fieldsSortOrder []utils.SortField) ([]*Post, error) { return utils.SortByOrder(fieldsSortOrder, Post{}) } +func (service *PostService) SearchByAllTextFields(phrase string) ([]*Post, error) { + return utils.FindPhraseByStringFields[Post](phrase, Post{}) +} diff --git a/internal/services/posttype.go b/internal/services/posttype.go index fd082e1..95fd30a 100644 --- a/internal/services/posttype.go +++ b/internal/services/posttype.go @@ -63,3 +63,6 @@ func (service *PostTypeService) Count() (int64, error) { func (service *PostTypeService) SortedByOrder(fieldsSortOrder []utils.SortField) ([]*PostType, error) { return utils.SortByOrder(fieldsSortOrder, PostType{}) } +func (service *PostTypeService) SearchByAllTextFields(phrase string) ([]*PostType, error) { + return utils.FindPhraseByStringFields[PostType](phrase, PostType{}) +} diff --git a/internal/utils/search.go b/internal/utils/search.go index a340cf2..4827abf 100644 --- a/internal/utils/search.go +++ b/internal/utils/search.go @@ -3,39 +3,27 @@ package utils import ( "app/internal/database" "fmt" - "github.com/kuzgoga/fogg" "gorm.io/gorm/clause" "reflect" ) -func FindPhraseByAllFields[T any](phrase string, entity T) ([]*T, error) { +func FindPhraseByStringFields[T any](phrase string, entity T) ([]*T, error) { db := database.GetInstance().Preload(clause.Associations) structType := reflect.TypeOf(entity) for i := 0; i < structType.NumField(); i++ { field := structType.Field(i) - tag, err := fogg.Parse(string(field.Tag)) - if err != nil { - return nil, fmt.Errorf("ошибка при разборе тэга '%s': %w", field.Name, err) - } - if field.Type.Kind() == reflect.Pointer { field.Type = field.Type.Elem() } if field.Type.Kind() == reflect.String { db.Where(fmt.Sprintf("`%s` like ?", field.Name), "%"+phrase+"%") - } else { - if tag.HasTag("ui") { - uiTag := tag.GetTag("ui") - nestedFieldPath := uiTag.GetParamOr("field", "") - if nestedFieldPath != "" { - db.Preload(fmt.Sprintf("%s.%s", field.Name, nestedFieldPath)) - } - } } - } - return nil, nil + + var items []*T + db.Find(&items) + return items, nil }