storj/satellite/satellitedb/consoledb.go

104 lines
2.4 KiB
Go
Raw Normal View History

2019-01-24 16:26:36 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"context"
2019-01-08 14:05:14 +00:00
"github.com/zeebo/errs"
"storj.io/storj/satellite/console"
dbx "storj.io/storj/satellite/satellitedb/dbx"
)
// ensures that ConsoleDB implements console.DB.
var _ console.DB = (*ConsoleDB)(nil)
// ConsoleDB contains access to different satellite databases.
type ConsoleDB struct {
db *dbx.DB
tx *dbx.Tx
methods dbx.Methods
}
// Users is getter a for Users repository.
func (db *ConsoleDB) Users() console.Users {
return &users{db.methods}
}
// Projects is a getter for Projects repository.
func (db *ConsoleDB) Projects() console.Projects {
return &projects{db.methods}
}
// ProjectMembers is a getter for ProjectMembers repository.
func (db *ConsoleDB) ProjectMembers() console.ProjectMembers {
return &projectMembers{db.methods, db.db}
}
// APIKeys is a getter for APIKeys repository.
func (db *ConsoleDB) APIKeys() console.APIKeys {
return &apikeys{db.methods, db.db}
}
// RegistrationTokens is a getter for RegistrationTokens repository.
func (db *ConsoleDB) RegistrationTokens() console.RegistrationTokens {
return &registrationTokens{db.methods}
}
// ResetPasswordTokens is a getter for ResetPasswordTokens repository.
func (db *ConsoleDB) ResetPasswordTokens() console.ResetPasswordTokens {
return &resetPasswordTokens{db.methods}
}
// UserCredits is a getter for console.UserCredits repository.
func (db *ConsoleDB) UserCredits() console.UserCredits {
satellite/satellitedb: add updateEarnedCredits method for user_credits table (#2609) * parent 13dd501042d0fa6eb0142b6f737704985c17f5bc author Yingrong Zhao <yingrong.zhao@gmail.com> 1563560530 -0400 committer Yingrong Zhao <yingrong.zhao@gmail.com> 1563581673 -0400 parent 13dd501042d0fa6eb0142b6f737704985c17f5bc author Yingrong Zhao <yingrong.zhao@gmail.com> 1563560530 -0400 committer Yingrong Zhao <yingrong.zhao@gmail.com> 1563581428 -0400 satellite/console: add referral link logic (#2576) * setup referral route * referredBy * add user id * modify user query * separate optional field from userInfo * get current reward on init of satellite gui * remove unsed code * fix format * only apply 0 credit on registration * only pass required information for rewards * fix time parsing * fix test and linter * rename method * add todo * remove user referral logic * add null check and fix format * get current offer * remove partnerID on CreateUser struct * fix storj-sim user creation * only redeem credit when there's an offer * fix default offer configuration * fix migration * Add helper function for get correct credit duration * add comment * only store userid into user_credit table * add check for partner id to set correct offer type * change free credit to use invitee credits * remove unecessary code * add credit update in activateAccount * remove unused code * fix format * close reader and fix front-end build * move create credit logic into CreateUser method * when there's no offer set, user flow shouldn't be interrupted by referral program * add appropriate error messages * remove unused code * add comment * add error class for no current offer error * add error class for credits update * add comment for migration * only log secret when it's in debug level * fix typo * add testdata
2019-07-30 14:21:00 +01:00
return &usercredits{db.db, db.tx}
}
// BeginTx is a method for opening transaction.
func (db *ConsoleDB) BeginTx(ctx context.Context) (console.DBTx, error) {
if db.db == nil {
return nil, errs.New("DB is not initialized!")
}
tx, err := db.db.Open(ctx)
if err != nil {
return nil, err
}
return &DBTx{
ConsoleDB: &ConsoleDB{
satellite/satellitedb: add updateEarnedCredits method for user_credits table (#2609) * parent 13dd501042d0fa6eb0142b6f737704985c17f5bc author Yingrong Zhao <yingrong.zhao@gmail.com> 1563560530 -0400 committer Yingrong Zhao <yingrong.zhao@gmail.com> 1563581673 -0400 parent 13dd501042d0fa6eb0142b6f737704985c17f5bc author Yingrong Zhao <yingrong.zhao@gmail.com> 1563560530 -0400 committer Yingrong Zhao <yingrong.zhao@gmail.com> 1563581428 -0400 satellite/console: add referral link logic (#2576) * setup referral route * referredBy * add user id * modify user query * separate optional field from userInfo * get current reward on init of satellite gui * remove unsed code * fix format * only apply 0 credit on registration * only pass required information for rewards * fix time parsing * fix test and linter * rename method * add todo * remove user referral logic * add null check and fix format * get current offer * remove partnerID on CreateUser struct * fix storj-sim user creation * only redeem credit when there's an offer * fix default offer configuration * fix migration * Add helper function for get correct credit duration * add comment * only store userid into user_credit table * add check for partner id to set correct offer type * change free credit to use invitee credits * remove unecessary code * add credit update in activateAccount * remove unused code * fix format * close reader and fix front-end build * move create credit logic into CreateUser method * when there's no offer set, user flow shouldn't be interrupted by referral program * add appropriate error messages * remove unused code * add comment * add error class for no current offer error * add error class for credits update * add comment for migration * only log secret when it's in debug level * fix typo * add testdata
2019-07-30 14:21:00 +01:00
// Need to expose dbx.DB for when database methods need access to check database driver type
db: db.db,
tx: tx,
methods: tx,
},
}, nil
}
// DBTx extends Database with transaction scope.
type DBTx struct {
*ConsoleDB
}
// Commit is a method for committing and closing transaction.
func (db *DBTx) Commit() error {
if db.tx == nil {
return errs.New("begin transaction before commit it!")
}
return db.tx.Commit()
}
// Rollback is a method for rollback and closing transaction.
func (db *DBTx) Rollback() error {
if db.tx == nil {
return errs.New("begin transaction before rollback it!")
}
return db.tx.Rollback()
}