This commit is contained in:
saeid_01 2023-11-17 23:50:29 +03:30
parent e8445a981b
commit 46547b6743
2 changed files with 53 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import (
func main() {
name := "order"
createSchemaEnt(name)
createDomain(name)
createEntity(name)
createHandler(name)
@ -18,6 +19,27 @@ func main() {
createRouter(name)
}
func createSchemaEnt(name string) {
fileName := "../../ent/schema/" + name + ".go"
file, err := os.Create(fileName)
if err != nil {
fmt.Println("error in create file on schema:", err)
return
}
defer file.Close()
replaced := replace(getFile("schema.text"), name)
_, err = file.WriteString(replaced)
if err != nil {
fmt.Println("error in write file content on schema:", err)
return
}
fmt.Printf("%s file created.\n", fileName)
}
func createRouter(name string) {
fileName := "../../api/handlers/" + name + "s/router.go"

View File

@ -0,0 +1,31 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"time"
)
// {Name} holds the schema definition for the {Name} entity.
type {Name} struct {
ent.Schema
}
// Fields of the {Name}.
func ({Name}) Fields() []ent.Field {
return []ent.Field{
field.Int("business_id").Optional().Nillable(),
field.Int("user_id").Optional().Nillable(),
field.Time("created_at").Default(time.Now()),
field.Time("updated_at").Default(time.Now()).UpdateDefault(time.Now),
}
}
// Edges of the {Name}.
func ({Name}) Edges() []ent.Edge {
return []ent.Edge{
edge.From("businesses", Business.Type).Ref("{name}s").Unique().Field("business_id"),
edge.From("users", User.Type).Ref("{name}s").Unique().Field("user_id"),
}
}