update repo methods without nil pointers
This commit is contained in:
parent
8fb3a4a1d3
commit
dea8b7c136
@ -30,9 +30,7 @@ func NewAccountRepository(deps Deps) *AccountRepository {
|
||||
|
||||
// test +
|
||||
func (r *AccountRepository) GetAccountByID(ctx context.Context, userID string) (model.Account, error) {
|
||||
userIDSql := sql.NullString{String: userID, Valid: userID != ""}
|
||||
|
||||
accountRows, err := r.queries.GetAccountWithPrivileges(ctx, userIDSql)
|
||||
accountRows, err := r.queries.GetAccountWithPrivileges(ctx, userID)
|
||||
if err != nil {
|
||||
return model.Account{}, err
|
||||
}
|
||||
@ -43,9 +41,9 @@ func (r *AccountRepository) GetAccountByID(ctx context.Context, userID string) (
|
||||
for _, row := range accountRows {
|
||||
if account.ID == "" {
|
||||
account.ID = row.ID.String()
|
||||
account.UserID = row.UserID.String
|
||||
account.CreatedAt = row.CreatedAt.Time
|
||||
account.Deleted = row.Deleted.Bool
|
||||
account.UserID = row.UserID
|
||||
account.CreatedAt = row.CreatedAt
|
||||
account.Deleted = row.Deleted
|
||||
}
|
||||
|
||||
if row.PrivilegeID != 0 {
|
||||
@ -54,7 +52,7 @@ func (r *AccountRepository) GetAccountByID(ctx context.Context, userID string) (
|
||||
PrivilegeID: row.Privilegeid,
|
||||
PrivilegeName: row.PrivilegeName,
|
||||
Amount: uint64(row.Amount),
|
||||
CreatedAt: row.PrivilegeCreatedAt.Time,
|
||||
CreatedAt: row.PrivilegeCreatedAt,
|
||||
}
|
||||
privileges[privilege.PrivilegeID] = privilege
|
||||
}
|
||||
@ -70,9 +68,8 @@ func (r *AccountRepository) GetAccountByID(ctx context.Context, userID string) (
|
||||
|
||||
// test +
|
||||
func (r *AccountRepository) GetPrivilegesByAccountID(ctx context.Context, userID string) ([]model.ShortPrivilege, error) {
|
||||
userIDSql := sql.NullString{String: userID, Valid: userID != ""}
|
||||
|
||||
privilegeRows, err := r.queries.GetPrivilegesByAccountIDWC(ctx, userIDSql)
|
||||
privilegeRows, err := r.queries.GetPrivilegesByAccountIDWC(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -82,10 +79,10 @@ func (r *AccountRepository) GetPrivilegesByAccountID(ctx context.Context, userID
|
||||
for _, row := range privilegeRows {
|
||||
privilege := model.ShortPrivilege{
|
||||
ID: fmt.Sprintf("%d", row.ID),
|
||||
PrivilegeID: row.Privilegeid.String,
|
||||
PrivilegeName: row.PrivilegeName.String,
|
||||
Amount: uint64(row.Amount.Int32),
|
||||
CreatedAt: row.CreatedAt.Time,
|
||||
PrivilegeID: row.Privilegeid,
|
||||
PrivilegeName: row.PrivilegeName,
|
||||
Amount: uint64(row.Amount),
|
||||
CreatedAt: row.CreatedAt,
|
||||
}
|
||||
privileges = append(privileges, privilege)
|
||||
}
|
||||
@ -98,9 +95,9 @@ func (r *AccountRepository) CreateAccount(ctx context.Context, data *model.Accou
|
||||
data.ID = uuid.NewString()
|
||||
row, err := r.queries.CreateAccount(ctx, sqlcgen.CreateAccountParams{
|
||||
ID: uuid.MustParse(data.ID),
|
||||
UserID: sql.NullString{String: data.UserID, Valid: data.UserID != ""},
|
||||
CreatedAt: sql.NullTime{Time: data.CreatedAt, Valid: !data.CreatedAt.IsZero()},
|
||||
Deleted: sql.NullBool{Bool: data.Deleted, Valid: true},
|
||||
UserID: data.UserID,
|
||||
CreatedAt: data.CreatedAt,
|
||||
Deleted: data.Deleted,
|
||||
})
|
||||
if err != nil {
|
||||
return model.Account{}, fmt.Errorf("failed to create account: %w", err)
|
||||
@ -108,16 +105,16 @@ func (r *AccountRepository) CreateAccount(ctx context.Context, data *model.Accou
|
||||
|
||||
createdAccount := model.Account{
|
||||
ID: row.ID.String(),
|
||||
UserID: row.UserID.String,
|
||||
UserID: row.UserID,
|
||||
}
|
||||
|
||||
for _, privilege := range data.Privileges {
|
||||
err := r.queries.InsertPrivilege(ctx, sqlcgen.InsertPrivilegeParams{
|
||||
Privilegeid: sql.NullString{String: privilege.PrivilegeID, Valid: privilege.PrivilegeID != ""},
|
||||
AccountID: uuid.NullUUID{UUID: uuid.MustParse(data.ID), Valid: true},
|
||||
PrivilegeName: sql.NullString{String: privilege.PrivilegeName, Valid: privilege.PrivilegeName != ""},
|
||||
Amount: sql.NullInt32{Int32: int32(privilege.Amount), Valid: true},
|
||||
CreatedAt: sql.NullTime{Time: privilege.CreatedAt, Valid: !privilege.CreatedAt.IsZero()},
|
||||
Privilegeid: privilege.PrivilegeID,
|
||||
AccountID: uuid.MustParse(data.ID),
|
||||
PrivilegeName: privilege.PrivilegeName,
|
||||
Amount: int32(privilege.Amount),
|
||||
CreatedAt: privilege.CreatedAt,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@ -140,7 +137,7 @@ func (r *AccountRepository) DeleteAccount(ctx context.Context, accountID string)
|
||||
return err
|
||||
}
|
||||
|
||||
err = r.queries.DeletePrivilegeByAccID(ctx, uuid.NullUUID{UUID: uuid.MustParse(accountID), Valid: true})
|
||||
err = r.queries.DeletePrivilegeByAccID(ctx, uuid.MustParse(accountID))
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
@ -164,9 +161,9 @@ func (r *AccountRepository) GetAccounts(ctx context.Context, limit uint64, offse
|
||||
for _, row := range rows {
|
||||
account := model.Account{
|
||||
ID: row.ID.String(),
|
||||
UserID: row.UserID.String,
|
||||
CreatedAt: row.CreatedAt.Time,
|
||||
Deleted: row.Deleted.Bool,
|
||||
UserID: row.UserID,
|
||||
CreatedAt: row.CreatedAt,
|
||||
Deleted: row.Deleted,
|
||||
}
|
||||
|
||||
accounts = append(accounts, account)
|
||||
@ -178,10 +175,10 @@ func (r *AccountRepository) GetAccounts(ctx context.Context, limit uint64, offse
|
||||
// test +
|
||||
func (r *AccountRepository) UpdatePrivilege(ctx context.Context, privilege *model.ShortPrivilege, accountID string) error {
|
||||
err := r.queries.UpdatePrivilege(ctx, sqlcgen.UpdatePrivilegeParams{
|
||||
Amount: sql.NullInt32{Int32: int32(privilege.Amount), Valid: true},
|
||||
CreatedAt: sql.NullTime{Time: privilege.CreatedAt, Valid: !privilege.CreatedAt.IsZero()},
|
||||
AccountID: uuid.NullUUID{UUID: uuid.MustParse(accountID), Valid: true},
|
||||
Privilegeid: sql.NullString{String: privilege.PrivilegeID, Valid: privilege.PrivilegeID != ""},
|
||||
Amount: int32(privilege.Amount),
|
||||
CreatedAt: privilege.CreatedAt,
|
||||
AccountID: uuid.MustParse(accountID),
|
||||
Privilegeid: privilege.PrivilegeID,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@ -194,11 +191,11 @@ func (r *AccountRepository) UpdatePrivilege(ctx context.Context, privilege *mode
|
||||
// test +
|
||||
func (r *AccountRepository) InsertPrivilege(ctx context.Context, privilege *model.ShortPrivilege, accountID string) error {
|
||||
err := r.queries.InsertPrivilege(ctx, sqlcgen.InsertPrivilegeParams{
|
||||
Amount: sql.NullInt32{Int32: int32(privilege.Amount), Valid: true},
|
||||
CreatedAt: sql.NullTime{Time: privilege.CreatedAt, Valid: !privilege.CreatedAt.IsZero()},
|
||||
AccountID: uuid.NullUUID{UUID: uuid.MustParse(accountID), Valid: true},
|
||||
Privilegeid: sql.NullString{String: privilege.PrivilegeID, Valid: privilege.PrivilegeID != ""},
|
||||
PrivilegeName: sql.NullString{String: privilege.PrivilegeName, Valid: privilege.PrivilegeName != ""},
|
||||
Amount: int32(privilege.Amount),
|
||||
CreatedAt: privilege.CreatedAt,
|
||||
AccountID: uuid.MustParse(accountID),
|
||||
Privilegeid: privilege.PrivilegeID,
|
||||
PrivilegeName: privilege.PrivilegeName,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@ -210,7 +207,7 @@ func (r *AccountRepository) InsertPrivilege(ctx context.Context, privilege *mode
|
||||
|
||||
// test +
|
||||
func (r *AccountRepository) GetExpired(ctx context.Context, privilegeID string) ([]model.ExpiredPrivileges, error) {
|
||||
rows, err := r.queries.GetExpiredDayPrivilege(ctx, sql.NullString{String: privilegeID, Valid: privilegeID != ""})
|
||||
rows, err := r.queries.GetExpiredDayPrivilege(ctx, privilegeID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -220,13 +217,13 @@ func (r *AccountRepository) GetExpired(ctx context.Context, privilegeID string)
|
||||
for _, row := range rows {
|
||||
privilege := model.ShortPrivilege{
|
||||
ID: fmt.Sprintf("%d", row.ID),
|
||||
PrivilegeID: row.Privilegeid.String,
|
||||
PrivilegeName: row.PrivilegeName.String,
|
||||
Amount: uint64(row.Amount.Int32),
|
||||
CreatedAt: row.CreatedAt.Time,
|
||||
PrivilegeID: row.Privilegeid,
|
||||
PrivilegeName: row.PrivilegeName,
|
||||
Amount: uint64(row.Amount),
|
||||
CreatedAt: row.CreatedAt,
|
||||
}
|
||||
expiredRecords = append(expiredRecords, model.ExpiredPrivileges{
|
||||
UserID: row.UserID.String,
|
||||
UserID: row.UserID,
|
||||
Privilege: privilege,
|
||||
})
|
||||
|
||||
@ -236,7 +233,7 @@ func (r *AccountRepository) GetExpired(ctx context.Context, privilegeID string)
|
||||
}
|
||||
|
||||
func (r *AccountRepository) GetExpiredCount(ctx context.Context, privilegeID string) ([]model.ExpiredPrivileges, error) {
|
||||
rows, err := r.queries.GetExpiredCountPrivilege(ctx, sql.NullString{String: privilegeID, Valid: privilegeID != ""})
|
||||
rows, err := r.queries.GetExpiredCountPrivilege(ctx, privilegeID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -246,13 +243,13 @@ func (r *AccountRepository) GetExpiredCount(ctx context.Context, privilegeID str
|
||||
for _, row := range rows {
|
||||
privilege := model.ShortPrivilege{
|
||||
ID: fmt.Sprintf("%d", row.ID),
|
||||
PrivilegeID: row.Privilegeid.String,
|
||||
PrivilegeName: row.PrivilegeName.String,
|
||||
Amount: uint64(row.Amount.Int32),
|
||||
CreatedAt: row.CreatedAt.Time,
|
||||
PrivilegeID: row.Privilegeid,
|
||||
PrivilegeName: row.PrivilegeName,
|
||||
Amount: uint64(row.Amount),
|
||||
CreatedAt: row.CreatedAt,
|
||||
}
|
||||
expiredRecords = append(expiredRecords, model.ExpiredPrivileges{
|
||||
UserID: row.UserID.String,
|
||||
UserID: row.UserID,
|
||||
Privilege: privilege,
|
||||
})
|
||||
|
||||
@ -263,9 +260,9 @@ func (r *AccountRepository) GetExpiredCount(ctx context.Context, privilegeID str
|
||||
|
||||
func (r *AccountRepository) CheckAndAddDefault(ctx context.Context, amount uint64, privilegeID string, zeroAmount uint64) error {
|
||||
err := r.queries.CheckAndAddDefault(ctx, sqlcgen.CheckAndAddDefaultParams{
|
||||
Amount: sql.NullInt32{Int32: int32(amount), Valid: true},
|
||||
PrivilegeName: sql.NullString{String: privilegeID, Valid: privilegeID != ""},
|
||||
Amount_2: sql.NullInt32{Int32: int32(zeroAmount), Valid: true},
|
||||
Amount: int32(amount),
|
||||
PrivilegeName: privilegeID,
|
||||
Amount_2: int32(zeroAmount),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@ -293,7 +290,7 @@ func (r *AccountRepository) UpdatePrivilegeAmount(ctx context.Context, ID string
|
||||
}
|
||||
|
||||
err = r.queries.UpdatePrivilegeAmount(ctx, sqlcgen.UpdatePrivilegeAmountParams{
|
||||
Amount: sql.NullInt32{Int32: int32(newAmount), Valid: true},
|
||||
Amount: int32(newAmount),
|
||||
ID: int32(intID),
|
||||
})
|
||||
|
||||
@ -309,21 +306,21 @@ func (r *AccountRepository) GetAccAndPrivilegeByEmail(ctx context.Context, email
|
||||
var account model.Account
|
||||
var privileges []model.ShortPrivilege
|
||||
|
||||
row, err := r.queries.GetAccAndPrivilegeByEmail(ctx, sql.NullString{String: email, Valid: true})
|
||||
row, err := r.queries.GetAccAndPrivilegeByEmail(ctx, email)
|
||||
if err != nil {
|
||||
return account, privileges, err
|
||||
}
|
||||
|
||||
account.ID = row.ID.String()
|
||||
account.UserID = row.UserID.String
|
||||
account.CreatedAt = row.CreatedAt.Time
|
||||
account.UserID = row.UserID
|
||||
account.CreatedAt = row.CreatedAt
|
||||
|
||||
if row.ID_2 != 0 {
|
||||
privilege := model.ShortPrivilege{
|
||||
ID: fmt.Sprint(row.ID_2),
|
||||
PrivilegeID: row.Privilegeid,
|
||||
Amount: uint64(row.Amount),
|
||||
CreatedAt: row.CreatedAt_2.Time,
|
||||
CreatedAt: row.CreatedAt_2,
|
||||
}
|
||||
privileges = append(privileges, privilege)
|
||||
}
|
||||
@ -349,8 +346,8 @@ func (r *AccountRepository) GetQidOwner(ctx context.Context, qId string) (string
|
||||
|
||||
func (r *AccountRepository) ManualDone(ctx context.Context, userID string) error {
|
||||
_, err := r.queries.DecrementManual(ctx, sqlcgen.DecrementManualParams{
|
||||
UserID: sql.NullString{String: userID, Valid: true},
|
||||
Privilegeid: sql.NullString{String: "quizManual", Valid: true},
|
||||
UserID: userID,
|
||||
Privilegeid: "quizManual",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user