This commit is contained in:
2025-02-16 20:24:34 +07:00
parent fd75c678bb
commit 6e52164914
5 changed files with 83 additions and 19 deletions

View File

@@ -14,4 +14,5 @@ func Generate(structName string, fields []entities.Field) {
panic(err) panic(err)
} }
GenerateService(structName, mkPath) GenerateService(structName, mkPath)
GenerateScheme(structName, fields, mkPath)
} }

View File

@@ -1,2 +0,0 @@
package generation

View File

@@ -1 +1,57 @@
package generation package generation
import (
"fmt"
"nto_cli/entities"
"nto_cli/utils"
"os"
"strings"
)
func GenerateScheme(structName string, fields []entities.Field, mkPath string) {
schemeFile, err := os.Create(mkPath + "/" + strings.ToTitle(structName) + "Scheme.vue")
if err != nil {
panic(err)
}
defer schemeFile.Close()
_, err = schemeFile.WriteString(fmt.Sprintf(
`<script setup lang="ts">
import Table from ../table/Table.vue
import { onMounted, reactive } from 'vue'
import { getDefaultValues } from '../utils/structs/defaults.util'
import S from './%s.service.ts'
import type { Scheme } from '../types/scheme.type'
import { %s } from '%s'
const service = new S
onMounted(async () => {
})
const scheme: Scheme = reactive({
%s
})
getDefaults = () => getDefaultValues(scheme)
</script>
<template>
<main class="w-screen h-screen>
<Table :scheme :service :getDefaults></Table>
</main>
</template>
`, strings.ToLower(structName), structName, utils.GetServiceStructType(structName), GenerateFields(fields)))
if err != nil {
panic(err)
}
}
func GenerateFields(fields []entities.Field) string {
result := "{\n"
for _, field := range fields {
result += field.Generate() + ", \n"
}
return result + "\n}"
}

View File

@@ -2,24 +2,12 @@ package generation
import ( import (
"fmt" "fmt"
"nto_cli/utils"
"os" "os"
"strings" "strings"
) )
func GetServiceBindPath(structName string) string {
path := fmt.Sprintf("../../bindings/app/internal/services/%sservice.ts", strings.ToLower(structName))
return path
}
func GetServiceStructType(structName string) string {
path := "../../bindings/app/internal/services/models.ts"
return path
}
func GetServiceType() string {
path := "../types/service.type.ts"
return path
}
func GenerateService(structName, mkPath string) { func GenerateService(structName, mkPath string) {
serviceFile, err := os.Create(mkPath + "/" + strings.ToLower(structName) + ".service.ts") serviceFile, err := os.Create(mkPath + "/" + strings.ToLower(structName) + ".service.ts")
@@ -32,7 +20,7 @@ func GenerateService(structName, mkPath string) {
import type { %s } from "%s" import type { %s } from "%s"
import type { IService } from "%s" import type { IService } from "%s"
export class %sService implements IService<%s> { export default class %sService implements IService<%s> {
async read(id: number) { async read(id: number) {
return await GetById(id) return await GetById(id)
} }
@@ -58,7 +46,7 @@ export class %sService implements IService<%s> {
return await ExportToExcel() return await ExportToExcel()
} }
} }
`, GetServiceBindPath(structName), structName, GetServiceStructType(structName), GetServiceType(), structName, structName, structName, structName, structName)) `, utils.GetServiceBindPath(structName), structName, utils.GetServiceStructType(structName), utils.GetServiceType(), structName, structName, structName, structName, structName))
if err != nil { if err != nil {
panic(err) panic(err)
} }

21
utils/imports.go Normal file
View File

@@ -0,0 +1,21 @@
package utils
import (
"fmt"
"strings"
)
func GetServiceBindPath(structName string) string {
path := fmt.Sprintf("../../bindings/app/internal/services/%sservice.ts", strings.ToLower(structName))
return path
}
func GetServiceStructType(structName string) string {
path := "../../bindings/app/internal/services/models.ts"
return path
}
func GetServiceType() string {
path := "../types/service.type.ts"
return path
}