Skip to content

Commit

Permalink
If there are no owner yet, allow to use default account
Browse files Browse the repository at this point in the history
  • Loading branch information
RadhiFadlillah committed Aug 22, 2019
1 parent 6b39742 commit c22f904
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
8 changes: 7 additions & 1 deletion internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions internal/database/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
12 changes: 8 additions & 4 deletions internal/database/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
12 changes: 8 additions & 4 deletions internal/webserver/handler-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit c22f904

Please sign in to comment.