feat(user): invitation-code registration (#24)
Adds an OSS invitation-code system gating public signups: - ent InvitationCode entity (code, group override, max uses, expiry, use count) - atomic consume inside the registration transaction; rolls back with user creation - invitation_code_required setting, exposed via public site config - signup form field when enforced; admin CRUD under Users - Invitation codes - codes generated with crypto-random; admin mutations require ScopeAdminWrite Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>pull/3583/head
parent
9023122182
commit
ff211bbe2c
File diff suppressed because one or more lines are too long
@ -0,0 +1,189 @@
|
||||
// Code generated by ent, DO NOT EDIT.
|
||||
|
||||
package ent
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"github.com/cloudreve/Cloudreve/v4/ent/invitationcode"
|
||||
)
|
||||
|
||||
// InvitationCode is the model entity for the InvitationCode schema.
|
||||
type InvitationCode struct {
|
||||
config `json:"-"`
|
||||
// ID of the ent.
|
||||
ID int `json:"id,omitempty"`
|
||||
// CreatedAt holds the value of the "created_at" field.
|
||||
CreatedAt time.Time `json:"created_at,omitempty"`
|
||||
// UpdatedAt holds the value of the "updated_at" field.
|
||||
UpdatedAt time.Time `json:"updated_at,omitempty"`
|
||||
// DeletedAt holds the value of the "deleted_at" field.
|
||||
DeletedAt *time.Time `json:"deleted_at,omitempty"`
|
||||
// Code holds the value of the "code" field.
|
||||
Code string `json:"code,omitempty"`
|
||||
// GroupID holds the value of the "group_id" field.
|
||||
GroupID int `json:"group_id,omitempty"`
|
||||
// MaxUses holds the value of the "max_uses" field.
|
||||
MaxUses int `json:"max_uses,omitempty"`
|
||||
// UsedCount holds the value of the "used_count" field.
|
||||
UsedCount int `json:"used_count,omitempty"`
|
||||
// ExpiresAt holds the value of the "expires_at" field.
|
||||
ExpiresAt *time.Time `json:"expires_at,omitempty"`
|
||||
selectValues sql.SelectValues
|
||||
}
|
||||
|
||||
// scanValues returns the types for scanning values from sql.Rows.
|
||||
func (*InvitationCode) scanValues(columns []string) ([]any, error) {
|
||||
values := make([]any, len(columns))
|
||||
for i := range columns {
|
||||
switch columns[i] {
|
||||
case invitationcode.FieldID, invitationcode.FieldGroupID, invitationcode.FieldMaxUses, invitationcode.FieldUsedCount:
|
||||
values[i] = new(sql.NullInt64)
|
||||
case invitationcode.FieldCode:
|
||||
values[i] = new(sql.NullString)
|
||||
case invitationcode.FieldCreatedAt, invitationcode.FieldUpdatedAt, invitationcode.FieldDeletedAt, invitationcode.FieldExpiresAt:
|
||||
values[i] = new(sql.NullTime)
|
||||
default:
|
||||
values[i] = new(sql.UnknownType)
|
||||
}
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
// assignValues assigns the values that were returned from sql.Rows (after scanning)
|
||||
// to the InvitationCode fields.
|
||||
func (ic *InvitationCode) assignValues(columns []string, values []any) error {
|
||||
if m, n := len(values), len(columns); m < n {
|
||||
return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
|
||||
}
|
||||
for i := range columns {
|
||||
switch columns[i] {
|
||||
case invitationcode.FieldID:
|
||||
value, ok := values[i].(*sql.NullInt64)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T for field id", value)
|
||||
}
|
||||
ic.ID = int(value.Int64)
|
||||
case invitationcode.FieldCreatedAt:
|
||||
if value, ok := values[i].(*sql.NullTime); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field created_at", values[i])
|
||||
} else if value.Valid {
|
||||
ic.CreatedAt = value.Time
|
||||
}
|
||||
case invitationcode.FieldUpdatedAt:
|
||||
if value, ok := values[i].(*sql.NullTime); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field updated_at", values[i])
|
||||
} else if value.Valid {
|
||||
ic.UpdatedAt = value.Time
|
||||
}
|
||||
case invitationcode.FieldDeletedAt:
|
||||
if value, ok := values[i].(*sql.NullTime); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field deleted_at", values[i])
|
||||
} else if value.Valid {
|
||||
ic.DeletedAt = new(time.Time)
|
||||
*ic.DeletedAt = value.Time
|
||||
}
|
||||
case invitationcode.FieldCode:
|
||||
if value, ok := values[i].(*sql.NullString); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field code", values[i])
|
||||
} else if value.Valid {
|
||||
ic.Code = value.String
|
||||
}
|
||||
case invitationcode.FieldGroupID:
|
||||
if value, ok := values[i].(*sql.NullInt64); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field group_id", values[i])
|
||||
} else if value.Valid {
|
||||
ic.GroupID = int(value.Int64)
|
||||
}
|
||||
case invitationcode.FieldMaxUses:
|
||||
if value, ok := values[i].(*sql.NullInt64); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field max_uses", values[i])
|
||||
} else if value.Valid {
|
||||
ic.MaxUses = int(value.Int64)
|
||||
}
|
||||
case invitationcode.FieldUsedCount:
|
||||
if value, ok := values[i].(*sql.NullInt64); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field used_count", values[i])
|
||||
} else if value.Valid {
|
||||
ic.UsedCount = int(value.Int64)
|
||||
}
|
||||
case invitationcode.FieldExpiresAt:
|
||||
if value, ok := values[i].(*sql.NullTime); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field expires_at", values[i])
|
||||
} else if value.Valid {
|
||||
ic.ExpiresAt = new(time.Time)
|
||||
*ic.ExpiresAt = value.Time
|
||||
}
|
||||
default:
|
||||
ic.selectValues.Set(columns[i], values[i])
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value returns the ent.Value that was dynamically selected and assigned to the InvitationCode.
|
||||
// This includes values selected through modifiers, order, etc.
|
||||
func (ic *InvitationCode) Value(name string) (ent.Value, error) {
|
||||
return ic.selectValues.Get(name)
|
||||
}
|
||||
|
||||
// Update returns a builder for updating this InvitationCode.
|
||||
// Note that you need to call InvitationCode.Unwrap() before calling this method if this InvitationCode
|
||||
// was returned from a transaction, and the transaction was committed or rolled back.
|
||||
func (ic *InvitationCode) Update() *InvitationCodeUpdateOne {
|
||||
return NewInvitationCodeClient(ic.config).UpdateOne(ic)
|
||||
}
|
||||
|
||||
// Unwrap unwraps the InvitationCode entity that was returned from a transaction after it was closed,
|
||||
// so that all future queries will be executed through the driver which created the transaction.
|
||||
func (ic *InvitationCode) Unwrap() *InvitationCode {
|
||||
_tx, ok := ic.config.driver.(*txDriver)
|
||||
if !ok {
|
||||
panic("ent: InvitationCode is not a transactional entity")
|
||||
}
|
||||
ic.config.driver = _tx.drv
|
||||
return ic
|
||||
}
|
||||
|
||||
// String implements the fmt.Stringer.
|
||||
func (ic *InvitationCode) String() string {
|
||||
var builder strings.Builder
|
||||
builder.WriteString("InvitationCode(")
|
||||
builder.WriteString(fmt.Sprintf("id=%v, ", ic.ID))
|
||||
builder.WriteString("created_at=")
|
||||
builder.WriteString(ic.CreatedAt.Format(time.ANSIC))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("updated_at=")
|
||||
builder.WriteString(ic.UpdatedAt.Format(time.ANSIC))
|
||||
builder.WriteString(", ")
|
||||
if v := ic.DeletedAt; v != nil {
|
||||
builder.WriteString("deleted_at=")
|
||||
builder.WriteString(v.Format(time.ANSIC))
|
||||
}
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("code=")
|
||||
builder.WriteString(ic.Code)
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("group_id=")
|
||||
builder.WriteString(fmt.Sprintf("%v", ic.GroupID))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("max_uses=")
|
||||
builder.WriteString(fmt.Sprintf("%v", ic.MaxUses))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("used_count=")
|
||||
builder.WriteString(fmt.Sprintf("%v", ic.UsedCount))
|
||||
builder.WriteString(", ")
|
||||
if v := ic.ExpiresAt; v != nil {
|
||||
builder.WriteString("expires_at=")
|
||||
builder.WriteString(v.Format(time.ANSIC))
|
||||
}
|
||||
builder.WriteByte(')')
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// InvitationCodes is a parsable slice of InvitationCode.
|
||||
type InvitationCodes []*InvitationCode
|
||||
@ -0,0 +1,130 @@
|
||||
// Code generated by ent, DO NOT EDIT.
|
||||
|
||||
package invitationcode
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
)
|
||||
|
||||
const (
|
||||
// Label holds the string label denoting the invitationcode type in the database.
|
||||
Label = "invitation_code"
|
||||
// FieldID holds the string denoting the id field in the database.
|
||||
FieldID = "id"
|
||||
// FieldCreatedAt holds the string denoting the created_at field in the database.
|
||||
FieldCreatedAt = "created_at"
|
||||
// FieldUpdatedAt holds the string denoting the updated_at field in the database.
|
||||
FieldUpdatedAt = "updated_at"
|
||||
// FieldDeletedAt holds the string denoting the deleted_at field in the database.
|
||||
FieldDeletedAt = "deleted_at"
|
||||
// FieldCode holds the string denoting the code field in the database.
|
||||
FieldCode = "code"
|
||||
// FieldGroupID holds the string denoting the group_id field in the database.
|
||||
FieldGroupID = "group_id"
|
||||
// FieldMaxUses holds the string denoting the max_uses field in the database.
|
||||
FieldMaxUses = "max_uses"
|
||||
// FieldUsedCount holds the string denoting the used_count field in the database.
|
||||
FieldUsedCount = "used_count"
|
||||
// FieldExpiresAt holds the string denoting the expires_at field in the database.
|
||||
FieldExpiresAt = "expires_at"
|
||||
// Table holds the table name of the invitationcode in the database.
|
||||
Table = "invitation_codes"
|
||||
)
|
||||
|
||||
// Columns holds all SQL columns for invitationcode fields.
|
||||
var Columns = []string{
|
||||
FieldID,
|
||||
FieldCreatedAt,
|
||||
FieldUpdatedAt,
|
||||
FieldDeletedAt,
|
||||
FieldCode,
|
||||
FieldGroupID,
|
||||
FieldMaxUses,
|
||||
FieldUsedCount,
|
||||
FieldExpiresAt,
|
||||
}
|
||||
|
||||
// ValidColumn reports if the column name is valid (part of the table columns).
|
||||
func ValidColumn(column string) bool {
|
||||
for i := range Columns {
|
||||
if column == Columns[i] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Note that the variables below are initialized by the runtime
|
||||
// package on the initialization of the application. Therefore,
|
||||
// it should be imported in the main as follows:
|
||||
//
|
||||
// import _ "github.com/cloudreve/Cloudreve/v4/ent/runtime"
|
||||
var (
|
||||
Hooks [1]ent.Hook
|
||||
Interceptors [1]ent.Interceptor
|
||||
// DefaultCreatedAt holds the default value on creation for the "created_at" field.
|
||||
DefaultCreatedAt func() time.Time
|
||||
// DefaultUpdatedAt holds the default value on creation for the "updated_at" field.
|
||||
DefaultUpdatedAt func() time.Time
|
||||
// UpdateDefaultUpdatedAt holds the default value on update for the "updated_at" field.
|
||||
UpdateDefaultUpdatedAt func() time.Time
|
||||
// CodeValidator is a validator for the "code" field. It is called by the builders before save.
|
||||
CodeValidator func(string) error
|
||||
// DefaultGroupID holds the default value on creation for the "group_id" field.
|
||||
DefaultGroupID int
|
||||
// DefaultMaxUses holds the default value on creation for the "max_uses" field.
|
||||
DefaultMaxUses int
|
||||
// DefaultUsedCount holds the default value on creation for the "used_count" field.
|
||||
DefaultUsedCount int
|
||||
)
|
||||
|
||||
// OrderOption defines the ordering options for the InvitationCode queries.
|
||||
type OrderOption func(*sql.Selector)
|
||||
|
||||
// ByID orders the results by the id field.
|
||||
func ByID(opts ...sql.OrderTermOption) OrderOption {
|
||||
return sql.OrderByField(FieldID, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByCreatedAt orders the results by the created_at field.
|
||||
func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption {
|
||||
return sql.OrderByField(FieldCreatedAt, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByUpdatedAt orders the results by the updated_at field.
|
||||
func ByUpdatedAt(opts ...sql.OrderTermOption) OrderOption {
|
||||
return sql.OrderByField(FieldUpdatedAt, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByDeletedAt orders the results by the deleted_at field.
|
||||
func ByDeletedAt(opts ...sql.OrderTermOption) OrderOption {
|
||||
return sql.OrderByField(FieldDeletedAt, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByCode orders the results by the code field.
|
||||
func ByCode(opts ...sql.OrderTermOption) OrderOption {
|
||||
return sql.OrderByField(FieldCode, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByGroupID orders the results by the group_id field.
|
||||
func ByGroupID(opts ...sql.OrderTermOption) OrderOption {
|
||||
return sql.OrderByField(FieldGroupID, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByMaxUses orders the results by the max_uses field.
|
||||
func ByMaxUses(opts ...sql.OrderTermOption) OrderOption {
|
||||
return sql.OrderByField(FieldMaxUses, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByUsedCount orders the results by the used_count field.
|
||||
func ByUsedCount(opts ...sql.OrderTermOption) OrderOption {
|
||||
return sql.OrderByField(FieldUsedCount, opts...).ToFunc()
|
||||
}
|
||||
|
||||
// ByExpiresAt orders the results by the expires_at field.
|
||||
func ByExpiresAt(opts ...sql.OrderTermOption) OrderOption {
|
||||
return sql.OrderByField(FieldExpiresAt, opts...).ToFunc()
|
||||
}
|
||||
@ -0,0 +1,475 @@
|
||||
// Code generated by ent, DO NOT EDIT.
|
||||
|
||||
package invitationcode
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"github.com/cloudreve/Cloudreve/v4/ent/predicate"
|
||||
)
|
||||
|
||||
// ID filters vertices based on their ID field.
|
||||
func ID(id int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldID, id))
|
||||
}
|
||||
|
||||
// IDEQ applies the EQ predicate on the ID field.
|
||||
func IDEQ(id int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldID, id))
|
||||
}
|
||||
|
||||
// IDNEQ applies the NEQ predicate on the ID field.
|
||||
func IDNEQ(id int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNEQ(FieldID, id))
|
||||
}
|
||||
|
||||
// IDIn applies the In predicate on the ID field.
|
||||
func IDIn(ids ...int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldIn(FieldID, ids...))
|
||||
}
|
||||
|
||||
// IDNotIn applies the NotIn predicate on the ID field.
|
||||
func IDNotIn(ids ...int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNotIn(FieldID, ids...))
|
||||
}
|
||||
|
||||
// IDGT applies the GT predicate on the ID field.
|
||||
func IDGT(id int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGT(FieldID, id))
|
||||
}
|
||||
|
||||
// IDGTE applies the GTE predicate on the ID field.
|
||||
func IDGTE(id int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGTE(FieldID, id))
|
||||
}
|
||||
|
||||
// IDLT applies the LT predicate on the ID field.
|
||||
func IDLT(id int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLT(FieldID, id))
|
||||
}
|
||||
|
||||
// IDLTE applies the LTE predicate on the ID field.
|
||||
func IDLTE(id int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLTE(FieldID, id))
|
||||
}
|
||||
|
||||
// CreatedAt applies equality check predicate on the "created_at" field. It's identical to CreatedAtEQ.
|
||||
func CreatedAt(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldCreatedAt, v))
|
||||
}
|
||||
|
||||
// UpdatedAt applies equality check predicate on the "updated_at" field. It's identical to UpdatedAtEQ.
|
||||
func UpdatedAt(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldUpdatedAt, v))
|
||||
}
|
||||
|
||||
// DeletedAt applies equality check predicate on the "deleted_at" field. It's identical to DeletedAtEQ.
|
||||
func DeletedAt(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldDeletedAt, v))
|
||||
}
|
||||
|
||||
// Code applies equality check predicate on the "code" field. It's identical to CodeEQ.
|
||||
func Code(v string) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldCode, v))
|
||||
}
|
||||
|
||||
// GroupID applies equality check predicate on the "group_id" field. It's identical to GroupIDEQ.
|
||||
func GroupID(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldGroupID, v))
|
||||
}
|
||||
|
||||
// MaxUses applies equality check predicate on the "max_uses" field. It's identical to MaxUsesEQ.
|
||||
func MaxUses(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldMaxUses, v))
|
||||
}
|
||||
|
||||
// UsedCount applies equality check predicate on the "used_count" field. It's identical to UsedCountEQ.
|
||||
func UsedCount(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldUsedCount, v))
|
||||
}
|
||||
|
||||
// ExpiresAt applies equality check predicate on the "expires_at" field. It's identical to ExpiresAtEQ.
|
||||
func ExpiresAt(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldExpiresAt, v))
|
||||
}
|
||||
|
||||
// CreatedAtEQ applies the EQ predicate on the "created_at" field.
|
||||
func CreatedAtEQ(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldCreatedAt, v))
|
||||
}
|
||||
|
||||
// CreatedAtNEQ applies the NEQ predicate on the "created_at" field.
|
||||
func CreatedAtNEQ(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNEQ(FieldCreatedAt, v))
|
||||
}
|
||||
|
||||
// CreatedAtIn applies the In predicate on the "created_at" field.
|
||||
func CreatedAtIn(vs ...time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldIn(FieldCreatedAt, vs...))
|
||||
}
|
||||
|
||||
// CreatedAtNotIn applies the NotIn predicate on the "created_at" field.
|
||||
func CreatedAtNotIn(vs ...time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNotIn(FieldCreatedAt, vs...))
|
||||
}
|
||||
|
||||
// CreatedAtGT applies the GT predicate on the "created_at" field.
|
||||
func CreatedAtGT(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGT(FieldCreatedAt, v))
|
||||
}
|
||||
|
||||
// CreatedAtGTE applies the GTE predicate on the "created_at" field.
|
||||
func CreatedAtGTE(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGTE(FieldCreatedAt, v))
|
||||
}
|
||||
|
||||
// CreatedAtLT applies the LT predicate on the "created_at" field.
|
||||
func CreatedAtLT(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLT(FieldCreatedAt, v))
|
||||
}
|
||||
|
||||
// CreatedAtLTE applies the LTE predicate on the "created_at" field.
|
||||
func CreatedAtLTE(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLTE(FieldCreatedAt, v))
|
||||
}
|
||||
|
||||
// UpdatedAtEQ applies the EQ predicate on the "updated_at" field.
|
||||
func UpdatedAtEQ(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldUpdatedAt, v))
|
||||
}
|
||||
|
||||
// UpdatedAtNEQ applies the NEQ predicate on the "updated_at" field.
|
||||
func UpdatedAtNEQ(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNEQ(FieldUpdatedAt, v))
|
||||
}
|
||||
|
||||
// UpdatedAtIn applies the In predicate on the "updated_at" field.
|
||||
func UpdatedAtIn(vs ...time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldIn(FieldUpdatedAt, vs...))
|
||||
}
|
||||
|
||||
// UpdatedAtNotIn applies the NotIn predicate on the "updated_at" field.
|
||||
func UpdatedAtNotIn(vs ...time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNotIn(FieldUpdatedAt, vs...))
|
||||
}
|
||||
|
||||
// UpdatedAtGT applies the GT predicate on the "updated_at" field.
|
||||
func UpdatedAtGT(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGT(FieldUpdatedAt, v))
|
||||
}
|
||||
|
||||
// UpdatedAtGTE applies the GTE predicate on the "updated_at" field.
|
||||
func UpdatedAtGTE(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGTE(FieldUpdatedAt, v))
|
||||
}
|
||||
|
||||
// UpdatedAtLT applies the LT predicate on the "updated_at" field.
|
||||
func UpdatedAtLT(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLT(FieldUpdatedAt, v))
|
||||
}
|
||||
|
||||
// UpdatedAtLTE applies the LTE predicate on the "updated_at" field.
|
||||
func UpdatedAtLTE(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLTE(FieldUpdatedAt, v))
|
||||
}
|
||||
|
||||
// DeletedAtEQ applies the EQ predicate on the "deleted_at" field.
|
||||
func DeletedAtEQ(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldDeletedAt, v))
|
||||
}
|
||||
|
||||
// DeletedAtNEQ applies the NEQ predicate on the "deleted_at" field.
|
||||
func DeletedAtNEQ(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNEQ(FieldDeletedAt, v))
|
||||
}
|
||||
|
||||
// DeletedAtIn applies the In predicate on the "deleted_at" field.
|
||||
func DeletedAtIn(vs ...time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldIn(FieldDeletedAt, vs...))
|
||||
}
|
||||
|
||||
// DeletedAtNotIn applies the NotIn predicate on the "deleted_at" field.
|
||||
func DeletedAtNotIn(vs ...time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNotIn(FieldDeletedAt, vs...))
|
||||
}
|
||||
|
||||
// DeletedAtGT applies the GT predicate on the "deleted_at" field.
|
||||
func DeletedAtGT(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGT(FieldDeletedAt, v))
|
||||
}
|
||||
|
||||
// DeletedAtGTE applies the GTE predicate on the "deleted_at" field.
|
||||
func DeletedAtGTE(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGTE(FieldDeletedAt, v))
|
||||
}
|
||||
|
||||
// DeletedAtLT applies the LT predicate on the "deleted_at" field.
|
||||
func DeletedAtLT(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLT(FieldDeletedAt, v))
|
||||
}
|
||||
|
||||
// DeletedAtLTE applies the LTE predicate on the "deleted_at" field.
|
||||
func DeletedAtLTE(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLTE(FieldDeletedAt, v))
|
||||
}
|
||||
|
||||
// DeletedAtIsNil applies the IsNil predicate on the "deleted_at" field.
|
||||
func DeletedAtIsNil() predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldIsNull(FieldDeletedAt))
|
||||
}
|
||||
|
||||
// DeletedAtNotNil applies the NotNil predicate on the "deleted_at" field.
|
||||
func DeletedAtNotNil() predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNotNull(FieldDeletedAt))
|
||||
}
|
||||
|
||||
// CodeEQ applies the EQ predicate on the "code" field.
|
||||
func CodeEQ(v string) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldCode, v))
|
||||
}
|
||||
|
||||
// CodeNEQ applies the NEQ predicate on the "code" field.
|
||||
func CodeNEQ(v string) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNEQ(FieldCode, v))
|
||||
}
|
||||
|
||||
// CodeIn applies the In predicate on the "code" field.
|
||||
func CodeIn(vs ...string) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldIn(FieldCode, vs...))
|
||||
}
|
||||
|
||||
// CodeNotIn applies the NotIn predicate on the "code" field.
|
||||
func CodeNotIn(vs ...string) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNotIn(FieldCode, vs...))
|
||||
}
|
||||
|
||||
// CodeGT applies the GT predicate on the "code" field.
|
||||
func CodeGT(v string) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGT(FieldCode, v))
|
||||
}
|
||||
|
||||
// CodeGTE applies the GTE predicate on the "code" field.
|
||||
func CodeGTE(v string) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGTE(FieldCode, v))
|
||||
}
|
||||
|
||||
// CodeLT applies the LT predicate on the "code" field.
|
||||
func CodeLT(v string) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLT(FieldCode, v))
|
||||
}
|
||||
|
||||
// CodeLTE applies the LTE predicate on the "code" field.
|
||||
func CodeLTE(v string) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLTE(FieldCode, v))
|
||||
}
|
||||
|
||||
// CodeContains applies the Contains predicate on the "code" field.
|
||||
func CodeContains(v string) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldContains(FieldCode, v))
|
||||
}
|
||||
|
||||
// CodeHasPrefix applies the HasPrefix predicate on the "code" field.
|
||||
func CodeHasPrefix(v string) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldHasPrefix(FieldCode, v))
|
||||
}
|
||||
|
||||
// CodeHasSuffix applies the HasSuffix predicate on the "code" field.
|
||||
func CodeHasSuffix(v string) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldHasSuffix(FieldCode, v))
|
||||
}
|
||||
|
||||
// CodeEqualFold applies the EqualFold predicate on the "code" field.
|
||||
func CodeEqualFold(v string) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEqualFold(FieldCode, v))
|
||||
}
|
||||
|
||||
// CodeContainsFold applies the ContainsFold predicate on the "code" field.
|
||||
func CodeContainsFold(v string) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldContainsFold(FieldCode, v))
|
||||
}
|
||||
|
||||
// GroupIDEQ applies the EQ predicate on the "group_id" field.
|
||||
func GroupIDEQ(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldGroupID, v))
|
||||
}
|
||||
|
||||
// GroupIDNEQ applies the NEQ predicate on the "group_id" field.
|
||||
func GroupIDNEQ(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNEQ(FieldGroupID, v))
|
||||
}
|
||||
|
||||
// GroupIDIn applies the In predicate on the "group_id" field.
|
||||
func GroupIDIn(vs ...int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldIn(FieldGroupID, vs...))
|
||||
}
|
||||
|
||||
// GroupIDNotIn applies the NotIn predicate on the "group_id" field.
|
||||
func GroupIDNotIn(vs ...int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNotIn(FieldGroupID, vs...))
|
||||
}
|
||||
|
||||
// GroupIDGT applies the GT predicate on the "group_id" field.
|
||||
func GroupIDGT(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGT(FieldGroupID, v))
|
||||
}
|
||||
|
||||
// GroupIDGTE applies the GTE predicate on the "group_id" field.
|
||||
func GroupIDGTE(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGTE(FieldGroupID, v))
|
||||
}
|
||||
|
||||
// GroupIDLT applies the LT predicate on the "group_id" field.
|
||||
func GroupIDLT(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLT(FieldGroupID, v))
|
||||
}
|
||||
|
||||
// GroupIDLTE applies the LTE predicate on the "group_id" field.
|
||||
func GroupIDLTE(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLTE(FieldGroupID, v))
|
||||
}
|
||||
|
||||
// MaxUsesEQ applies the EQ predicate on the "max_uses" field.
|
||||
func MaxUsesEQ(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldMaxUses, v))
|
||||
}
|
||||
|
||||
// MaxUsesNEQ applies the NEQ predicate on the "max_uses" field.
|
||||
func MaxUsesNEQ(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNEQ(FieldMaxUses, v))
|
||||
}
|
||||
|
||||
// MaxUsesIn applies the In predicate on the "max_uses" field.
|
||||
func MaxUsesIn(vs ...int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldIn(FieldMaxUses, vs...))
|
||||
}
|
||||
|
||||
// MaxUsesNotIn applies the NotIn predicate on the "max_uses" field.
|
||||
func MaxUsesNotIn(vs ...int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNotIn(FieldMaxUses, vs...))
|
||||
}
|
||||
|
||||
// MaxUsesGT applies the GT predicate on the "max_uses" field.
|
||||
func MaxUsesGT(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGT(FieldMaxUses, v))
|
||||
}
|
||||
|
||||
// MaxUsesGTE applies the GTE predicate on the "max_uses" field.
|
||||
func MaxUsesGTE(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGTE(FieldMaxUses, v))
|
||||
}
|
||||
|
||||
// MaxUsesLT applies the LT predicate on the "max_uses" field.
|
||||
func MaxUsesLT(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLT(FieldMaxUses, v))
|
||||
}
|
||||
|
||||
// MaxUsesLTE applies the LTE predicate on the "max_uses" field.
|
||||
func MaxUsesLTE(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLTE(FieldMaxUses, v))
|
||||
}
|
||||
|
||||
// UsedCountEQ applies the EQ predicate on the "used_count" field.
|
||||
func UsedCountEQ(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldUsedCount, v))
|
||||
}
|
||||
|
||||
// UsedCountNEQ applies the NEQ predicate on the "used_count" field.
|
||||
func UsedCountNEQ(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNEQ(FieldUsedCount, v))
|
||||
}
|
||||
|
||||
// UsedCountIn applies the In predicate on the "used_count" field.
|
||||
func UsedCountIn(vs ...int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldIn(FieldUsedCount, vs...))
|
||||
}
|
||||
|
||||
// UsedCountNotIn applies the NotIn predicate on the "used_count" field.
|
||||
func UsedCountNotIn(vs ...int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNotIn(FieldUsedCount, vs...))
|
||||
}
|
||||
|
||||
// UsedCountGT applies the GT predicate on the "used_count" field.
|
||||
func UsedCountGT(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGT(FieldUsedCount, v))
|
||||
}
|
||||
|
||||
// UsedCountGTE applies the GTE predicate on the "used_count" field.
|
||||
func UsedCountGTE(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGTE(FieldUsedCount, v))
|
||||
}
|
||||
|
||||
// UsedCountLT applies the LT predicate on the "used_count" field.
|
||||
func UsedCountLT(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLT(FieldUsedCount, v))
|
||||
}
|
||||
|
||||
// UsedCountLTE applies the LTE predicate on the "used_count" field.
|
||||
func UsedCountLTE(v int) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLTE(FieldUsedCount, v))
|
||||
}
|
||||
|
||||
// ExpiresAtEQ applies the EQ predicate on the "expires_at" field.
|
||||
func ExpiresAtEQ(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldEQ(FieldExpiresAt, v))
|
||||
}
|
||||
|
||||
// ExpiresAtNEQ applies the NEQ predicate on the "expires_at" field.
|
||||
func ExpiresAtNEQ(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNEQ(FieldExpiresAt, v))
|
||||
}
|
||||
|
||||
// ExpiresAtIn applies the In predicate on the "expires_at" field.
|
||||
func ExpiresAtIn(vs ...time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldIn(FieldExpiresAt, vs...))
|
||||
}
|
||||
|
||||
// ExpiresAtNotIn applies the NotIn predicate on the "expires_at" field.
|
||||
func ExpiresAtNotIn(vs ...time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNotIn(FieldExpiresAt, vs...))
|
||||
}
|
||||
|
||||
// ExpiresAtGT applies the GT predicate on the "expires_at" field.
|
||||
func ExpiresAtGT(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGT(FieldExpiresAt, v))
|
||||
}
|
||||
|
||||
// ExpiresAtGTE applies the GTE predicate on the "expires_at" field.
|
||||
func ExpiresAtGTE(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldGTE(FieldExpiresAt, v))
|
||||
}
|
||||
|
||||
// ExpiresAtLT applies the LT predicate on the "expires_at" field.
|
||||
func ExpiresAtLT(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLT(FieldExpiresAt, v))
|
||||
}
|
||||
|
||||
// ExpiresAtLTE applies the LTE predicate on the "expires_at" field.
|
||||
func ExpiresAtLTE(v time.Time) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldLTE(FieldExpiresAt, v))
|
||||
}
|
||||
|
||||
// ExpiresAtIsNil applies the IsNil predicate on the "expires_at" field.
|
||||
func ExpiresAtIsNil() predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldIsNull(FieldExpiresAt))
|
||||
}
|
||||
|
||||
// ExpiresAtNotNil applies the NotNil predicate on the "expires_at" field.
|
||||
func ExpiresAtNotNil() predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.FieldNotNull(FieldExpiresAt))
|
||||
}
|
||||
|
||||
// And groups predicates with the AND operator between them.
|
||||
func And(predicates ...predicate.InvitationCode) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.AndPredicates(predicates...))
|
||||
}
|
||||
|
||||
// Or groups predicates with the OR operator between them.
|
||||
func Or(predicates ...predicate.InvitationCode) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.OrPredicates(predicates...))
|
||||
}
|
||||
|
||||
// Not applies the not operator on the given predicate.
|
||||
func Not(p predicate.InvitationCode) predicate.InvitationCode {
|
||||
return predicate.InvitationCode(sql.NotPredicates(p))
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,88 @@
|
||||
// Code generated by ent, DO NOT EDIT.
|
||||
|
||||
package ent
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/schema/field"
|
||||
"github.com/cloudreve/Cloudreve/v4/ent/invitationcode"
|
||||
"github.com/cloudreve/Cloudreve/v4/ent/predicate"
|
||||
)
|
||||
|
||||
// InvitationCodeDelete is the builder for deleting a InvitationCode entity.
|
||||
type InvitationCodeDelete struct {
|
||||
config
|
||||
hooks []Hook
|
||||
mutation *InvitationCodeMutation
|
||||
}
|
||||
|
||||
// Where appends a list predicates to the InvitationCodeDelete builder.
|
||||
func (icd *InvitationCodeDelete) Where(ps ...predicate.InvitationCode) *InvitationCodeDelete {
|
||||
icd.mutation.Where(ps...)
|
||||
return icd
|
||||
}
|
||||
|
||||
// Exec executes the deletion query and returns how many vertices were deleted.
|
||||
func (icd *InvitationCodeDelete) Exec(ctx context.Context) (int, error) {
|
||||
return withHooks(ctx, icd.sqlExec, icd.mutation, icd.hooks)
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
func (icd *InvitationCodeDelete) ExecX(ctx context.Context) int {
|
||||
n, err := icd.Exec(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (icd *InvitationCodeDelete) sqlExec(ctx context.Context) (int, error) {
|
||||
_spec := sqlgraph.NewDeleteSpec(invitationcode.Table, sqlgraph.NewFieldSpec(invitationcode.FieldID, field.TypeInt))
|
||||
if ps := icd.mutation.predicates; len(ps) > 0 {
|
||||
_spec.Predicate = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
affected, err := sqlgraph.DeleteNodes(ctx, icd.driver, _spec)
|
||||
if err != nil && sqlgraph.IsConstraintError(err) {
|
||||
err = &ConstraintError{msg: err.Error(), wrap: err}
|
||||
}
|
||||
icd.mutation.done = true
|
||||
return affected, err
|
||||
}
|
||||
|
||||
// InvitationCodeDeleteOne is the builder for deleting a single InvitationCode entity.
|
||||
type InvitationCodeDeleteOne struct {
|
||||
icd *InvitationCodeDelete
|
||||
}
|
||||
|
||||
// Where appends a list predicates to the InvitationCodeDelete builder.
|
||||
func (icdo *InvitationCodeDeleteOne) Where(ps ...predicate.InvitationCode) *InvitationCodeDeleteOne {
|
||||
icdo.icd.mutation.Where(ps...)
|
||||
return icdo
|
||||
}
|
||||
|
||||
// Exec executes the deletion query.
|
||||
func (icdo *InvitationCodeDeleteOne) Exec(ctx context.Context) error {
|
||||
n, err := icdo.icd.Exec(ctx)
|
||||
switch {
|
||||
case err != nil:
|
||||
return err
|
||||
case n == 0:
|
||||
return &NotFoundError{invitationcode.Label}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
func (icdo *InvitationCodeDeleteOne) ExecX(ctx context.Context) {
|
||||
if err := icdo.Exec(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,526 @@
|
||||
// Code generated by ent, DO NOT EDIT.
|
||||
|
||||
package ent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/schema/field"
|
||||
"github.com/cloudreve/Cloudreve/v4/ent/invitationcode"
|
||||
"github.com/cloudreve/Cloudreve/v4/ent/predicate"
|
||||
)
|
||||
|
||||
// InvitationCodeQuery is the builder for querying InvitationCode entities.
|
||||
type InvitationCodeQuery struct {
|
||||
config
|
||||
ctx *QueryContext
|
||||
order []invitationcode.OrderOption
|
||||
inters []Interceptor
|
||||
predicates []predicate.InvitationCode
|
||||
// intermediate query (i.e. traversal path).
|
||||
sql *sql.Selector
|
||||
path func(context.Context) (*sql.Selector, error)
|
||||
}
|
||||
|
||||
// Where adds a new predicate for the InvitationCodeQuery builder.
|
||||
func (icq *InvitationCodeQuery) Where(ps ...predicate.InvitationCode) *InvitationCodeQuery {
|
||||
icq.predicates = append(icq.predicates, ps...)
|
||||
return icq
|
||||
}
|
||||
|
||||
// Limit the number of records to be returned by this query.
|
||||
func (icq *InvitationCodeQuery) Limit(limit int) *InvitationCodeQuery {
|
||||
icq.ctx.Limit = &limit
|
||||
return icq
|
||||
}
|
||||
|
||||
// Offset to start from.
|
||||
func (icq *InvitationCodeQuery) Offset(offset int) *InvitationCodeQuery {
|
||||
icq.ctx.Offset = &offset
|
||||
return icq
|
||||
}
|
||||
|
||||
// Unique configures the query builder to filter duplicate records on query.
|
||||
// By default, unique is set to true, and can be disabled using this method.
|
||||
func (icq *InvitationCodeQuery) Unique(unique bool) *InvitationCodeQuery {
|
||||
icq.ctx.Unique = &unique
|
||||
return icq
|
||||
}
|
||||
|
||||
// Order specifies how the records should be ordered.
|
||||
func (icq *InvitationCodeQuery) Order(o ...invitationcode.OrderOption) *InvitationCodeQuery {
|
||||
icq.order = append(icq.order, o...)
|
||||
return icq
|
||||
}
|
||||
|
||||
// First returns the first InvitationCode entity from the query.
|
||||
// Returns a *NotFoundError when no InvitationCode was found.
|
||||
func (icq *InvitationCodeQuery) First(ctx context.Context) (*InvitationCode, error) {
|
||||
nodes, err := icq.Limit(1).All(setContextOp(ctx, icq.ctx, "First"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(nodes) == 0 {
|
||||
return nil, &NotFoundError{invitationcode.Label}
|
||||
}
|
||||
return nodes[0], nil
|
||||
}
|
||||
|
||||
// FirstX is like First, but panics if an error occurs.
|
||||
func (icq *InvitationCodeQuery) FirstX(ctx context.Context) *InvitationCode {
|
||||
node, err := icq.First(ctx)
|
||||
if err != nil && !IsNotFound(err) {
|
||||
panic(err)
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
// FirstID returns the first InvitationCode ID from the query.
|
||||
// Returns a *NotFoundError when no InvitationCode ID was found.
|
||||
func (icq *InvitationCodeQuery) FirstID(ctx context.Context) (id int, err error) {
|
||||
var ids []int
|
||||
if ids, err = icq.Limit(1).IDs(setContextOp(ctx, icq.ctx, "FirstID")); err != nil {
|
||||
return
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
err = &NotFoundError{invitationcode.Label}
|
||||
return
|
||||
}
|
||||
return ids[0], nil
|
||||
}
|
||||
|
||||
// FirstIDX is like FirstID, but panics if an error occurs.
|
||||
func (icq *InvitationCodeQuery) FirstIDX(ctx context.Context) int {
|
||||
id, err := icq.FirstID(ctx)
|
||||
if err != nil && !IsNotFound(err) {
|
||||
panic(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// Only returns a single InvitationCode entity found by the query, ensuring it only returns one.
|
||||
// Returns a *NotSingularError when more than one InvitationCode entity is found.
|
||||
// Returns a *NotFoundError when no InvitationCode entities are found.
|
||||
func (icq *InvitationCodeQuery) Only(ctx context.Context) (*InvitationCode, error) {
|
||||
nodes, err := icq.Limit(2).All(setContextOp(ctx, icq.ctx, "Only"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch len(nodes) {
|
||||
case 1:
|
||||
return nodes[0], nil
|
||||
case 0:
|
||||
return nil, &NotFoundError{invitationcode.Label}
|
||||
default:
|
||||
return nil, &NotSingularError{invitationcode.Label}
|
||||
}
|
||||
}
|
||||
|
||||
// OnlyX is like Only, but panics if an error occurs.
|
||||
func (icq *InvitationCodeQuery) OnlyX(ctx context.Context) *InvitationCode {
|
||||
node, err := icq.Only(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
// OnlyID is like Only, but returns the only InvitationCode ID in the query.
|
||||
// Returns a *NotSingularError when more than one InvitationCode ID is found.
|
||||
// Returns a *NotFoundError when no entities are found.
|
||||
func (icq *InvitationCodeQuery) OnlyID(ctx context.Context) (id int, err error) {
|
||||
var ids []int
|
||||
if ids, err = icq.Limit(2).IDs(setContextOp(ctx, icq.ctx, "OnlyID")); err != nil {
|
||||
return
|
||||
}
|
||||
switch len(ids) {
|
||||
case 1:
|
||||
id = ids[0]
|
||||
case 0:
|
||||
err = &NotFoundError{invitationcode.Label}
|
||||
default:
|
||||
err = &NotSingularError{invitationcode.Label}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// OnlyIDX is like OnlyID, but panics if an error occurs.
|
||||
func (icq *InvitationCodeQuery) OnlyIDX(ctx context.Context) int {
|
||||
id, err := icq.OnlyID(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// All executes the query and returns a list of InvitationCodes.
|
||||
func (icq *InvitationCodeQuery) All(ctx context.Context) ([]*InvitationCode, error) {
|
||||
ctx = setContextOp(ctx, icq.ctx, "All")
|
||||
if err := icq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
qr := querierAll[[]*InvitationCode, *InvitationCodeQuery]()
|
||||
return withInterceptors[[]*InvitationCode](ctx, icq, qr, icq.inters)
|
||||
}
|
||||
|
||||
// AllX is like All, but panics if an error occurs.
|
||||
func (icq *InvitationCodeQuery) AllX(ctx context.Context) []*InvitationCode {
|
||||
nodes, err := icq.All(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return nodes
|
||||
}
|
||||
|
||||
// IDs executes the query and returns a list of InvitationCode IDs.
|
||||
func (icq *InvitationCodeQuery) IDs(ctx context.Context) (ids []int, err error) {
|
||||
if icq.ctx.Unique == nil && icq.path != nil {
|
||||
icq.Unique(true)
|
||||
}
|
||||
ctx = setContextOp(ctx, icq.ctx, "IDs")
|
||||
if err = icq.Select(invitationcode.FieldID).Scan(ctx, &ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// IDsX is like IDs, but panics if an error occurs.
|
||||
func (icq *InvitationCodeQuery) IDsX(ctx context.Context) []int {
|
||||
ids, err := icq.IDs(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
// Count returns the count of the given query.
|
||||
func (icq *InvitationCodeQuery) Count(ctx context.Context) (int, error) {
|
||||
ctx = setContextOp(ctx, icq.ctx, "Count")
|
||||
if err := icq.prepareQuery(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return withInterceptors[int](ctx, icq, querierCount[*InvitationCodeQuery](), icq.inters)
|
||||
}
|
||||
|
||||
// CountX is like Count, but panics if an error occurs.
|
||||
func (icq *InvitationCodeQuery) CountX(ctx context.Context) int {
|
||||
count, err := icq.Count(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// Exist returns true if the query has elements in the graph.
|
||||
func (icq *InvitationCodeQuery) Exist(ctx context.Context) (bool, error) {
|
||||
ctx = setContextOp(ctx, icq.ctx, "Exist")
|
||||
switch _, err := icq.FirstID(ctx); {
|
||||
case IsNotFound(err):
|
||||
return false, nil
|
||||
case err != nil:
|
||||
return false, fmt.Errorf("ent: check existence: %w", err)
|
||||
default:
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
// ExistX is like Exist, but panics if an error occurs.
|
||||
func (icq *InvitationCodeQuery) ExistX(ctx context.Context) bool {
|
||||
exist, err := icq.Exist(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return exist
|
||||
}
|
||||
|
||||
// Clone returns a duplicate of the InvitationCodeQuery builder, including all associated steps. It can be
|
||||
// used to prepare common query builders and use them differently after the clone is made.
|
||||
func (icq *InvitationCodeQuery) Clone() *InvitationCodeQuery {
|
||||
if icq == nil {
|
||||
return nil
|
||||
}
|
||||
return &InvitationCodeQuery{
|
||||
config: icq.config,
|
||||
ctx: icq.ctx.Clone(),
|
||||
order: append([]invitationcode.OrderOption{}, icq.order...),
|
||||
inters: append([]Interceptor{}, icq.inters...),
|
||||
predicates: append([]predicate.InvitationCode{}, icq.predicates...),
|
||||
// clone intermediate query.
|
||||
sql: icq.sql.Clone(),
|
||||
path: icq.path,
|
||||
}
|
||||
}
|
||||
|
||||
// GroupBy is used to group vertices by one or more fields/columns.
|
||||
// It is often used with aggregate functions, like: count, max, mean, min, sum.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// var v []struct {
|
||||
// CreatedAt time.Time `json:"created_at,omitempty"`
|
||||
// Count int `json:"count,omitempty"`
|
||||
// }
|
||||
//
|
||||
// client.InvitationCode.Query().
|
||||
// GroupBy(invitationcode.FieldCreatedAt).
|
||||
// Aggregate(ent.Count()).
|
||||
// Scan(ctx, &v)
|
||||
func (icq *InvitationCodeQuery) GroupBy(field string, fields ...string) *InvitationCodeGroupBy {
|
||||
icq.ctx.Fields = append([]string{field}, fields...)
|
||||
grbuild := &InvitationCodeGroupBy{build: icq}
|
||||
grbuild.flds = &icq.ctx.Fields
|
||||
grbuild.label = invitationcode.Label
|
||||
grbuild.scan = grbuild.Scan
|
||||
return grbuild
|
||||
}
|
||||
|
||||
// Select allows the selection one or more fields/columns for the given query,
|
||||
// instead of selecting all fields in the entity.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// var v []struct {
|
||||
// CreatedAt time.Time `json:"created_at,omitempty"`
|
||||
// }
|
||||
//
|
||||
// client.InvitationCode.Query().
|
||||
// Select(invitationcode.FieldCreatedAt).
|
||||
// Scan(ctx, &v)
|
||||
func (icq *InvitationCodeQuery) Select(fields ...string) *InvitationCodeSelect {
|
||||
icq.ctx.Fields = append(icq.ctx.Fields, fields...)
|
||||
sbuild := &InvitationCodeSelect{InvitationCodeQuery: icq}
|
||||
sbuild.label = invitationcode.Label
|
||||
sbuild.flds, sbuild.scan = &icq.ctx.Fields, sbuild.Scan
|
||||
return sbuild
|
||||
}
|
||||
|
||||
// Aggregate returns a InvitationCodeSelect configured with the given aggregations.
|
||||
func (icq *InvitationCodeQuery) Aggregate(fns ...AggregateFunc) *InvitationCodeSelect {
|
||||
return icq.Select().Aggregate(fns...)
|
||||
}
|
||||
|
||||
func (icq *InvitationCodeQuery) prepareQuery(ctx context.Context) error {
|
||||
for _, inter := range icq.inters {
|
||||
if inter == nil {
|
||||
return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
|
||||
}
|
||||
if trv, ok := inter.(Traverser); ok {
|
||||
if err := trv.Traverse(ctx, icq); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, f := range icq.ctx.Fields {
|
||||
if !invitationcode.ValidColumn(f) {
|
||||
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
||||
}
|
||||
}
|
||||
if icq.path != nil {
|
||||
prev, err := icq.path(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
icq.sql = prev
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (icq *InvitationCodeQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*InvitationCode, error) {
|
||||
var (
|
||||
nodes = []*InvitationCode{}
|
||||
_spec = icq.querySpec()
|
||||
)
|
||||
_spec.ScanValues = func(columns []string) ([]any, error) {
|
||||
return (*InvitationCode).scanValues(nil, columns)
|
||||
}
|
||||
_spec.Assign = func(columns []string, values []any) error {
|
||||
node := &InvitationCode{config: icq.config}
|
||||
nodes = append(nodes, node)
|
||||
return node.assignValues(columns, values)
|
||||
}
|
||||
for i := range hooks {
|
||||
hooks[i](ctx, _spec)
|
||||
}
|
||||
if err := sqlgraph.QueryNodes(ctx, icq.driver, _spec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(nodes) == 0 {
|
||||
return nodes, nil
|
||||
}
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
func (icq *InvitationCodeQuery) sqlCount(ctx context.Context) (int, error) {
|
||||
_spec := icq.querySpec()
|
||||
_spec.Node.Columns = icq.ctx.Fields
|
||||
if len(icq.ctx.Fields) > 0 {
|
||||
_spec.Unique = icq.ctx.Unique != nil && *icq.ctx.Unique
|
||||
}
|
||||
return sqlgraph.CountNodes(ctx, icq.driver, _spec)
|
||||
}
|
||||
|
||||
func (icq *InvitationCodeQuery) querySpec() *sqlgraph.QuerySpec {
|
||||
_spec := sqlgraph.NewQuerySpec(invitationcode.Table, invitationcode.Columns, sqlgraph.NewFieldSpec(invitationcode.FieldID, field.TypeInt))
|
||||
_spec.From = icq.sql
|
||||
if unique := icq.ctx.Unique; unique != nil {
|
||||
_spec.Unique = *unique
|
||||
} else if icq.path != nil {
|
||||
_spec.Unique = true
|
||||
}
|
||||
if fields := icq.ctx.Fields; len(fields) > 0 {
|
||||
_spec.Node.Columns = make([]string, 0, len(fields))
|
||||
_spec.Node.Columns = append(_spec.Node.Columns, invitationcode.FieldID)
|
||||
for i := range fields {
|
||||
if fields[i] != invitationcode.FieldID {
|
||||
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
if ps := icq.predicates; len(ps) > 0 {
|
||||
_spec.Predicate = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
if limit := icq.ctx.Limit; limit != nil {
|
||||
_spec.Limit = *limit
|
||||
}
|
||||
if offset := icq.ctx.Offset; offset != nil {
|
||||
_spec.Offset = *offset
|
||||
}
|
||||
if ps := icq.order; len(ps) > 0 {
|
||||
_spec.Order = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
return _spec
|
||||
}
|
||||
|
||||
func (icq *InvitationCodeQuery) sqlQuery(ctx context.Context) *sql.Selector {
|
||||
builder := sql.Dialect(icq.driver.Dialect())
|
||||
t1 := builder.Table(invitationcode.Table)
|
||||
columns := icq.ctx.Fields
|
||||
if len(columns) == 0 {
|
||||
columns = invitationcode.Columns
|
||||
}
|
||||
selector := builder.Select(t1.Columns(columns...)...).From(t1)
|
||||
if icq.sql != nil {
|
||||
selector = icq.sql
|
||||
selector.Select(selector.Columns(columns...)...)
|
||||
}
|
||||
if icq.ctx.Unique != nil && *icq.ctx.Unique {
|
||||
selector.Distinct()
|
||||
}
|
||||
for _, p := range icq.predicates {
|
||||
p(selector)
|
||||
}
|
||||
for _, p := range icq.order {
|
||||
p(selector)
|
||||
}
|
||||
if offset := icq.ctx.Offset; offset != nil {
|
||||
// limit is mandatory for offset clause. We start
|
||||
// with default value, and override it below if needed.
|
||||
selector.Offset(*offset).Limit(math.MaxInt32)
|
||||
}
|
||||
if limit := icq.ctx.Limit; limit != nil {
|
||||
selector.Limit(*limit)
|
||||
}
|
||||
return selector
|
||||
}
|
||||
|
||||
// InvitationCodeGroupBy is the group-by builder for InvitationCode entities.
|
||||
type InvitationCodeGroupBy struct {
|
||||
selector
|
||||
build *InvitationCodeQuery
|
||||
}
|
||||
|
||||
// Aggregate adds the given aggregation functions to the group-by query.
|
||||
func (icgb *InvitationCodeGroupBy) Aggregate(fns ...AggregateFunc) *InvitationCodeGroupBy {
|
||||
icgb.fns = append(icgb.fns, fns...)
|
||||
return icgb
|
||||
}
|
||||
|
||||
// Scan applies the selector query and scans the result into the given value.
|
||||
func (icgb *InvitationCodeGroupBy) Scan(ctx context.Context, v any) error {
|
||||
ctx = setContextOp(ctx, icgb.build.ctx, "GroupBy")
|
||||
if err := icgb.build.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return scanWithInterceptors[*InvitationCodeQuery, *InvitationCodeGroupBy](ctx, icgb.build, icgb, icgb.build.inters, v)
|
||||
}
|
||||
|
||||
func (icgb *InvitationCodeGroupBy) sqlScan(ctx context.Context, root *InvitationCodeQuery, v any) error {
|
||||
selector := root.sqlQuery(ctx).Select()
|
||||
aggregation := make([]string, 0, len(icgb.fns))
|
||||
for _, fn := range icgb.fns {
|
||||
aggregation = append(aggregation, fn(selector))
|
||||
}
|
||||
if len(selector.SelectedColumns()) == 0 {
|
||||
columns := make([]string, 0, len(*icgb.flds)+len(icgb.fns))
|
||||
for _, f := range *icgb.flds {
|
||||
columns = append(columns, selector.C(f))
|
||||
}
|
||||
columns = append(columns, aggregation...)
|
||||
selector.Select(columns...)
|
||||
}
|
||||
selector.GroupBy(selector.Columns(*icgb.flds...)...)
|
||||
if err := selector.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
rows := &sql.Rows{}
|
||||
query, args := selector.Query()
|
||||
if err := icgb.build.driver.Query(ctx, query, args, rows); err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
return sql.ScanSlice(rows, v)
|
||||
}
|
||||
|
||||
// InvitationCodeSelect is the builder for selecting fields of InvitationCode entities.
|
||||
type InvitationCodeSelect struct {
|
||||
*InvitationCodeQuery
|
||||
selector
|
||||
}
|
||||
|
||||
// Aggregate adds the given aggregation functions to the selector query.
|
||||
func (ics *InvitationCodeSelect) Aggregate(fns ...AggregateFunc) *InvitationCodeSelect {
|
||||
ics.fns = append(ics.fns, fns...)
|
||||
return ics
|
||||
}
|
||||
|
||||
// Scan applies the selector query and scans the result into the given value.
|
||||
func (ics *InvitationCodeSelect) Scan(ctx context.Context, v any) error {
|
||||
ctx = setContextOp(ctx, ics.ctx, "Select")
|
||||
if err := ics.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return scanWithInterceptors[*InvitationCodeQuery, *InvitationCodeSelect](ctx, ics.InvitationCodeQuery, ics, ics.inters, v)
|
||||
}
|
||||
|
||||
func (ics *InvitationCodeSelect) sqlScan(ctx context.Context, root *InvitationCodeQuery, v any) error {
|
||||
selector := root.sqlQuery(ctx)
|
||||
aggregation := make([]string, 0, len(ics.fns))
|
||||
for _, fn := range ics.fns {
|
||||
aggregation = append(aggregation, fn(selector))
|
||||
}
|
||||
switch n := len(*ics.selector.flds); {
|
||||
case n == 0 && len(aggregation) > 0:
|
||||
selector.Select(aggregation...)
|
||||
case n != 0 && len(aggregation) > 0:
|
||||
selector.AppendSelect(aggregation...)
|
||||
}
|
||||
rows := &sql.Rows{}
|
||||
query, args := selector.Query()
|
||||
if err := ics.driver.Query(ctx, query, args, rows); err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
return sql.ScanSlice(rows, v)
|
||||
}
|
||||
@ -0,0 +1,550 @@
|
||||
// Code generated by ent, DO NOT EDIT.
|
||||
|
||||
package ent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/schema/field"
|
||||
"github.com/cloudreve/Cloudreve/v4/ent/invitationcode"
|
||||
"github.com/cloudreve/Cloudreve/v4/ent/predicate"
|
||||
)
|
||||
|
||||
// InvitationCodeUpdate is the builder for updating InvitationCode entities.
|
||||
type InvitationCodeUpdate struct {
|
||||
config
|
||||
hooks []Hook
|
||||
mutation *InvitationCodeMutation
|
||||
}
|
||||
|
||||
// Where appends a list predicates to the InvitationCodeUpdate builder.
|
||||
func (icu *InvitationCodeUpdate) Where(ps ...predicate.InvitationCode) *InvitationCodeUpdate {
|
||||
icu.mutation.Where(ps...)
|
||||
return icu
|
||||
}
|
||||
|
||||
// SetUpdatedAt sets the "updated_at" field.
|
||||
func (icu *InvitationCodeUpdate) SetUpdatedAt(t time.Time) *InvitationCodeUpdate {
|
||||
icu.mutation.SetUpdatedAt(t)
|
||||
return icu
|
||||
}
|
||||
|
||||
// SetDeletedAt sets the "deleted_at" field.
|
||||
func (icu *InvitationCodeUpdate) SetDeletedAt(t time.Time) *InvitationCodeUpdate {
|
||||
icu.mutation.SetDeletedAt(t)
|
||||
return icu
|
||||
}
|
||||
|
||||
// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
|
||||
func (icu *InvitationCodeUpdate) SetNillableDeletedAt(t *time.Time) *InvitationCodeUpdate {
|
||||
if t != nil {
|
||||
icu.SetDeletedAt(*t)
|
||||
}
|
||||
return icu
|
||||
}
|
||||
|
||||
// ClearDeletedAt clears the value of the "deleted_at" field.
|
||||
func (icu *InvitationCodeUpdate) ClearDeletedAt() *InvitationCodeUpdate {
|
||||
icu.mutation.ClearDeletedAt()
|
||||
return icu
|
||||
}
|
||||
|
||||
// SetCode sets the "code" field.
|
||||
func (icu *InvitationCodeUpdate) SetCode(s string) *InvitationCodeUpdate {
|
||||
icu.mutation.SetCode(s)
|
||||
return icu
|
||||
}
|
||||
|
||||
// SetNillableCode sets the "code" field if the given value is not nil.
|
||||
func (icu *InvitationCodeUpdate) SetNillableCode(s *string) *InvitationCodeUpdate {
|
||||
if s != nil {
|
||||
icu.SetCode(*s)
|
||||
}
|
||||
return icu
|
||||
}
|
||||
|
||||
// SetGroupID sets the "group_id" field.
|
||||
func (icu *InvitationCodeUpdate) SetGroupID(i int) *InvitationCodeUpdate {
|
||||
icu.mutation.ResetGroupID()
|
||||
icu.mutation.SetGroupID(i)
|
||||
return icu
|
||||
}
|
||||
|
||||
// SetNillableGroupID sets the "group_id" field if the given value is not nil.
|
||||
func (icu *InvitationCodeUpdate) SetNillableGroupID(i *int) *InvitationCodeUpdate {
|
||||
if i != nil {
|
||||
icu.SetGroupID(*i)
|
||||
}
|
||||
return icu
|
||||
}
|
||||
|
||||
// AddGroupID adds i to the "group_id" field.
|
||||
func (icu *InvitationCodeUpdate) AddGroupID(i int) *InvitationCodeUpdate {
|
||||
icu.mutation.AddGroupID(i)
|
||||
return icu
|
||||
}
|
||||
|
||||
// SetMaxUses sets the "max_uses" field.
|
||||
func (icu *InvitationCodeUpdate) SetMaxUses(i int) *InvitationCodeUpdate {
|
||||
icu.mutation.ResetMaxUses()
|
||||
icu.mutation.SetMaxUses(i)
|
||||
return icu
|
||||
}
|
||||
|
||||
// SetNillableMaxUses sets the "max_uses" field if the given value is not nil.
|
||||
func (icu *InvitationCodeUpdate) SetNillableMaxUses(i *int) *InvitationCodeUpdate {
|
||||
if i != nil {
|
||||
icu.SetMaxUses(*i)
|
||||
}
|
||||
return icu
|
||||
}
|
||||
|
||||
// AddMaxUses adds i to the "max_uses" field.
|
||||
func (icu *InvitationCodeUpdate) AddMaxUses(i int) *InvitationCodeUpdate {
|
||||
icu.mutation.AddMaxUses(i)
|
||||
return icu
|
||||
}
|
||||
|
||||
// SetUsedCount sets the "used_count" field.
|
||||
func (icu *InvitationCodeUpdate) SetUsedCount(i int) *InvitationCodeUpdate {
|
||||
icu.mutation.ResetUsedCount()
|
||||
icu.mutation.SetUsedCount(i)
|
||||
return icu
|
||||
}
|
||||
|
||||
// SetNillableUsedCount sets the "used_count" field if the given value is not nil.
|
||||
func (icu *InvitationCodeUpdate) SetNillableUsedCount(i *int) *InvitationCodeUpdate {
|
||||
if i != nil {
|
||||
icu.SetUsedCount(*i)
|
||||
}
|
||||
return icu
|
||||
}
|
||||
|
||||
// AddUsedCount adds i to the "used_count" field.
|
||||
func (icu *InvitationCodeUpdate) AddUsedCount(i int) *InvitationCodeUpdate {
|
||||
icu.mutation.AddUsedCount(i)
|
||||
return icu
|
||||
}
|
||||
|
||||
// SetExpiresAt sets the "expires_at" field.
|
||||
func (icu *InvitationCodeUpdate) SetExpiresAt(t time.Time) *InvitationCodeUpdate {
|
||||
icu.mutation.SetExpiresAt(t)
|
||||
return icu
|
||||
}
|
||||
|
||||
// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
|
||||
func (icu *InvitationCodeUpdate) SetNillableExpiresAt(t *time.Time) *InvitationCodeUpdate {
|
||||
if t != nil {
|
||||
icu.SetExpiresAt(*t)
|
||||
}
|
||||
return icu
|
||||
}
|
||||
|
||||
// ClearExpiresAt clears the value of the "expires_at" field.
|
||||
func (icu *InvitationCodeUpdate) ClearExpiresAt() *InvitationCodeUpdate {
|
||||
icu.mutation.ClearExpiresAt()
|
||||
return icu
|
||||
}
|
||||
|
||||
// Mutation returns the InvitationCodeMutation object of the builder.
|
||||
func (icu *InvitationCodeUpdate) Mutation() *InvitationCodeMutation {
|
||||
return icu.mutation
|
||||
}
|
||||
|
||||
// Save executes the query and returns the number of nodes affected by the update operation.
|
||||
func (icu *InvitationCodeUpdate) Save(ctx context.Context) (int, error) {
|
||||
if err := icu.defaults(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return withHooks(ctx, icu.sqlSave, icu.mutation, icu.hooks)
|
||||
}
|
||||
|
||||
// SaveX is like Save, but panics if an error occurs.
|
||||
func (icu *InvitationCodeUpdate) SaveX(ctx context.Context) int {
|
||||
affected, err := icu.Save(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return affected
|
||||
}
|
||||
|
||||
// Exec executes the query.
|
||||
func (icu *InvitationCodeUpdate) Exec(ctx context.Context) error {
|
||||
_, err := icu.Save(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
func (icu *InvitationCodeUpdate) ExecX(ctx context.Context) {
|
||||
if err := icu.Exec(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// defaults sets the default values of the builder before save.
|
||||
func (icu *InvitationCodeUpdate) defaults() error {
|
||||
if _, ok := icu.mutation.UpdatedAt(); !ok {
|
||||
if invitationcode.UpdateDefaultUpdatedAt == nil {
|
||||
return fmt.Errorf("ent: uninitialized invitationcode.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
|
||||
}
|
||||
v := invitationcode.UpdateDefaultUpdatedAt()
|
||||
icu.mutation.SetUpdatedAt(v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (icu *InvitationCodeUpdate) check() error {
|
||||
if v, ok := icu.mutation.Code(); ok {
|
||||
if err := invitationcode.CodeValidator(v); err != nil {
|
||||
return &ValidationError{Name: "code", err: fmt.Errorf(`ent: validator failed for field "InvitationCode.code": %w`, err)}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (icu *InvitationCodeUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
if err := icu.check(); err != nil {
|
||||
return n, err
|
||||
}
|
||||
_spec := sqlgraph.NewUpdateSpec(invitationcode.Table, invitationcode.Columns, sqlgraph.NewFieldSpec(invitationcode.FieldID, field.TypeInt))
|
||||
if ps := icu.mutation.predicates; len(ps) > 0 {
|
||||
_spec.Predicate = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
if value, ok := icu.mutation.UpdatedAt(); ok {
|
||||
_spec.SetField(invitationcode.FieldUpdatedAt, field.TypeTime, value)
|
||||
}
|
||||
if value, ok := icu.mutation.DeletedAt(); ok {
|
||||
_spec.SetField(invitationcode.FieldDeletedAt, field.TypeTime, value)
|
||||
}
|
||||
if icu.mutation.DeletedAtCleared() {
|
||||
_spec.ClearField(invitationcode.FieldDeletedAt, field.TypeTime)
|
||||
}
|
||||
if value, ok := icu.mutation.Code(); ok {
|
||||
_spec.SetField(invitationcode.FieldCode, field.TypeString, value)
|
||||
}
|
||||
if value, ok := icu.mutation.GroupID(); ok {
|
||||
_spec.SetField(invitationcode.FieldGroupID, field.TypeInt, value)
|
||||
}
|
||||
if value, ok := icu.mutation.AddedGroupID(); ok {
|
||||
_spec.AddField(invitationcode.FieldGroupID, field.TypeInt, value)
|
||||
}
|
||||
if value, ok := icu.mutation.MaxUses(); ok {
|
||||
_spec.SetField(invitationcode.FieldMaxUses, field.TypeInt, value)
|
||||
}
|
||||
if value, ok := icu.mutation.AddedMaxUses(); ok {
|
||||
_spec.AddField(invitationcode.FieldMaxUses, field.TypeInt, value)
|
||||
}
|
||||
if value, ok := icu.mutation.UsedCount(); ok {
|
||||
_spec.SetField(invitationcode.FieldUsedCount, field.TypeInt, value)
|
||||
}
|
||||
if value, ok := icu.mutation.AddedUsedCount(); ok {
|
||||
_spec.AddField(invitationcode.FieldUsedCount, field.TypeInt, value)
|
||||
}
|
||||
if value, ok := icu.mutation.ExpiresAt(); ok {
|
||||
_spec.SetField(invitationcode.FieldExpiresAt, field.TypeTime, value)
|
||||
}
|
||||
if icu.mutation.ExpiresAtCleared() {
|
||||
_spec.ClearField(invitationcode.FieldExpiresAt, field.TypeTime)
|
||||
}
|
||||
if n, err = sqlgraph.UpdateNodes(ctx, icu.driver, _spec); err != nil {
|
||||
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
||||
err = &NotFoundError{invitationcode.Label}
|
||||
} else if sqlgraph.IsConstraintError(err) {
|
||||
err = &ConstraintError{msg: err.Error(), wrap: err}
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
icu.mutation.done = true
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// InvitationCodeUpdateOne is the builder for updating a single InvitationCode entity.
|
||||
type InvitationCodeUpdateOne struct {
|
||||
config
|
||||
fields []string
|
||||
hooks []Hook
|
||||
mutation *InvitationCodeMutation
|
||||
}
|
||||
|
||||
// SetUpdatedAt sets the "updated_at" field.
|
||||
func (icuo *InvitationCodeUpdateOne) SetUpdatedAt(t time.Time) *InvitationCodeUpdateOne {
|
||||
icuo.mutation.SetUpdatedAt(t)
|
||||
return icuo
|
||||
}
|
||||
|
||||
// SetDeletedAt sets the "deleted_at" field.
|
||||
func (icuo *InvitationCodeUpdateOne) SetDeletedAt(t time.Time) *InvitationCodeUpdateOne {
|
||||
icuo.mutation.SetDeletedAt(t)
|
||||
return icuo
|
||||
}
|
||||
|
||||
// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
|
||||
func (icuo *InvitationCodeUpdateOne) SetNillableDeletedAt(t *time.Time) *InvitationCodeUpdateOne {
|
||||
if t != nil {
|
||||
icuo.SetDeletedAt(*t)
|
||||
}
|
||||
return icuo
|
||||
}
|
||||
|
||||
// ClearDeletedAt clears the value of the "deleted_at" field.
|
||||
func (icuo *InvitationCodeUpdateOne) ClearDeletedAt() *InvitationCodeUpdateOne {
|
||||
icuo.mutation.ClearDeletedAt()
|
||||
return icuo
|
||||
}
|
||||
|
||||
// SetCode sets the "code" field.
|
||||
func (icuo *InvitationCodeUpdateOne) SetCode(s string) *InvitationCodeUpdateOne {
|
||||
icuo.mutation.SetCode(s)
|
||||
return icuo
|
||||
}
|
||||
|
||||
// SetNillableCode sets the "code" field if the given value is not nil.
|
||||
func (icuo *InvitationCodeUpdateOne) SetNillableCode(s *string) *InvitationCodeUpdateOne {
|
||||
if s != nil {
|
||||
icuo.SetCode(*s)
|
||||
}
|
||||
return icuo
|
||||
}
|
||||
|
||||
// SetGroupID sets the "group_id" field.
|
||||
func (icuo *InvitationCodeUpdateOne) SetGroupID(i int) *InvitationCodeUpdateOne {
|
||||
icuo.mutation.ResetGroupID()
|
||||
icuo.mutation.SetGroupID(i)
|
||||
return icuo
|
||||
}
|
||||
|
||||
// SetNillableGroupID sets the "group_id" field if the given value is not nil.
|
||||
func (icuo *InvitationCodeUpdateOne) SetNillableGroupID(i *int) *InvitationCodeUpdateOne {
|
||||
if i != nil {
|
||||
icuo.SetGroupID(*i)
|
||||
}
|
||||
return icuo
|
||||
}
|
||||
|
||||
// AddGroupID adds i to the "group_id" field.
|
||||
func (icuo *InvitationCodeUpdateOne) AddGroupID(i int) *InvitationCodeUpdateOne {
|
||||
icuo.mutation.AddGroupID(i)
|
||||
return icuo
|
||||
}
|
||||
|
||||
// SetMaxUses sets the "max_uses" field.
|
||||
func (icuo *InvitationCodeUpdateOne) SetMaxUses(i int) *InvitationCodeUpdateOne {
|
||||
icuo.mutation.ResetMaxUses()
|
||||
icuo.mutation.SetMaxUses(i)
|
||||
return icuo
|
||||
}
|
||||
|
||||
// SetNillableMaxUses sets the "max_uses" field if the given value is not nil.
|
||||
func (icuo *InvitationCodeUpdateOne) SetNillableMaxUses(i *int) *InvitationCodeUpdateOne {
|
||||
if i != nil {
|
||||
icuo.SetMaxUses(*i)
|
||||
}
|
||||
return icuo
|
||||
}
|
||||
|
||||
// AddMaxUses adds i to the "max_uses" field.
|
||||
func (icuo *InvitationCodeUpdateOne) AddMaxUses(i int) *InvitationCodeUpdateOne {
|
||||
icuo.mutation.AddMaxUses(i)
|
||||
return icuo
|
||||
}
|
||||
|
||||
// SetUsedCount sets the "used_count" field.
|
||||
func (icuo *InvitationCodeUpdateOne) SetUsedCount(i int) *InvitationCodeUpdateOne {
|
||||
icuo.mutation.ResetUsedCount()
|
||||
icuo.mutation.SetUsedCount(i)
|
||||
return icuo
|
||||
}
|
||||
|
||||
// SetNillableUsedCount sets the "used_count" field if the given value is not nil.
|
||||
func (icuo *InvitationCodeUpdateOne) SetNillableUsedCount(i *int) *InvitationCodeUpdateOne {
|
||||
if i != nil {
|
||||
icuo.SetUsedCount(*i)
|
||||
}
|
||||
return icuo
|
||||
}
|
||||
|
||||
// AddUsedCount adds i to the "used_count" field.
|
||||
func (icuo *InvitationCodeUpdateOne) AddUsedCount(i int) *InvitationCodeUpdateOne {
|
||||
icuo.mutation.AddUsedCount(i)
|
||||
return icuo
|
||||
}
|
||||
|
||||
// SetExpiresAt sets the "expires_at" field.
|
||||
func (icuo *InvitationCodeUpdateOne) SetExpiresAt(t time.Time) *InvitationCodeUpdateOne {
|
||||
icuo.mutation.SetExpiresAt(t)
|
||||
return icuo
|
||||
}
|
||||
|
||||
// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
|
||||
func (icuo *InvitationCodeUpdateOne) SetNillableExpiresAt(t *time.Time) *InvitationCodeUpdateOne {
|
||||
if t != nil {
|
||||
icuo.SetExpiresAt(*t)
|
||||
}
|
||||
return icuo
|
||||
}
|
||||
|
||||
// ClearExpiresAt clears the value of the "expires_at" field.
|
||||
func (icuo *InvitationCodeUpdateOne) ClearExpiresAt() *InvitationCodeUpdateOne {
|
||||
icuo.mutation.ClearExpiresAt()
|
||||
return icuo
|
||||
}
|
||||
|
||||
// Mutation returns the InvitationCodeMutation object of the builder.
|
||||
func (icuo *InvitationCodeUpdateOne) Mutation() *InvitationCodeMutation {
|
||||
return icuo.mutation
|
||||
}
|
||||
|
||||
// Where appends a list predicates to the InvitationCodeUpdate builder.
|
||||
func (icuo *InvitationCodeUpdateOne) Where(ps ...predicate.InvitationCode) *InvitationCodeUpdateOne {
|
||||
icuo.mutation.Where(ps...)
|
||||
return icuo
|
||||
}
|
||||
|
||||
// Select allows selecting one or more fields (columns) of the returned entity.
|
||||
// The default is selecting all fields defined in the entity schema.
|
||||
func (icuo *InvitationCodeUpdateOne) Select(field string, fields ...string) *InvitationCodeUpdateOne {
|
||||
icuo.fields = append([]string{field}, fields...)
|
||||
return icuo
|
||||
}
|
||||
|
||||
// Save executes the query and returns the updated InvitationCode entity.
|
||||
func (icuo *InvitationCodeUpdateOne) Save(ctx context.Context) (*InvitationCode, error) {
|
||||
if err := icuo.defaults(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return withHooks(ctx, icuo.sqlSave, icuo.mutation, icuo.hooks)
|
||||
}
|
||||
|
||||
// SaveX is like Save, but panics if an error occurs.
|
||||
func (icuo *InvitationCodeUpdateOne) SaveX(ctx context.Context) *InvitationCode {
|
||||
node, err := icuo.Save(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
// Exec executes the query on the entity.
|
||||
func (icuo *InvitationCodeUpdateOne) Exec(ctx context.Context) error {
|
||||
_, err := icuo.Save(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
func (icuo *InvitationCodeUpdateOne) ExecX(ctx context.Context) {
|
||||
if err := icuo.Exec(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// defaults sets the default values of the builder before save.
|
||||
func (icuo *InvitationCodeUpdateOne) defaults() error {
|
||||
if _, ok := icuo.mutation.UpdatedAt(); !ok {
|
||||
if invitationcode.UpdateDefaultUpdatedAt == nil {
|
||||
return fmt.Errorf("ent: uninitialized invitationcode.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
|
||||
}
|
||||
v := invitationcode.UpdateDefaultUpdatedAt()
|
||||
icuo.mutation.SetUpdatedAt(v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (icuo *InvitationCodeUpdateOne) check() error {
|
||||
if v, ok := icuo.mutation.Code(); ok {
|
||||
if err := invitationcode.CodeValidator(v); err != nil {
|
||||
return &ValidationError{Name: "code", err: fmt.Errorf(`ent: validator failed for field "InvitationCode.code": %w`, err)}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (icuo *InvitationCodeUpdateOne) sqlSave(ctx context.Context) (_node *InvitationCode, err error) {
|
||||
if err := icuo.check(); err != nil {
|
||||
return _node, err
|
||||
}
|
||||
_spec := sqlgraph.NewUpdateSpec(invitationcode.Table, invitationcode.Columns, sqlgraph.NewFieldSpec(invitationcode.FieldID, field.TypeInt))
|
||||
id, ok := icuo.mutation.ID()
|
||||
if !ok {
|
||||
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "InvitationCode.id" for update`)}
|
||||
}
|
||||
_spec.Node.ID.Value = id
|
||||
if fields := icuo.fields; len(fields) > 0 {
|
||||
_spec.Node.Columns = make([]string, 0, len(fields))
|
||||
_spec.Node.Columns = append(_spec.Node.Columns, invitationcode.FieldID)
|
||||
for _, f := range fields {
|
||||
if !invitationcode.ValidColumn(f) {
|
||||
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
||||
}
|
||||
if f != invitationcode.FieldID {
|
||||
_spec.Node.Columns = append(_spec.Node.Columns, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
if ps := icuo.mutation.predicates; len(ps) > 0 {
|
||||
_spec.Predicate = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
if value, ok := icuo.mutation.UpdatedAt(); ok {
|
||||
_spec.SetField(invitationcode.FieldUpdatedAt, field.TypeTime, value)
|
||||
}
|
||||
if value, ok := icuo.mutation.DeletedAt(); ok {
|
||||
_spec.SetField(invitationcode.FieldDeletedAt, field.TypeTime, value)
|
||||
}
|
||||
if icuo.mutation.DeletedAtCleared() {
|
||||
_spec.ClearField(invitationcode.FieldDeletedAt, field.TypeTime)
|
||||
}
|
||||
if value, ok := icuo.mutation.Code(); ok {
|
||||
_spec.SetField(invitationcode.FieldCode, field.TypeString, value)
|
||||
}
|
||||
if value, ok := icuo.mutation.GroupID(); ok {
|
||||
_spec.SetField(invitationcode.FieldGroupID, field.TypeInt, value)
|
||||
}
|
||||
if value, ok := icuo.mutation.AddedGroupID(); ok {
|
||||
_spec.AddField(invitationcode.FieldGroupID, field.TypeInt, value)
|
||||
}
|
||||
if value, ok := icuo.mutation.MaxUses(); ok {
|
||||
_spec.SetField(invitationcode.FieldMaxUses, field.TypeInt, value)
|
||||
}
|
||||
if value, ok := icuo.mutation.AddedMaxUses(); ok {
|
||||
_spec.AddField(invitationcode.FieldMaxUses, field.TypeInt, value)
|
||||
}
|
||||
if value, ok := icuo.mutation.UsedCount(); ok {
|
||||
_spec.SetField(invitationcode.FieldUsedCount, field.TypeInt, value)
|
||||
}
|
||||
if value, ok := icuo.mutation.AddedUsedCount(); ok {
|
||||
_spec.AddField(invitationcode.FieldUsedCount, field.TypeInt, value)
|
||||
}
|
||||
if value, ok := icuo.mutation.ExpiresAt(); ok {
|
||||
_spec.SetField(invitationcode.FieldExpiresAt, field.TypeTime, value)
|
||||
}
|
||||
if icuo.mutation.ExpiresAtCleared() {
|
||||
_spec.ClearField(invitationcode.FieldExpiresAt, field.TypeTime)
|
||||
}
|
||||
_node = &InvitationCode{config: icuo.config}
|
||||
_spec.Assign = _node.assignValues
|
||||
_spec.ScanValues = _node.scanValues
|
||||
if err = sqlgraph.UpdateNode(ctx, icuo.driver, _spec); err != nil {
|
||||
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
||||
err = &NotFoundError{invitationcode.Label}
|
||||
} else if sqlgraph.IsConstraintError(err) {
|
||||
err = &ConstraintError{msg: err.Error(), wrap: err}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
icuo.mutation.done = true
|
||||
return _node, nil
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// InvitationCode holds the schema definition for invitation codes that can
|
||||
// gate public registration.
|
||||
type InvitationCode struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
func (InvitationCode) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("code").
|
||||
MaxLen(64).
|
||||
Unique(),
|
||||
// group_id assigns new users to this group; 0 means the default group.
|
||||
field.Int("group_id").
|
||||
Default(0),
|
||||
// max_uses limits how many accounts the code can create; 0 = unlimited.
|
||||
field.Int("max_uses").
|
||||
Default(1),
|
||||
field.Int("used_count").
|
||||
Default(0),
|
||||
field.Time("expires_at").
|
||||
Optional().
|
||||
Nillable(),
|
||||
}
|
||||
}
|
||||
|
||||
func (InvitationCode) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
CommonMixin{},
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,241 @@
|
||||
import { Delete } from "@mui/icons-material";
|
||||
import {
|
||||
Box,
|
||||
Button,
|
||||
DialogContent,
|
||||
IconButton,
|
||||
Stack,
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableContainer,
|
||||
TableHead,
|
||||
TableRow,
|
||||
Tooltip,
|
||||
Typography,
|
||||
} from "@mui/material";
|
||||
import { useSnackbar } from "notistack";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import {
|
||||
createInvitationCode,
|
||||
deleteInvitationCode,
|
||||
InvitationCode,
|
||||
listInvitationCodes,
|
||||
} from "../../../api/api";
|
||||
import { useAppDispatch } from "../../../redux/hooks";
|
||||
import { DefaultCloseAction } from "../../Common/Snackbar/snackbar";
|
||||
import { DenseFilledTextField, NoWrapTableCell, SecondaryButton, StyledTableContainerPaper } from "../../Common/StyledComponents";
|
||||
import DraggableDialog from "../../Dialogs/DraggableDialog";
|
||||
import Add from "../../Icons/Add";
|
||||
import ArrowSync from "../../Icons/ArrowSync";
|
||||
import CopyOutlined from "../../Icons/CopyOutlined";
|
||||
import SettingForm from "../../Pages/Setting/SettingForm";
|
||||
import GroupSelectionInput from "../Common/GroupSelectionInput";
|
||||
|
||||
export interface InvitationCodeDialogProps {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
const InvitationCodeDialog = ({ open, onClose }: InvitationCodeDialogProps) => {
|
||||
const { t } = useTranslation("dashboard");
|
||||
const dispatch = useAppDispatch();
|
||||
const { enqueueSnackbar } = useSnackbar();
|
||||
|
||||
const [codes, setCodes] = useState<InvitationCode[]>([]);
|
||||
const [nextToken, setNextToken] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [creating, setCreating] = useState(false);
|
||||
|
||||
const [code, setCode] = useState("");
|
||||
const [groupId, setGroupId] = useState("");
|
||||
const [maxUses, setMaxUses] = useState("1");
|
||||
const [expiresAt, setExpiresAt] = useState("");
|
||||
|
||||
const fetchCodes = useCallback(
|
||||
(token?: string) => {
|
||||
setLoading(true);
|
||||
dispatch(listInvitationCodes({ page_size: 20, page_token: token }))
|
||||
.then((res) => {
|
||||
setCodes((prev) => (token ? [...prev, ...res.codes] : res.codes));
|
||||
setNextToken(res.pagination.next_token ?? "");
|
||||
})
|
||||
.finally(() => setLoading(false));
|
||||
},
|
||||
[dispatch],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setCodes([]);
|
||||
setCode("");
|
||||
setGroupId("");
|
||||
setMaxUses("1");
|
||||
setExpiresAt("");
|
||||
fetchCodes();
|
||||
}
|
||||
}, [open, fetchCodes]);
|
||||
|
||||
const handleCreate = () => {
|
||||
setCreating(true);
|
||||
dispatch(
|
||||
createInvitationCode({
|
||||
code: code || undefined,
|
||||
group_id: groupId ? parseInt(groupId) : undefined,
|
||||
max_uses: maxUses ? parseInt(maxUses) : undefined,
|
||||
expires_at: expiresAt ? new Date(expiresAt).toISOString() : undefined,
|
||||
}),
|
||||
)
|
||||
.then(() => {
|
||||
setCode("");
|
||||
fetchCodes();
|
||||
enqueueSnackbar({ message: t("user.invitationCodeCreated"), variant: "success", action: DefaultCloseAction });
|
||||
})
|
||||
.finally(() => setCreating(false));
|
||||
};
|
||||
|
||||
const handleDelete = (id: number) => {
|
||||
dispatch(deleteInvitationCode(id)).then(() => fetchCodes());
|
||||
};
|
||||
|
||||
const handleCopy = (value: string) => {
|
||||
navigator.clipboard.writeText(value);
|
||||
enqueueSnackbar({ message: t("user.invitationCodeCopied"), variant: "success", action: DefaultCloseAction });
|
||||
};
|
||||
|
||||
const formatDate = (value?: string) => (value ? new Date(value).toLocaleString() : "-");
|
||||
|
||||
return (
|
||||
<DraggableDialog
|
||||
title={t("user.invitationCodes")}
|
||||
dialogProps={{
|
||||
open,
|
||||
onClose,
|
||||
fullWidth: true,
|
||||
maxWidth: "md",
|
||||
}}
|
||||
>
|
||||
<DialogContent>
|
||||
<Stack spacing={2}>
|
||||
<Typography variant="subtitle2" color="text.secondary">
|
||||
{t("user.invitationCodesDes")}
|
||||
</Typography>
|
||||
<Stack direction={{ xs: "column", sm: "row" }} spacing={1} alignItems={{ sm: "flex-end" }}>
|
||||
<SettingForm title={t("user.invitationCode")} lgWidth={4}>
|
||||
<DenseFilledTextField
|
||||
fullWidth
|
||||
placeholder={t("user.invitationCodeAuto")}
|
||||
value={code}
|
||||
onChange={(e) => setCode(e.target.value)}
|
||||
/>
|
||||
</SettingForm>
|
||||
<SettingForm title={t("user.invitationCodeGroup")} lgWidth={3}>
|
||||
<GroupSelectionInput value={groupId} onChange={setGroupId} fullWidth />
|
||||
</SettingForm>
|
||||
<SettingForm title={t("user.invitationCodeMaxUses")} lgWidth={2}>
|
||||
<DenseFilledTextField
|
||||
fullWidth
|
||||
type="number"
|
||||
slotProps={{ htmlInput: { min: 0 } }}
|
||||
value={maxUses}
|
||||
onChange={(e) => setMaxUses(e.target.value)}
|
||||
/>
|
||||
</SettingForm>
|
||||
<SettingForm title={t("user.invitationCodeExpires")} lgWidth={3}>
|
||||
<DenseFilledTextField
|
||||
fullWidth
|
||||
type="datetime-local"
|
||||
value={expiresAt}
|
||||
onChange={(e) => setExpiresAt(e.target.value)}
|
||||
/>
|
||||
</SettingForm>
|
||||
<Box sx={{ pb: 0.5 }}>
|
||||
<Button
|
||||
variant="contained"
|
||||
startIcon={<Add />}
|
||||
onClick={handleCreate}
|
||||
disabled={creating}
|
||||
sx={{ whiteSpace: "nowrap" }}
|
||||
>
|
||||
{t("group.create")}
|
||||
</Button>
|
||||
</Box>
|
||||
</Stack>
|
||||
|
||||
<Stack direction="row" justifyContent="flex-end">
|
||||
<SecondaryButton size="small" startIcon={<ArrowSync />} onClick={() => fetchCodes()} disabled={loading}>
|
||||
{t("node.refresh")}
|
||||
</SecondaryButton>
|
||||
</Stack>
|
||||
|
||||
<TableContainer component={StyledTableContainerPaper}>
|
||||
<Table size="small">
|
||||
<TableHead>
|
||||
<TableRow>
|
||||
<NoWrapTableCell>{t("user.invitationCode")}</NoWrapTableCell>
|
||||
<NoWrapTableCell>{t("user.group")}</NoWrapTableCell>
|
||||
<NoWrapTableCell>{t("user.invitationCodeUses")}</NoWrapTableCell>
|
||||
<NoWrapTableCell>{t("user.invitationCodeExpires")}</NoWrapTableCell>
|
||||
<NoWrapTableCell>{t("user.createdAt")}</NoWrapTableCell>
|
||||
<NoWrapTableCell align="right" />
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{codes.map((c) => {
|
||||
const expired = !!c.expires_at && new Date(c.expires_at) < new Date();
|
||||
const exhausted = c.max_uses > 0 && c.used_count >= c.max_uses;
|
||||
return (
|
||||
<TableRow key={c.id} sx={{ opacity: expired || exhausted ? 0.5 : 1 }}>
|
||||
<TableCell>
|
||||
<Stack direction="row" spacing={0.5} alignItems="center">
|
||||
<Typography variant="body2" fontFamily="monospace">
|
||||
{c.code}
|
||||
</Typography>
|
||||
<Tooltip title={t("user.invitationCodeCopy")}>
|
||||
<IconButton size="small" onClick={() => handleCopy(c.code)}>
|
||||
<CopyOutlined fontSize="inherit" />
|
||||
</IconButton>
|
||||
</Tooltip>
|
||||
</Stack>
|
||||
</TableCell>
|
||||
<TableCell>{c.group_id > 0 ? c.group_id : t("user.invitationCodeDefaultGroup")}</TableCell>
|
||||
<TableCell>
|
||||
{c.used_count}/{c.max_uses > 0 ? c.max_uses : "∞"}
|
||||
</TableCell>
|
||||
<TableCell>{formatDate(c.expires_at)}</TableCell>
|
||||
<TableCell>{formatDate(c.created_at)}</TableCell>
|
||||
<TableCell align="right">
|
||||
<IconButton size="small" color="error" onClick={() => handleDelete(c.id)}>
|
||||
<Delete fontSize="small" />
|
||||
</IconButton>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
);
|
||||
})}
|
||||
{codes.length === 0 && !loading && (
|
||||
<TableRow>
|
||||
<TableCell colSpan={6} align="center">
|
||||
<Typography variant="body2" color="text.secondary" sx={{ py: 3 }}>
|
||||
{t("user.noInvitationCodes")}
|
||||
</Typography>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</TableContainer>
|
||||
{nextToken && (
|
||||
<Box sx={{ textAlign: "center" }}>
|
||||
<SecondaryButton size="small" onClick={() => fetchCodes(nextToken)} disabled={loading}>
|
||||
{t("user.loadMore")}
|
||||
</SecondaryButton>
|
||||
</Box>
|
||||
)}
|
||||
</Stack>
|
||||
</DialogContent>
|
||||
</DraggableDialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default InvitationCodeDialog;
|
||||
@ -0,0 +1,178 @@
|
||||
package inventory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"github.com/cloudreve/Cloudreve/v4/ent"
|
||||
"github.com/cloudreve/Cloudreve/v4/ent/invitationcode"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/conf"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/hashid"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvitationCodeNotFound = errors.New("invitation code not found")
|
||||
ErrInvitationCodeExpired = errors.New("invitation code expired")
|
||||
ErrInvitationCodeExhausted = errors.New("invitation code fully used")
|
||||
)
|
||||
|
||||
type (
|
||||
InvitationCodeClient interface {
|
||||
TxOperator
|
||||
// Create creates a new invitation code.
|
||||
Create(ctx context.Context, params *CreateInvitationCodeParams) (*ent.InvitationCode, error)
|
||||
// List returns a paginated list of invitation codes.
|
||||
List(ctx context.Context, args *ListInvitationCodeArgs) (*ListInvitationCodeResult, error)
|
||||
// Delete revokes an invitation code.
|
||||
Delete(ctx context.Context, id int) error
|
||||
// Consume validates the code and atomically increments its use count.
|
||||
// The returned record carries the group assignment for the new user.
|
||||
Consume(ctx context.Context, code string) (*ent.InvitationCode, error)
|
||||
}
|
||||
|
||||
CreateInvitationCodeParams struct {
|
||||
Code string
|
||||
GroupID int
|
||||
MaxUses int
|
||||
ExpiresAt *time.Time
|
||||
}
|
||||
|
||||
ListInvitationCodeArgs struct {
|
||||
*PaginationArgs
|
||||
Keyword string
|
||||
}
|
||||
|
||||
ListInvitationCodeResult struct {
|
||||
*PaginationResults
|
||||
Codes []*ent.InvitationCode
|
||||
}
|
||||
)
|
||||
|
||||
func NewInvitationCodeClient(client *ent.Client, dbType conf.DBType, hasher hashid.Encoder) InvitationCodeClient {
|
||||
return &invitationCodeClient{
|
||||
client: client,
|
||||
hasher: hasher,
|
||||
maxSQlParam: sqlParamLimit(dbType),
|
||||
}
|
||||
}
|
||||
|
||||
type invitationCodeClient struct {
|
||||
maxSQlParam int
|
||||
client *ent.Client
|
||||
hasher hashid.Encoder
|
||||
}
|
||||
|
||||
func (c *invitationCodeClient) SetClient(newClient *ent.Client) TxOperator {
|
||||
return &invitationCodeClient{client: newClient, hasher: c.hasher, maxSQlParam: c.maxSQlParam}
|
||||
}
|
||||
|
||||
func (c *invitationCodeClient) GetClient() *ent.Client {
|
||||
return c.client
|
||||
}
|
||||
|
||||
func (c *invitationCodeClient) Create(ctx context.Context, params *CreateInvitationCodeParams) (*ent.InvitationCode, error) {
|
||||
create := c.client.InvitationCode.Create().
|
||||
SetCode(params.Code).
|
||||
SetGroupID(params.GroupID).
|
||||
SetMaxUses(params.MaxUses)
|
||||
if params.ExpiresAt != nil {
|
||||
create.SetExpiresAt(*params.ExpiresAt)
|
||||
}
|
||||
|
||||
return create.Save(ctx)
|
||||
}
|
||||
|
||||
func (c *invitationCodeClient) Delete(ctx context.Context, id int) error {
|
||||
return c.client.InvitationCode.DeleteOneID(id).Exec(ctx)
|
||||
}
|
||||
|
||||
func (c *invitationCodeClient) Consume(ctx context.Context, code string) (*ent.InvitationCode, error) {
|
||||
record, err := c.client.InvitationCode.Query().
|
||||
Where(invitationcode.CodeEQ(code)).
|
||||
First(ctx)
|
||||
if err != nil {
|
||||
if ent.IsNotFound(err) {
|
||||
return nil, ErrInvitationCodeNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if record.ExpiresAt != nil && record.ExpiresAt.Before(time.Now()) {
|
||||
return nil, ErrInvitationCodeExpired
|
||||
}
|
||||
if record.MaxUses > 0 && record.UsedCount >= record.MaxUses {
|
||||
return nil, ErrInvitationCodeExhausted
|
||||
}
|
||||
|
||||
// Guarded increment keeps concurrent registrations honest: only one can win
|
||||
// the last remaining use.
|
||||
affected, err := c.client.InvitationCode.Update().
|
||||
Where(
|
||||
invitationcode.ID(record.ID),
|
||||
invitationcode.Or(
|
||||
invitationcode.ExpiresAtIsNil(),
|
||||
invitationcode.ExpiresAtGT(time.Now()),
|
||||
),
|
||||
invitationcode.Or(
|
||||
invitationcode.MaxUsesEQ(0),
|
||||
invitationcode.UsedCountLT(record.MaxUses),
|
||||
),
|
||||
).
|
||||
AddUsedCount(1).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if affected == 0 {
|
||||
return nil, ErrInvitationCodeExhausted
|
||||
}
|
||||
|
||||
return record, nil
|
||||
}
|
||||
|
||||
func (c *invitationCodeClient) List(ctx context.Context, args *ListInvitationCodeArgs) (*ListInvitationCodeResult, error) {
|
||||
query := c.client.InvitationCode.Query()
|
||||
if args.Keyword != "" {
|
||||
query.Where(invitationcode.CodeContainsFold(args.Keyword))
|
||||
}
|
||||
query.Order(invitationcode.ByID(sql.OrderDesc()))
|
||||
|
||||
pageSize := capPageSize(c.maxSQlParam, args.PageSize, 10)
|
||||
var pageToken *PageToken
|
||||
if args.PageToken != "" {
|
||||
var err error
|
||||
pageToken, err = pageTokenFromString(args.PageToken, c.hasher, hashid.InvitationCodeID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid page token %q: %w", args.PageToken, err)
|
||||
}
|
||||
query.Where(invitationcode.IDLT(pageToken.ID))
|
||||
}
|
||||
|
||||
query.Limit(pageSize + 1)
|
||||
codes, err := query.All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nextTokenStr := ""
|
||||
if len(codes) > pageSize {
|
||||
nextToken, err := (&PageToken{ID: codes[len(codes)-2].ID}).Encode(c.hasher, hashid.EncodeInvitationCodeID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to generate next page token: %w", err)
|
||||
}
|
||||
nextTokenStr = nextToken
|
||||
}
|
||||
|
||||
return &ListInvitationCodeResult{
|
||||
Codes: lo.Subset(codes, 0, uint(pageSize)),
|
||||
PaginationResults: &PaginationResults{
|
||||
PageSize: pageSize,
|
||||
NextPageToken: nextTokenStr,
|
||||
IsCursor: true,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@ -0,0 +1,107 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/cloudreve/Cloudreve/v4/application/dependency"
|
||||
"github.com/cloudreve/Cloudreve/v4/ent"
|
||||
"github.com/cloudreve/Cloudreve/v4/inventory"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/hashid"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/serializer"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
type (
|
||||
// InvitationCodeListService lists invitation codes for admins.
|
||||
InvitationCodeListService struct {
|
||||
Keyword string `form:"keyword" json:"keyword"`
|
||||
PageSize int `form:"page_size" json:"page_size" binding:"required,min=1"`
|
||||
PageToken string `form:"page_token" json:"page_token"`
|
||||
}
|
||||
InvitationCodeListParamCtx struct{}
|
||||
|
||||
// UpsertInvitationCodeService creates an invitation code. An empty Code
|
||||
// auto-generates one.
|
||||
UpsertInvitationCodeService struct {
|
||||
Code string `json:"code" binding:"omitempty,min=4,max=64"`
|
||||
GroupID int `json:"group_id" binding:"omitempty,min=0"`
|
||||
MaxUses int `json:"max_uses" binding:"omitempty,min=0"`
|
||||
ExpiresAt *time.Time `json:"expires_at"`
|
||||
}
|
||||
UpsertInvitationCodeParamCtx struct{}
|
||||
|
||||
// SingleInvitationCodeService targets one code by ID.
|
||||
SingleInvitationCodeService struct {
|
||||
ID int `uri:"id" json:"id" binding:"required"`
|
||||
}
|
||||
SingleInvitationCodeParamCtx struct{}
|
||||
|
||||
InvitationCodeResponse struct {
|
||||
*ent.InvitationCode
|
||||
HashID string `json:"hash_id"`
|
||||
}
|
||||
)
|
||||
|
||||
func (service *InvitationCodeListService) List(c *gin.Context) (*ListInvitationCodeResult, error) {
|
||||
dep := dependency.FromContext(c)
|
||||
res, err := dep.InvitationCodeClient().List(c, &inventory.ListInvitationCodeArgs{
|
||||
PaginationArgs: &inventory.PaginationArgs{
|
||||
PageSize: service.PageSize,
|
||||
PageToken: service.PageToken,
|
||||
},
|
||||
Keyword: service.Keyword,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, serializer.NewError(serializer.CodeDBError, "Failed to list invitation codes", err)
|
||||
}
|
||||
|
||||
return &ListInvitationCodeResult{
|
||||
Pagination: res.PaginationResults,
|
||||
Codes: lo.Map(res.Codes, func(code *ent.InvitationCode, _ int) *InvitationCodeResponse {
|
||||
return &InvitationCodeResponse{
|
||||
InvitationCode: code,
|
||||
HashID: hashid.EncodeInvitationCodeID(dep.HashIDEncoder(), code.ID),
|
||||
}
|
||||
}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (service *UpsertInvitationCodeService) Create(c *gin.Context) (*InvitationCodeResponse, error) {
|
||||
dep := dependency.FromContext(c)
|
||||
|
||||
code := service.Code
|
||||
if code == "" {
|
||||
code = util.RandString(16, util.RandomLowerCases)
|
||||
}
|
||||
|
||||
if service.GroupID > 0 {
|
||||
if _, err := dep.GroupClient().GetByID(c, service.GroupID); err != nil {
|
||||
return nil, serializer.NewError(serializer.CodeParamErr, "Invalid group", err)
|
||||
}
|
||||
}
|
||||
|
||||
record, err := dep.InvitationCodeClient().Create(c, &inventory.CreateInvitationCodeParams{
|
||||
Code: code,
|
||||
GroupID: service.GroupID,
|
||||
MaxUses: service.MaxUses,
|
||||
ExpiresAt: service.ExpiresAt,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, serializer.NewError(serializer.CodeDBError, "Failed to create invitation code", err)
|
||||
}
|
||||
|
||||
return &InvitationCodeResponse{
|
||||
InvitationCode: record,
|
||||
HashID: hashid.EncodeInvitationCodeID(dep.HashIDEncoder(), record.ID),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (service *SingleInvitationCodeService) Delete(c *gin.Context) error {
|
||||
dep := dependency.FromContext(c)
|
||||
if err := dep.InvitationCodeClient().Delete(c, service.ID); err != nil {
|
||||
return serializer.NewError(serializer.CodeDBError, "Failed to delete invitation code", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Reference in new issue