feat: relatives paths

This commit is contained in:
2025-02-14 13:26:44 +07:00
parent 013a3be72b
commit 8290462d92
5 changed files with 42 additions and 24 deletions

View File

@@ -2,45 +2,64 @@ package generation
import (
"fmt"
"nto_cli/utils"
"os"
"strings"
)
func GetServiceBindPath(structName string) string {
path := utils.FindFrontendPath()
path += fmt.Sprintf("/bindings/app/internal/services/%sservice.ts", strings.ToLower(structName))
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) {
serviceFile, err := os.Create(mkPath + "/" + strings.ToLower(structName) + ".service.ts")
serviceFile, err := os.Create(mkPath + "/" + strings.ToLower(structName) + ".service.ts")
if err != nil {
panic(err)
}
defer serviceFile.Close()
_, err = serviceFile.WriteString(fmt.Sprintf(`export class %sService {
async read() {
_, err = serviceFile.WriteString(
fmt.Sprintf(`import { GetAll, Create, Delete, ExportToExcel, GetById, Update, Count } from "%s"
import type { %s } from "%s"
import type { Service } from "%s"
export class %sService implements Service {
async read(id: number) {
return await GetById(id)
}
async readAll() {
return await GetAll()
}
async create() {
async create(item: %s) {
return await Create(item)
}
async delete() {
async delete(item: %s) {
return await Delete(item)
}
async update() {
async update(item: %s) {
return await Update(item)
}
async count() {
return await Count()
}
async exportToExcel() {
return await ExportToExcel()
}
}
`, structName))
`, GetServiceBindPath(structName), structName, GetServiceStructType(structName), GetServiceType(), structName, structName, structName, structName))
if err != nil {
panic(err)
}
}
}