mirror of
https://github.com/rjNemo/go-wiki
synced 2026-06-12 13:36:38 +00:00
userStore find buggy
This commit is contained in:
parent
9bef392094
commit
db947b17f3
5 changed files with 36 additions and 6 deletions
1
TODO.md
1
TODO.md
|
|
@ -2,3 +2,4 @@
|
||||||
|
|
||||||
- [x] The mail field is case sensitive. Convert mail to lowercase before they are sent to InsertUser
|
- [x] The mail field is case sensitive. Convert mail to lowercase before they are sent to InsertUser
|
||||||
- [] All errors are fatal. Provide proper error handling.
|
- [] All errors are fatal. Provide proper error handling.
|
||||||
|
- [] Refactor UserStore code
|
||||||
|
|
|
||||||
|
|
@ -33,9 +33,10 @@ func UsePSQL() {
|
||||||
// log.Print(u)
|
// log.Print(u)
|
||||||
u1 := model.NewUser(3, 20, "paul", "newman", "PdsNz@FDKML.COM")
|
u1 := model.NewUser(3, 20, "paul", "newman", "PdsNz@FDKML.COM")
|
||||||
store.Update(16, u1)
|
store.Update(16, u1)
|
||||||
log.Print(store.Get(1))
|
log.Println(store.Get(1))
|
||||||
// store.Delete(8)
|
// store.Delete(8)
|
||||||
log.Print(store.GetAll())
|
log.Println(store.GetAll())
|
||||||
|
log.Println(store.Find("first_name", "John"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func sqlExec(db *sql.DB, s string) {
|
func sqlExec(db *sql.DB, s string) {
|
||||||
|
|
@ -58,6 +59,6 @@ type Store interface {
|
||||||
Get(id int) (model.User, error)
|
Get(id int) (model.User, error)
|
||||||
GetAll(id int) ([]model.User, error)
|
GetAll(id int) ([]model.User, error)
|
||||||
Delete(id int)
|
Delete(id int)
|
||||||
// Find(id int)
|
Find(ex string)
|
||||||
Update(id int, i interface{})
|
Update(id int, i interface{})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,12 +14,18 @@ const (
|
||||||
|
|
||||||
// QueryGet is the SQL command used to retrieve a user from the table
|
// QueryGet is the SQL command used to retrieve a user from the table
|
||||||
QueryGet = `
|
QueryGet = `
|
||||||
SELECT * FROM users WHERE id=$1;
|
SELECT * FROM users WHERE id = $1;
|
||||||
`
|
`
|
||||||
|
|
||||||
// QueryGetAll is the SQL command used to retrieve all users from the database.
|
// QueryGetAll is the SQL command used to retrieve all users from the database.
|
||||||
QueryGetAll = `
|
QueryGetAll = `
|
||||||
SELECT * FROM users;
|
SELECT * FROM users ORDER BY id;
|
||||||
|
`
|
||||||
|
|
||||||
|
// QueryFind is the SQL command used to retrieve all users from the table
|
||||||
|
// which satisfy the specified condition
|
||||||
|
QueryFind = `
|
||||||
|
SELECT * FROM users WHERE $1 = $2;
|
||||||
`
|
`
|
||||||
|
|
||||||
// QueryInsert is the SQL command used to insert a user in the table.
|
// QueryInsert is the SQL command used to insert a user in the table.
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,28 @@ func (us UserStore) GetAll() ([]model.User, error) {
|
||||||
return users, nil
|
return users, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Find retrieves a user from the database using an expression
|
||||||
|
func (us UserStore) Find(k interface{}, v interface{}) ([]model.User, error) {
|
||||||
|
var id, age int
|
||||||
|
var firstName, lastName, email string
|
||||||
|
|
||||||
|
rows, err := us.db.Query(QueryFind, k, v)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var users []model.User
|
||||||
|
for rows.Next() {
|
||||||
|
err := rows.Scan(&id, &age, &firstName, &lastName, &email)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err) // too severe
|
||||||
|
}
|
||||||
|
u := model.NewUser(id, age, firstName, lastName, email)
|
||||||
|
users = append(users, u)
|
||||||
|
}
|
||||||
|
return users, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Get retrieves the user identified by 'id' from database
|
// Get retrieves the user identified by 'id' from database
|
||||||
func (us UserStore) Get(uid int) (model.User, error) {
|
func (us UserStore) Get(uid int) (model.User, error) {
|
||||||
var id, age int
|
var id, age int
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ func TestUser() User {
|
||||||
id: 42,
|
id: 42,
|
||||||
firstName: "John",
|
firstName: "John",
|
||||||
lastName: "Doe",
|
lastName: "Doe",
|
||||||
email: "jddddSZ@mail.com",
|
email: "jd@mail.com",
|
||||||
age: 42,
|
age: 42,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue