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