fix: create

This commit is contained in:
2025-03-09 20:17:42 +07:00
parent 7c7c8e6cc5
commit 6bce64f159
4 changed files with 60 additions and 9 deletions

View File

@@ -20,7 +20,7 @@ export function Count(): Promise<number> & { cancel(): void } {
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) => {
let $typingPromise = $resultPromise.then(($result) => {
return $$createType0($result);
}) as any;
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);
@@ -34,7 +34,7 @@ export function Delete(id: number): Promise<void> & { cancel(): void } {
export function GetAll(): Promise<($models.Author | null)[]> & { cancel(): void } {
let $resultPromise = $Call.ByID(3248293926) as any;
let $typingPromise = $resultPromise.then(($result: any) => {
let $typingPromise = $resultPromise.then(($result) => {
return $$createType2($result);
}) as any;
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);
@@ -43,7 +43,7 @@ export function GetAll(): Promise<($models.Author | null)[]> & { cancel(): void
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) => {
let $typingPromise = $resultPromise.then(($result) => {
return $$createType1($result);
}) as any;
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);
@@ -52,7 +52,7 @@ export function GetById(id: number): Promise<$models.Author | null> & { cancel()
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) => {
let $typingPromise = $resultPromise.then(($result) => {
return $$createType0($result);
}) as any;
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);

View File

@@ -20,7 +20,7 @@ export function Count(): Promise<number> & { cancel(): void } {
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) => {
let $typingPromise = $resultPromise.then(($result) => {
return $$createType0($result);
}) as any;
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);
@@ -39,7 +39,7 @@ export function ExportToExcel(): Promise<void> & { cancel(): void } {
export function GetAll(): Promise<($models.Post | null)[]> & { cancel(): void } {
let $resultPromise = $Call.ByID(65691059) as any;
let $typingPromise = $resultPromise.then(($result: any) => {
let $typingPromise = $resultPromise.then(($result) => {
return $$createType2($result);
}) as any;
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);
@@ -48,7 +48,7 @@ export function GetAll(): Promise<($models.Post | null)[]> & { cancel(): void }
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) => {
let $typingPromise = $resultPromise.then(($result) => {
return $$createType1($result);
}) as any;
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);
@@ -57,7 +57,7 @@ export function GetById(id: number): Promise<$models.Post | null> & { cancel():
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) => {
let $typingPromise = $resultPromise.then(($result) => {
return $$createType0($result);
}) as any;
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);

View File

@@ -0,0 +1,45 @@
package services
import (
"errors"
"reflect"
"gorm.io/gorm"
)
// AppendAssociations uses reflection to find all exported slice fields
// in 'item' and then appends any elements in each slice to the association.
func AppendAssociations(db *gorm.DB, item interface{}) error {
// We expect a pointer to a struct so we can do db.Model(item).
val := reflect.ValueOf(item)
if val.Kind() != reflect.Ptr {
return errors.New("item must be a pointer to a struct")
}
elem := val.Elem()
if elem.Kind() != reflect.Struct {
return errors.New("item must be a pointer to a struct")
}
t := elem.Type()
for i := 0; i < elem.NumField(); i++ {
fieldVal := elem.Field(i)
fieldType := t.Field(i)
// Process only exported fields (PkgPath == "") and slices.
if fieldType.PkgPath == "" && fieldVal.Kind() == reflect.Slice {
// The association name is the struct field name by default.
assocName := fieldType.Name
// Append slice elements to the existing association.
// You can add checks here if you want to skip empty slices, etc.
if fieldVal.Len() > 0 {
if err := db.Model(item).Association(assocName).Append(fieldVal.Interface()); err != nil {
return err
}
}
}
}
return nil
}

View File

@@ -15,14 +15,20 @@ type Author = models.Author
func (service *AuthorService) Create(item Author) (Author, error) {
ReplaceEmptySlicesWithNil(&item)
err := dal.Author.Preload(field.Associations).Create(&item)
err := dal.Author.Create(&item)
if err != nil {
return item, err
}
err = 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 {