1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package admin
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
type Store struct {
database *pgxpool.Pool
}
func NewStore(database *pgxpool.Pool) *Store {
return &Store{
database,
}
}
func (store Store) Find(requestAdmin Admin) (Admin, error) {
adminRows, err := store.database.Query(context.Background(),
"select * from admin")
if err != nil {
return Admin{}, err
}
defer adminRows.Close()
admin, found := createAdmin(requestAdmin, adminRows)
if found {
return admin, nil
}
return Admin{}, errors.New("invalid username or password")
}
func createAdmin(requestAdmin Admin, adminRows pgx.Rows) (Admin, bool) {
var databaseAdmin Admin
for adminRows.Next() {
err := adminRows.Scan(&databaseAdmin.Username, &databaseAdmin.Password)
if err != nil {
return Admin{}, false
}
if databaseAdmin.Username == requestAdmin.Username &&
databaseAdmin.Password == requestAdmin.Password {
return databaseAdmin, true
}
}
return Admin{}, false
}
|