feat: full sorting

This commit is contained in:
2025-03-12 20:57:36 +07:00
parent 7e140bd2ce
commit fc575fb2ab
12 changed files with 60 additions and 516 deletions

View File

@@ -1,4 +0,0 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
export * from "./models.js";

View File

@@ -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<Author> = {}) {
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<Author>);
}
}
export class Comment {
"Id": number;
"Text": string;
"AuthorId": number;
"Author": Author;
"Posts": Post[];
/** Creates a new Comment instance. */
constructor($$source: Partial<Comment> = {}) {
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<Comment>);
}
}
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<Post> = {}) {
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<Post>);
}
}
export class PostType {
"Id": number;
"Name": string;
/** Creates a new PostType instance. */
constructor($$source: Partial<PostType> = {}) {
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<PostType>);
}
}
// 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;

View File

@@ -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<number> & { 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<void> & { 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);

View File

@@ -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<number> & { 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<void> & { 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);

View File

@@ -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";

View File

@@ -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;

View File

@@ -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<number> & { 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<void> & { cancel(): void } {
let $resultPromise = $Call.ByID(2924549135, id) as any;
return $resultPromise;
}
export function ExportToExcel(): Promise<void> & { 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);

View File

@@ -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<number> & { 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<void> & { 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<void> & { 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);

View File

@@ -2,6 +2,8 @@
import { Dialog } from "primevue"; import { Dialog } from "primevue";
import { useErrorStore } from "./stores/error.store"; import { useErrorStore } from "./stores/error.store";
import { RouterView } from "vue-router"; import { RouterView } from "vue-router";
import AuthorScheme from "./author/AuthorScheme.vue";
import PostScheme from "./post/PostScheme.vue";
const errorStore = useErrorStore(); const errorStore = useErrorStore();
</script> </script>
@@ -17,5 +19,6 @@ const errorStore = useErrorStore();
</Dialog> </Dialog>
<main class="w-screen h-screen"> <main class="w-screen h-screen">
<RouterView /> <RouterView />
<PostScheme />
</main> </main>
</template> </template>

View File

@@ -31,7 +31,7 @@ const load = async () => {
onMounted(async () => { onMounted(async () => {
await load(); await load();
console.log(await SortedByOrder({"Name": "ASC"})) console.log(await SortedByOrder({"Comments": "DESC"}))
}); });
const scheme: Scheme<Author> = reactive({ const scheme: Scheme<Author> = reactive({

View File

@@ -15,6 +15,8 @@ import PosttypeService from "../posttype/posttype.service.ts";
const posttypeService = new PosttypeService(); const posttypeService = new PosttypeService();
import CommentService from "../comment/comment.service.ts"; import CommentService from "../comment/comment.service.ts";
import {SortedByOrder} from "../../bindings/app/internal/services/postservice.ts";
const commentService = new CommentService(); const commentService = new CommentService();
const service = new Service(); const service = new Service();
@@ -36,6 +38,7 @@ const load = async () => {
onMounted(async () => { onMounted(async () => {
await load(); await load();
console.log(await SortedByOrder({"Author": "DESC", "Text": "ASC"}));
}); });
const scheme: Scheme<Post> = reactive({ const scheme: Scheme<Post> = reactive({

View File

@@ -4,7 +4,10 @@ import (
"app/internal/database" "app/internal/database"
"errors" "errors"
"fmt" "fmt"
"github.com/kuzgoga/fogg"
"gorm.io/gorm/clause"
"reflect" "reflect"
"strings"
) )
// SortByOrder Order items by specified field and a sort type // SortByOrder Order items by specified field and a sort type
@@ -13,28 +16,73 @@ import (
// DESC - по убыванию (от Я до А) // DESC - по убыванию (от Я до А)
func SortByOrder[T any](fieldsSortOrder map[string]string, entity T) ([]*T, error) { func SortByOrder[T any](fieldsSortOrder map[string]string, entity T) ([]*T, error) {
var ( var (
orderQuery string orderQuery []string
items []*T items []*T
joins []string
) )
for name, order := range fieldsSortOrder { for name, order := range fieldsSortOrder {
structInfo := reflect.ValueOf(entity).Type() structInfo := reflect.ValueOf(entity).Type()
_, fieldExist := structInfo.FieldByName(name) field, fieldExist := structInfo.FieldByName(name)
if !fieldExist { if !fieldExist {
return nil, errors.New(fmt.Sprintf("Field `%s` not found", name)) 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)) 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))
} }
result := database.GetInstance().Order(orderQuery).Find(&items) 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))
}
}
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 { if result.Error != nil {
return items, result.Error return items, result.Error
} }
return items, nil return items, nil
} }