github.com/ngocphuongnb/tetua@v0.0.7-alpha/packages/entrepository/ent/schema/post.go (about)

     1  package schema
     2  
     3  import (
     4  	"entgo.io/ent"
     5  	"entgo.io/ent/dialect/entsql"
     6  	"entgo.io/ent/schema"
     7  	"entgo.io/ent/schema/edge"
     8  	"entgo.io/ent/schema/field"
     9  	"entgo.io/ent/schema/index"
    10  )
    11  
    12  // Post holds the schema definition for the Post entity.
    13  type Post struct {
    14  	ent.Schema
    15  }
    16  
    17  // Fields of the Post.
    18  func (Post) Fields() []ent.Field {
    19  	return []ent.Field{
    20  		field.String("name"),
    21  		field.String("slug"),
    22  		field.String("description").Optional().StructTag(`validate:"max=255"`),
    23  		field.Text("content").StructTag(`validate:"required"`),
    24  		field.Text("content_html"),
    25  		field.Int64("view_count").Default(0),
    26  		field.Int64("comment_count").Default(0),
    27  		field.Int64("rating_count").Optional().Default(0),
    28  		field.Int64("rating_total").Optional().Default(0),
    29  		field.Bool("draft").Optional().Default(false),
    30  		field.Bool("approved").Optional().Default(false),
    31  		field.Int("featured_image_id").Optional(),
    32  		field.Int("user_id").Optional(),
    33  	}
    34  }
    35  
    36  // Edges of the Post.
    37  func (Post) Edges() []ent.Edge {
    38  	return []ent.Edge{
    39  		edge.From("user", User.Type).Ref("posts").Field("user_id").Unique(),
    40  		edge.From("topics", Topic.Type).Ref("posts"),
    41  		edge.From("featured_image", File.Type).Ref("posts").Field("featured_image_id").Unique(),
    42  		edge.To("comments", Comment.Type).
    43  			Annotations(ondeleteSetNull).
    44  			StorageKey(edge.Column("post_id"), edge.Symbol("comment_post")),
    45  	}
    46  }
    47  
    48  func (Post) Indexes() []ent.Index {
    49  	return []ent.Index{
    50  		index.Fields("name").StorageKey("name_idx"),
    51  		index.Fields("view_count").StorageKey("view_count_idx"),
    52  	}
    53  }
    54  
    55  func (Post) Annotations() []schema.Annotation {
    56  	return []schema.Annotation{
    57  		entsql.Annotation{
    58  			Charset:   "utf8mb4",
    59  			Collation: "utf8mb4_unicode_ci",
    60  		},
    61  	}
    62  }
    63  
    64  func (Post) Mixin() []ent.Mixin {
    65  	return []ent.Mixin{
    66  		TimeStamp{},
    67  	}
    68  }