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