github.com/ars0915/glossika-exercise@v0.0.0-20240904180321-a5617a00d892/entity/user.go (about)

     1  package entity
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"gorm.io/gorm"
     8  )
     9  
    10  type User struct {
    11  	ID               uint           `json:"id" gorm:"primary_key"`
    12  	Email            *string        `json:"email" gorm:"type:varchar(255);not null;unique"`
    13  	Password         *string        `json:"password" gorm:"type:varchar(60);not null"`
    14  	EmailVerified    *bool          `json:"emailVerified" gorm:"type:boolean;default:false"`
    15  	VerificationCode *string        `json:"verificationCode" gorm:"type:varchar(6);not null"`
    16  	CreatedAt        time.Time      `json:"-"`
    17  	UpdatedAt        time.Time      `json:"-"`
    18  	DeletedAt        gorm.DeletedAt `json:"-"`
    19  }
    20  
    21  func (t User) MarshalJSON() ([]byte, error) {
    22  	type Alias User
    23  	s := struct {
    24  		*Alias
    25  		CreatedAt int64  `json:"created_at"`
    26  		UpdatedAt int64  `json:"updated_at"`
    27  		DeletedAt *int64 `json:"deleted_at,omitempty"`
    28  	}{
    29  		Alias:     (*Alias)(&t),
    30  		CreatedAt: t.CreatedAt.Unix(),
    31  		UpdatedAt: t.UpdatedAt.Unix(),
    32  	}
    33  
    34  	if t.DeletedAt.Valid {
    35  		deletedAt := t.DeletedAt.Time.Unix()
    36  		s.DeletedAt = &deletedAt
    37  	}
    38  
    39  	return json.Marshal(&s)
    40  }