If there are no owner yet, allow to use default account

This commit is contained in:
Radhi Fadlillah 2019-08-22 14:34:36 +07:00
parent 6b39742a35
commit c22f904bdd
4 changed files with 31 additions and 13 deletions

View file

@ -30,6 +30,12 @@ type GetBookmarksOptions struct {
Offset int
}
// GetAccountsOptions is options for fetching accounts from database.
type GetAccountsOptions struct {
Keyword string
Owner bool
}
// DB is interface for accessing and manipulating data in database.
type DB interface {
// SaveBookmarks saves bookmarks data to database.
@ -51,7 +57,7 @@ type DB interface {
SaveAccount(model.Account) error
// GetAccounts fetch list of account (without its password) with matching keyword.
GetAccounts(keyword string) ([]model.Account, error)
GetAccounts(opts GetAccountsOptions) ([]model.Account, error)
// GetAccount fetch account with matching username.
GetAccount(username string) (model.Account, bool)

View file

@ -528,15 +528,19 @@ func (db *MySQLDatabase) SaveAccount(account model.Account) (err error) {
return err
}
// GetAccounts fetch list of account (without its password) with matching keyword.
func (db *MySQLDatabase) GetAccounts(keyword string) ([]model.Account, error) {
// GetAccounts fetch list of account (without its password) based on submitted options.
func (db *MySQLDatabase) GetAccounts(opts GetAccountsOptions) ([]model.Account, error) {
// Create query
args := []interface{}{}
query := `SELECT id, username, owner FROM account WHERE 1`
if keyword != "" {
if opts.Keyword != "" {
query += " AND username LIKE ?"
args = append(args, "%"+keyword+"%")
args = append(args, "%"+opts.Keyword+"%")
}
if opts.Owner {
query += " AND owner = 1"
}
query += ` ORDER BY username`

View file

@ -543,15 +543,19 @@ func (db *SQLiteDatabase) SaveAccount(account model.Account) (err error) {
return err
}
// GetAccounts fetch list of account (without its password) with matching keyword.
func (db *SQLiteDatabase) GetAccounts(keyword string) ([]model.Account, error) {
// GetAccounts fetch list of account (without its password) based on submitted options.
func (db *SQLiteDatabase) GetAccounts(opts GetAccountsOptions) ([]model.Account, error) {
// Create query
args := []interface{}{}
query := `SELECT id, username, owner FROM account WHERE 1`
if keyword != "" {
if opts.Keyword != "" {
query += " AND username LIKE ?"
args = append(args, "%"+keyword+"%")
args = append(args, "%"+opts.Keyword+"%")
}
if opts.Owner {
query += " AND owner = 1"
}
query += ` ORDER BY username`

View file

@ -77,9 +77,13 @@ func (h *handler) apiLogin(w http.ResponseWriter, r *http.Request, ps httprouter
checkError(err)
}
// Check if user's database is empty.
// If database still empty, and user uses default account, let him in.
accounts, err := h.DB.GetAccounts("")
// Check if user's database is empty or there are no owner.
// If yes, and user uses default account, let him in.
searchOptions := database.GetAccountsOptions{
Owner: true,
}
accounts, err := h.DB.GetAccounts(searchOptions)
checkError(err)
if len(accounts) == 0 && request.Username == "shiori" && request.Password == "gopher" {
@ -744,7 +748,7 @@ func (h *handler) apiGetAccounts(w http.ResponseWriter, r *http.Request, ps http
checkError(err)
// Get list of usernames from database
accounts, err := h.DB.GetAccounts("")
accounts, err := h.DB.GetAccounts(database.GetAccountsOptions{})
checkError(err)
w.Header().Set("Content-Type", "application/json")