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

     1  // Code generated by entc, DO NOT EDIT.
     2  
     3  package ent
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  	"time"
     9  
    10  	"entgo.io/ent/dialect/sql"
    11  	"github.com/ngocphuongnb/tetua/packages/entrepository/ent/topic"
    12  )
    13  
    14  // Topic is the model entity for the Topic schema.
    15  type Topic struct {
    16  	config `json:"-" validate:"-"`
    17  	// ID of the ent.
    18  	ID int `json:"id,omitempty"`
    19  	// CreatedAt holds the value of the "created_at" field.
    20  	CreatedAt time.Time `json:"omitempty"`
    21  	// UpdatedAt holds the value of the "updated_at" field.
    22  	UpdatedAt time.Time `json:"omitempty"`
    23  	// DeletedAt holds the value of the "deleted_at" field.
    24  	DeletedAt time.Time `json:"omitempty"`
    25  	// Name holds the value of the "name" field.
    26  	Name string `json:"name,omitempty" validate:"max=255"`
    27  	// Slug holds the value of the "slug" field.
    28  	Slug string `json:"slug,omitempty" validate:"max=255"`
    29  	// Description holds the value of the "description" field.
    30  	Description string `json:"description,omitempty" validate:"max=255"`
    31  	// Content holds the value of the "content" field.
    32  	Content string `json:"content,omitempty" validate:"required"`
    33  	// ContentHTML holds the value of the "content_html" field.
    34  	ContentHTML string `json:"content_html,omitempty" validate:"required"`
    35  	// ParentID holds the value of the "parent_id" field.
    36  	ParentID int `json:"parent_id,omitempty"`
    37  	// Edges holds the relations/edges for other nodes in the graph.
    38  	// The values are being populated by the TopicQuery when eager-loading is set.
    39  	Edges TopicEdges `json:"edges"`
    40  }
    41  
    42  // TopicEdges holds the relations/edges for other nodes in the graph.
    43  type TopicEdges struct {
    44  	// Posts holds the value of the posts edge.
    45  	Posts []*Post `json:"posts,omitempty"`
    46  	// Children holds the value of the children edge.
    47  	Children []*Topic `json:"children,omitempty"`
    48  	// Parent holds the value of the parent edge.
    49  	Parent *Topic `json:"parent,omitempty"`
    50  	// loadedTypes holds the information for reporting if a
    51  	// type was loaded (or requested) in eager-loading or not.
    52  	loadedTypes [3]bool
    53  }
    54  
    55  // PostsOrErr returns the Posts value or an error if the edge
    56  // was not loaded in eager-loading.
    57  func (e TopicEdges) PostsOrErr() ([]*Post, error) {
    58  	if e.loadedTypes[0] {
    59  		return e.Posts, nil
    60  	}
    61  	return nil, &NotLoadedError{edge: "posts"}
    62  }
    63  
    64  // ChildrenOrErr returns the Children value or an error if the edge
    65  // was not loaded in eager-loading.
    66  func (e TopicEdges) ChildrenOrErr() ([]*Topic, error) {
    67  	if e.loadedTypes[1] {
    68  		return e.Children, nil
    69  	}
    70  	return nil, &NotLoadedError{edge: "children"}
    71  }
    72  
    73  // ParentOrErr returns the Parent value or an error if the edge
    74  // was not loaded in eager-loading, or loaded but was not found.
    75  func (e TopicEdges) ParentOrErr() (*Topic, error) {
    76  	if e.loadedTypes[2] {
    77  		if e.Parent == nil {
    78  			// The edge parent was loaded in eager-loading,
    79  			// but was not found.
    80  			return nil, &NotFoundError{label: topic.Label}
    81  		}
    82  		return e.Parent, nil
    83  	}
    84  	return nil, &NotLoadedError{edge: "parent"}
    85  }
    86  
    87  // scanValues returns the types for scanning values from sql.Rows.
    88  func (*Topic) scanValues(columns []string) ([]interface{}, error) {
    89  	values := make([]interface{}, len(columns))
    90  	for i := range columns {
    91  		switch columns[i] {
    92  		case topic.FieldID, topic.FieldParentID:
    93  			values[i] = new(sql.NullInt64)
    94  		case topic.FieldName, topic.FieldSlug, topic.FieldDescription, topic.FieldContent, topic.FieldContentHTML:
    95  			values[i] = new(sql.NullString)
    96  		case topic.FieldCreatedAt, topic.FieldUpdatedAt, topic.FieldDeletedAt:
    97  			values[i] = new(sql.NullTime)
    98  		default:
    99  			return nil, fmt.Errorf("unexpected column %q for type Topic", columns[i])
   100  		}
   101  	}
   102  	return values, nil
   103  }
   104  
   105  // assignValues assigns the values that were returned from sql.Rows (after scanning)
   106  // to the Topic fields.
   107  func (t *Topic) assignValues(columns []string, values []interface{}) error {
   108  	if m, n := len(values), len(columns); m < n {
   109  		return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
   110  	}
   111  	for i := range columns {
   112  		switch columns[i] {
   113  		case topic.FieldID:
   114  			value, ok := values[i].(*sql.NullInt64)
   115  			if !ok {
   116  				return fmt.Errorf("unexpected type %T for field id", value)
   117  			}
   118  			t.ID = int(value.Int64)
   119  		case topic.FieldCreatedAt:
   120  			if value, ok := values[i].(*sql.NullTime); !ok {
   121  				return fmt.Errorf("unexpected type %T for field created_at", values[i])
   122  			} else if value.Valid {
   123  				t.CreatedAt = value.Time
   124  			}
   125  		case topic.FieldUpdatedAt:
   126  			if value, ok := values[i].(*sql.NullTime); !ok {
   127  				return fmt.Errorf("unexpected type %T for field updated_at", values[i])
   128  			} else if value.Valid {
   129  				t.UpdatedAt = value.Time
   130  			}
   131  		case topic.FieldDeletedAt:
   132  			if value, ok := values[i].(*sql.NullTime); !ok {
   133  				return fmt.Errorf("unexpected type %T for field deleted_at", values[i])
   134  			} else if value.Valid {
   135  				t.DeletedAt = value.Time
   136  			}
   137  		case topic.FieldName:
   138  			if value, ok := values[i].(*sql.NullString); !ok {
   139  				return fmt.Errorf("unexpected type %T for field name", values[i])
   140  			} else if value.Valid {
   141  				t.Name = value.String
   142  			}
   143  		case topic.FieldSlug:
   144  			if value, ok := values[i].(*sql.NullString); !ok {
   145  				return fmt.Errorf("unexpected type %T for field slug", values[i])
   146  			} else if value.Valid {
   147  				t.Slug = value.String
   148  			}
   149  		case topic.FieldDescription:
   150  			if value, ok := values[i].(*sql.NullString); !ok {
   151  				return fmt.Errorf("unexpected type %T for field description", values[i])
   152  			} else if value.Valid {
   153  				t.Description = value.String
   154  			}
   155  		case topic.FieldContent:
   156  			if value, ok := values[i].(*sql.NullString); !ok {
   157  				return fmt.Errorf("unexpected type %T for field content", values[i])
   158  			} else if value.Valid {
   159  				t.Content = value.String
   160  			}
   161  		case topic.FieldContentHTML:
   162  			if value, ok := values[i].(*sql.NullString); !ok {
   163  				return fmt.Errorf("unexpected type %T for field content_html", values[i])
   164  			} else if value.Valid {
   165  				t.ContentHTML = value.String
   166  			}
   167  		case topic.FieldParentID:
   168  			if value, ok := values[i].(*sql.NullInt64); !ok {
   169  				return fmt.Errorf("unexpected type %T for field parent_id", values[i])
   170  			} else if value.Valid {
   171  				t.ParentID = int(value.Int64)
   172  			}
   173  		}
   174  	}
   175  	return nil
   176  }
   177  
   178  // QueryPosts queries the "posts" edge of the Topic entity.
   179  func (t *Topic) QueryPosts() *PostQuery {
   180  	return (&TopicClient{config: t.config}).QueryPosts(t)
   181  }
   182  
   183  // QueryChildren queries the "children" edge of the Topic entity.
   184  func (t *Topic) QueryChildren() *TopicQuery {
   185  	return (&TopicClient{config: t.config}).QueryChildren(t)
   186  }
   187  
   188  // QueryParent queries the "parent" edge of the Topic entity.
   189  func (t *Topic) QueryParent() *TopicQuery {
   190  	return (&TopicClient{config: t.config}).QueryParent(t)
   191  }
   192  
   193  // Update returns a builder for updating this Topic.
   194  // Note that you need to call Topic.Unwrap() before calling this method if this Topic
   195  // was returned from a transaction, and the transaction was committed or rolled back.
   196  func (t *Topic) Update() *TopicUpdateOne {
   197  	return (&TopicClient{config: t.config}).UpdateOne(t)
   198  }
   199  
   200  // Unwrap unwraps the Topic entity that was returned from a transaction after it was closed,
   201  // so that all future queries will be executed through the driver which created the transaction.
   202  func (t *Topic) Unwrap() *Topic {
   203  	tx, ok := t.config.driver.(*txDriver)
   204  	if !ok {
   205  		panic("ent: Topic is not a transactional entity")
   206  	}
   207  	t.config.driver = tx.drv
   208  	return t
   209  }
   210  
   211  // String implements the fmt.Stringer.
   212  func (t *Topic) String() string {
   213  	var builder strings.Builder
   214  	builder.WriteString("Topic(")
   215  	builder.WriteString(fmt.Sprintf("id=%v", t.ID))
   216  	builder.WriteString(", created_at=")
   217  	builder.WriteString(t.CreatedAt.Format(time.ANSIC))
   218  	builder.WriteString(", updated_at=")
   219  	builder.WriteString(t.UpdatedAt.Format(time.ANSIC))
   220  	builder.WriteString(", deleted_at=")
   221  	builder.WriteString(t.DeletedAt.Format(time.ANSIC))
   222  	builder.WriteString(", name=")
   223  	builder.WriteString(t.Name)
   224  	builder.WriteString(", slug=")
   225  	builder.WriteString(t.Slug)
   226  	builder.WriteString(", description=")
   227  	builder.WriteString(t.Description)
   228  	builder.WriteString(", content=")
   229  	builder.WriteString(t.Content)
   230  	builder.WriteString(", content_html=")
   231  	builder.WriteString(t.ContentHTML)
   232  	builder.WriteString(", parent_id=")
   233  	builder.WriteString(fmt.Sprintf("%v", t.ParentID))
   234  	builder.WriteByte(')')
   235  	return builder.String()
   236  }
   237  
   238  // Topics is a parsable slice of Topic.
   239  type Topics []*Topic
   240  
   241  func (t Topics) config(cfg config) {
   242  	for _i := range t {
   243  		t[_i].config = cfg
   244  	}
   245  }