From fc575fb2abd305aeee0e064064aee0d3184aa2b1 Mon Sep 17 00:00:00 2001 From: gogacoder Date: Wed, 12 Mar 2025 20:57:36 +0700 Subject: [PATCH] feat: full sorting --- .../bindings/app/internal/models/index.ts | 4 - .../bindings/app/internal/models/models.ts | 190 ------------------ .../app/internal/services/authorservice.ts | 74 ------- .../app/internal/services/commentservice.ts | 65 ------ .../bindings/app/internal/services/index.ts | 15 -- .../bindings/app/internal/services/models.ts | 22 -- .../app/internal/services/postservice.ts | 70 ------- .../app/internal/services/posttypeservice.ts | 70 ------- frontend/src/App.vue | 3 + frontend/src/author/AuthorScheme.vue | 2 +- frontend/src/post/PostScheme.vue | 3 + internal/utils/sorting.go | 58 +++++- 12 files changed, 60 insertions(+), 516 deletions(-) delete mode 100644 frontend/bindings/app/internal/models/index.ts delete mode 100644 frontend/bindings/app/internal/models/models.ts delete mode 100644 frontend/bindings/app/internal/services/authorservice.ts delete mode 100644 frontend/bindings/app/internal/services/commentservice.ts delete mode 100644 frontend/bindings/app/internal/services/index.ts delete mode 100644 frontend/bindings/app/internal/services/models.ts delete mode 100644 frontend/bindings/app/internal/services/postservice.ts delete mode 100644 frontend/bindings/app/internal/services/posttypeservice.ts diff --git a/frontend/bindings/app/internal/models/index.ts b/frontend/bindings/app/internal/models/index.ts deleted file mode 100644 index c9d993a..0000000 --- a/frontend/bindings/app/internal/models/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL -// This file is automatically generated. DO NOT EDIT - -export * from "./models.js"; diff --git a/frontend/bindings/app/internal/models/models.ts b/frontend/bindings/app/internal/models/models.ts deleted file mode 100644 index 97c7442..0000000 --- a/frontend/bindings/app/internal/models/models.ts +++ /dev/null @@ -1,190 +0,0 @@ -// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL -// This file is automatically generated. DO NOT EDIT - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import {Create as $Create} from "@wailsio/runtime"; - -export class Author { - "Id": number; - "Name": string; - "Posts": Post[]; - "Comments": Comment[]; - - /** Creates a new Author instance. */ - constructor($$source: Partial = {}) { - if (!("Id" in $$source)) { - this["Id"] = 0; - } - if (!("Name" in $$source)) { - this["Name"] = ""; - } - if (!("Posts" in $$source)) { - this["Posts"] = []; - } - if (!("Comments" in $$source)) { - this["Comments"] = []; - } - - Object.assign(this, $$source); - } - - /** - * Creates a new Author instance from a string or object. - */ - static createFrom($$source: any = {}): Author { - const $$createField2_0 = $$createType1; - const $$createField3_0 = $$createType3; - let $$parsedSource = typeof $$source === 'string' ? JSON.parse($$source) : $$source; - if ("Posts" in $$parsedSource) { - $$parsedSource["Posts"] = $$createField2_0($$parsedSource["Posts"]); - } - if ("Comments" in $$parsedSource) { - $$parsedSource["Comments"] = $$createField3_0($$parsedSource["Comments"]); - } - return new Author($$parsedSource as Partial); - } -} - -export class Comment { - "Id": number; - "Text": string; - "AuthorId": number; - "Author": Author; - "Posts": Post[]; - - /** Creates a new Comment instance. */ - constructor($$source: Partial = {}) { - if (!("Id" in $$source)) { - this["Id"] = 0; - } - if (!("Text" in $$source)) { - this["Text"] = ""; - } - if (!("AuthorId" in $$source)) { - this["AuthorId"] = 0; - } - if (!("Author" in $$source)) { - this["Author"] = (new Author()); - } - if (!("Posts" in $$source)) { - this["Posts"] = []; - } - - Object.assign(this, $$source); - } - - /** - * Creates a new Comment instance from a string or object. - */ - static createFrom($$source: any = {}): Comment { - const $$createField3_0 = $$createType4; - const $$createField4_0 = $$createType1; - let $$parsedSource = typeof $$source === 'string' ? JSON.parse($$source) : $$source; - if ("Author" in $$parsedSource) { - $$parsedSource["Author"] = $$createField3_0($$parsedSource["Author"]); - } - if ("Posts" in $$parsedSource) { - $$parsedSource["Posts"] = $$createField4_0($$parsedSource["Posts"]); - } - return new Comment($$parsedSource as Partial); - } -} - -export class Post { - "Id": number; - "Text": string; - "Deadline": number; - "CreatedAt": number; - "AuthorId": number; - "Author": Author; - "PostTypeId": number; - "PostType": PostType; - "Comments": Comment[]; - - /** Creates a new Post instance. */ - constructor($$source: Partial = {}) { - if (!("Id" in $$source)) { - this["Id"] = 0; - } - if (!("Text" in $$source)) { - this["Text"] = ""; - } - if (!("Deadline" in $$source)) { - this["Deadline"] = 0; - } - if (!("CreatedAt" in $$source)) { - this["CreatedAt"] = 0; - } - if (!("AuthorId" in $$source)) { - this["AuthorId"] = 0; - } - if (!("Author" in $$source)) { - this["Author"] = (new Author()); - } - if (!("PostTypeId" in $$source)) { - this["PostTypeId"] = 0; - } - if (!("PostType" in $$source)) { - this["PostType"] = (new PostType()); - } - if (!("Comments" in $$source)) { - this["Comments"] = []; - } - - Object.assign(this, $$source); - } - - /** - * Creates a new Post instance from a string or object. - */ - static createFrom($$source: any = {}): Post { - const $$createField5_0 = $$createType4; - const $$createField7_0 = $$createType5; - const $$createField8_0 = $$createType3; - let $$parsedSource = typeof $$source === 'string' ? JSON.parse($$source) : $$source; - if ("Author" in $$parsedSource) { - $$parsedSource["Author"] = $$createField5_0($$parsedSource["Author"]); - } - if ("PostType" in $$parsedSource) { - $$parsedSource["PostType"] = $$createField7_0($$parsedSource["PostType"]); - } - if ("Comments" in $$parsedSource) { - $$parsedSource["Comments"] = $$createField8_0($$parsedSource["Comments"]); - } - return new Post($$parsedSource as Partial); - } -} - -export class PostType { - "Id": number; - "Name": string; - - /** Creates a new PostType instance. */ - constructor($$source: Partial = {}) { - if (!("Id" in $$source)) { - this["Id"] = 0; - } - if (!("Name" in $$source)) { - this["Name"] = ""; - } - - Object.assign(this, $$source); - } - - /** - * Creates a new PostType instance from a string or object. - */ - static createFrom($$source: any = {}): PostType { - let $$parsedSource = typeof $$source === 'string' ? JSON.parse($$source) : $$source; - return new PostType($$parsedSource as Partial); - } -} - -// Private type creation functions -const $$createType0 = Post.createFrom; -const $$createType1 = $Create.Array($$createType0); -const $$createType2 = Comment.createFrom; -const $$createType3 = $Create.Array($$createType2); -const $$createType4 = Author.createFrom; -const $$createType5 = PostType.createFrom; diff --git a/frontend/bindings/app/internal/services/authorservice.ts b/frontend/bindings/app/internal/services/authorservice.ts deleted file mode 100644 index 8d42285..0000000 --- a/frontend/bindings/app/internal/services/authorservice.ts +++ /dev/null @@ -1,74 +0,0 @@ -// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL -// This file is automatically generated. DO NOT EDIT - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import {Call as $Call, Create as $Create} from "@wailsio/runtime"; - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import * as models$0 from "../models/models.js"; - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import * as $models from "./models.js"; - -export function Count(): Promise & { cancel(): void } { - let $resultPromise = $Call.ByID(3969879864) as any; - return $resultPromise; -} - -export function Create(item: $models.Author): Promise<$models.Author> & { cancel(): void } { - let $resultPromise = $Call.ByID(3684602449, item) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType0($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -export function Delete(id: number): Promise & { cancel(): void } { - let $resultPromise = $Call.ByID(2096845974, id) as any; - return $resultPromise; -} - -export function GetAll(): Promise<($models.Author | null)[]> & { cancel(): void } { - let $resultPromise = $Call.ByID(3248293926) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType2($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -export function GetById(id: number): Promise<$models.Author | null> & { cancel(): void } { - let $resultPromise = $Call.ByID(1703016211, id) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType1($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -export function SortedByOrder(fieldsSortOrder: { [_: string]: string }): Promise<($models.Author | null)[]> & { cancel(): void } { - let $resultPromise = $Call.ByID(3046628691, fieldsSortOrder) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType2($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -export function Update(item: $models.Author): Promise<$models.Author> & { cancel(): void } { - let $resultPromise = $Call.ByID(2240704960, item) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType0($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -// Private type creation functions -const $$createType0 = models$0.Author.createFrom; -const $$createType1 = $Create.Nullable($$createType0); -const $$createType2 = $Create.Array($$createType1); diff --git a/frontend/bindings/app/internal/services/commentservice.ts b/frontend/bindings/app/internal/services/commentservice.ts deleted file mode 100644 index f7ccaf9..0000000 --- a/frontend/bindings/app/internal/services/commentservice.ts +++ /dev/null @@ -1,65 +0,0 @@ -// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL -// This file is automatically generated. DO NOT EDIT - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import {Call as $Call, Create as $Create} from "@wailsio/runtime"; - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import * as models$0 from "../models/models.js"; - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import * as $models from "./models.js"; - -export function Count(): Promise & { cancel(): void } { - let $resultPromise = $Call.ByID(3225397984) as any; - return $resultPromise; -} - -export function Create(item: $models.Comment): Promise<$models.Comment> & { cancel(): void } { - let $resultPromise = $Call.ByID(4239106089, item) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType0($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -export function Delete(id: number): Promise & { cancel(): void } { - let $resultPromise = $Call.ByID(2553503582, id) as any; - return $resultPromise; -} - -export function GetAll(): Promise<($models.Comment | null)[]> & { cancel(): void } { - let $resultPromise = $Call.ByID(1805763390) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType2($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -export function GetById(id: number): Promise<$models.Comment | null> & { cancel(): void } { - let $resultPromise = $Call.ByID(3217823099, id) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType1($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -export function Update(item: $models.Comment): Promise<$models.Comment> & { cancel(): void } { - let $resultPromise = $Call.ByID(3080970936, item) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType0($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -// Private type creation functions -const $$createType0 = models$0.Comment.createFrom; -const $$createType1 = $Create.Nullable($$createType0); -const $$createType2 = $Create.Array($$createType1); diff --git a/frontend/bindings/app/internal/services/index.ts b/frontend/bindings/app/internal/services/index.ts deleted file mode 100644 index 50666ce..0000000 --- a/frontend/bindings/app/internal/services/index.ts +++ /dev/null @@ -1,15 +0,0 @@ -// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL -// This file is automatically generated. DO NOT EDIT - -import * as AuthorService from "./authorservice.js"; -import * as CommentService from "./commentservice.js"; -import * as PostService from "./postservice.js"; -import * as PostTypeService from "./posttypeservice.js"; -export { - AuthorService, - CommentService, - PostService, - PostTypeService -}; - -export * from "./models.js"; diff --git a/frontend/bindings/app/internal/services/models.ts b/frontend/bindings/app/internal/services/models.ts deleted file mode 100644 index 41eba10..0000000 --- a/frontend/bindings/app/internal/services/models.ts +++ /dev/null @@ -1,22 +0,0 @@ -// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL -// This file is automatically generated. DO NOT EDIT - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import {Create as $Create} from "@wailsio/runtime"; - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import * as models$0 from "../models/models.js"; - -export const Author = models$0.Author; -export type Author = models$0.Author; - -export const Comment = models$0.Comment; -export type Comment = models$0.Comment; - -export const Post = models$0.Post; -export type Post = models$0.Post; - -export const PostType = models$0.PostType; -export type PostType = models$0.PostType; diff --git a/frontend/bindings/app/internal/services/postservice.ts b/frontend/bindings/app/internal/services/postservice.ts deleted file mode 100644 index 72d6196..0000000 --- a/frontend/bindings/app/internal/services/postservice.ts +++ /dev/null @@ -1,70 +0,0 @@ -// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL -// This file is automatically generated. DO NOT EDIT - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import {Call as $Call, Create as $Create} from "@wailsio/runtime"; - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import * as models$0 from "../models/models.js"; - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import * as $models from "./models.js"; - -export function Count(): Promise & { cancel(): void } { - let $resultPromise = $Call.ByID(3109924027) as any; - return $resultPromise; -} - -export function Create(item: $models.Post): Promise<$models.Post> & { cancel(): void } { - let $resultPromise = $Call.ByID(1443399856, item) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType0($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -export function Delete(id: number): Promise & { cancel(): void } { - let $resultPromise = $Call.ByID(2924549135, id) as any; - return $resultPromise; -} - -export function ExportToExcel(): Promise & { cancel(): void } { - let $resultPromise = $Call.ByID(75322242) as any; - return $resultPromise; -} - -export function GetAll(): Promise<($models.Post | null)[]> & { cancel(): void } { - let $resultPromise = $Call.ByID(65691059) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType2($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -export function GetById(id: number): Promise<$models.Post | null> & { cancel(): void } { - let $resultPromise = $Call.ByID(4074736792, id) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType1($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -export function Update(item: $models.Post): Promise<$models.Post> & { cancel(): void } { - let $resultPromise = $Call.ByID(137798821, item) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType0($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -// Private type creation functions -const $$createType0 = models$0.Post.createFrom; -const $$createType1 = $Create.Nullable($$createType0); -const $$createType2 = $Create.Array($$createType1); diff --git a/frontend/bindings/app/internal/services/posttypeservice.ts b/frontend/bindings/app/internal/services/posttypeservice.ts deleted file mode 100644 index f4445ff..0000000 --- a/frontend/bindings/app/internal/services/posttypeservice.ts +++ /dev/null @@ -1,70 +0,0 @@ -// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL -// This file is automatically generated. DO NOT EDIT - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import {Call as $Call, Create as $Create} from "@wailsio/runtime"; - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import * as models$0 from "../models/models.js"; - -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore: Unused imports -import * as $models from "./models.js"; - -export function Count(): Promise & { cancel(): void } { - let $resultPromise = $Call.ByID(554487955) as any; - return $resultPromise; -} - -export function Create(item: $models.PostType): Promise<$models.PostType> & { cancel(): void } { - let $resultPromise = $Call.ByID(1092898136, item) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType0($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -export function Delete(id: number): Promise & { cancel(): void } { - let $resultPromise = $Call.ByID(2114913543, id) as any; - return $resultPromise; -} - -export function GetAll(): Promise<($models.PostType | null)[]> & { cancel(): void } { - let $resultPromise = $Call.ByID(416231387) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType2($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -export function GetById(id: number): Promise<$models.PostType | null> & { cancel(): void } { - let $resultPromise = $Call.ByID(3237123344, id) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType1($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -export function ImportFromExcel(): Promise & { cancel(): void } { - let $resultPromise = $Call.ByID(1671814244) as any; - return $resultPromise; -} - -export function Update(item: $models.PostType): Promise<$models.PostType> & { cancel(): void } { - let $resultPromise = $Call.ByID(2773888269, item) as any; - let $typingPromise = $resultPromise.then(($result: any) => { - return $$createType0($result); - }) as any; - $typingPromise.cancel = $resultPromise.cancel.bind($resultPromise); - return $typingPromise; -} - -// Private type creation functions -const $$createType0 = models$0.PostType.createFrom; -const $$createType1 = $Create.Nullable($$createType0); -const $$createType2 = $Create.Array($$createType1); diff --git a/frontend/src/App.vue b/frontend/src/App.vue index cb61fe7..ceef776 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -2,6 +2,8 @@ import { Dialog } from "primevue"; import { useErrorStore } from "./stores/error.store"; import { RouterView } from "vue-router"; +import AuthorScheme from "./author/AuthorScheme.vue"; +import PostScheme from "./post/PostScheme.vue"; const errorStore = useErrorStore(); @@ -17,5 +19,6 @@ const errorStore = useErrorStore();
+
diff --git a/frontend/src/author/AuthorScheme.vue b/frontend/src/author/AuthorScheme.vue index 97f3a1c..f2e59a1 100644 --- a/frontend/src/author/AuthorScheme.vue +++ b/frontend/src/author/AuthorScheme.vue @@ -31,7 +31,7 @@ const load = async () => { onMounted(async () => { await load(); - console.log(await SortedByOrder({"Name": "ASC"})) + console.log(await SortedByOrder({"Comments": "DESC"})) }); const scheme: Scheme = reactive({ diff --git a/frontend/src/post/PostScheme.vue b/frontend/src/post/PostScheme.vue index 72d14f6..b5478a7 100644 --- a/frontend/src/post/PostScheme.vue +++ b/frontend/src/post/PostScheme.vue @@ -15,6 +15,8 @@ import PosttypeService from "../posttype/posttype.service.ts"; const posttypeService = new PosttypeService(); import CommentService from "../comment/comment.service.ts"; +import {SortedByOrder} from "../../bindings/app/internal/services/postservice.ts"; + const commentService = new CommentService(); const service = new Service(); @@ -36,6 +38,7 @@ const load = async () => { onMounted(async () => { await load(); + console.log(await SortedByOrder({"Author": "DESC", "Text": "ASC"})); }); const scheme: Scheme = reactive({ diff --git a/internal/utils/sorting.go b/internal/utils/sorting.go index ebaa331..aa2bfcf 100644 --- a/internal/utils/sorting.go +++ b/internal/utils/sorting.go @@ -4,7 +4,10 @@ import ( "app/internal/database" "errors" "fmt" + "github.com/kuzgoga/fogg" + "gorm.io/gorm/clause" "reflect" + "strings" ) // SortByOrder Order items by specified field and a sort type @@ -13,28 +16,73 @@ import ( // DESC - по убыванию (от Я до А) func SortByOrder[T any](fieldsSortOrder map[string]string, entity T) ([]*T, error) { var ( - orderQuery string + orderQuery []string items []*T + joins []string ) for name, order := range fieldsSortOrder { structInfo := reflect.ValueOf(entity).Type() - _, fieldExist := structInfo.FieldByName(name) + field, fieldExist := structInfo.FieldByName(name) if !fieldExist { return nil, errors.New(fmt.Sprintf("Field `%s` not found", name)) } - if order != "ASC" && order != "DESC" { + if strings.ToUpper(order) != "ASC" && strings.ToUpper(order) != "DESC" { return nil, errors.New(fmt.Sprintf("Field `%s` can only be sorted by ASC or DESC", name)) } - orderQuery += fmt.Sprintf("%s %s", name, order) + tag, err := fogg.Parse(string(field.Tag)) + if err != nil { + return nil, errors.New(fmt.Sprintf("Failed to parse tag for `%s` failed: %s", name, err)) + } + + if !tag.HasTag("ui") { + return nil, errors.New(fmt.Sprintf("Field `%s` doesn't have ui tag", name)) + } + + if field.Type.Kind() == reflect.Slice { + return nil, errors.New(fmt.Sprintf("Field `%s` is array and cannot be used for sorting", name)) + } + + fieldPath := tag.GetTag("ui").GetParamOr("field", "") + if fieldPath == "" { + orderQuery = append(orderQuery, fmt.Sprintf("%s %s", name, order)) + } else { + fieldsPathParts := strings.Split(fieldPath, ".") + + if len(fieldsPathParts) > 1 { + return nil, errors.New(fmt.Sprintf("Too complex fieldPath for structure `%s`", name)) + } + + if len(fieldsPathParts) == 0 { + return nil, errors.New(fmt.Sprintf("Invalid field path for `%s` field", name)) + } + + joinPathParts := append([]string{field.Type.Name()}, fieldsPathParts...) + joinField := strings.Join(joinPathParts, ".") + joinTable := field.Type.Name() + joins = append(joins, joinTable) + orderQuery = append(orderQuery, fmt.Sprintf("%s %s", joinField, order)) + } } - result := database.GetInstance().Order(orderQuery).Find(&items) + db := database.GetInstance() + + for _, join := range joins { + db = db.Joins(join) + } + + if len(orderQuery) != 0 { + db = db.Order(strings.Join(orderQuery, ", ")) + } + + result := db.Preload(clause.Associations).Find(&items) + if result.Error != nil { return items, result.Error } + return items, nil }