github.com/ngocphuongnb/tetua@v0.0.7-alpha/packages/entrepository/ent/schema/comment.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 ) 10 11 // Comment holds the schema definition for the Comment entity. 12 type Comment struct { 13 ent.Schema 14 } 15 16 // Fields of the Comment. 17 func (Comment) Fields() []ent.Field { 18 return []ent.Field{ 19 field.Text("content").StructTag(`validate:"required"`), 20 field.Text("content_html").StructTag(`validate:"required"`), 21 field.Int64("votes").Default(0), 22 field.Int("post_id").Optional(), 23 field.Int("user_id").Optional(), 24 field.Int("parent_id").Optional(), 25 } 26 } 27 28 // Edges of the Comment. 29 func (Comment) Edges() []ent.Edge { 30 return []ent.Edge{ 31 edge.From("post", Post.Type).Ref("comments").Field("post_id").Unique(), 32 edge.From("user", User.Type).Ref("comments").Field("user_id").Unique(), 33 edge.To("children", Comment.Type). 34 Annotations(ondeleteSetNull). 35 StorageKey(edge.Column("parent_id"), edge.Symbol("comment_parent")), 36 edge.From("parent", Comment.Type). 37 Field("parent_id"). 38 Ref("children"). 39 Unique(), 40 } 41 } 42 43 func (Comment) Annotations() []schema.Annotation { 44 return []schema.Annotation{ 45 entsql.Annotation{ 46 Charset: "utf8mb4", 47 Collation: "utf8mb4_unicode_ci", 48 }, 49 } 50 } 51 52 func (Comment) Mixin() []ent.Mixin { 53 return []ent.Mixin{ 54 TimeStamp{}, 55 } 56 }