mirror of
https://github.com/rjNemo/go-wiki
synced 2026-06-12 13:36:38 +00:00
views package, add, update in userStore, user model rules, add todo.md
This commit is contained in:
parent
75c3a7fd20
commit
5e84fd34fc
3 changed files with 25 additions and 12 deletions
10
data/psql.go
10
data/psql.go
|
|
@ -34,6 +34,7 @@ func UsePSQL() {
|
||||||
u1 := model.NewUser(3, 19, "paul", "newman", "PdsN@FDKML.COM")
|
u1 := model.NewUser(3, 19, "paul", "newman", "PdsN@FDKML.COM")
|
||||||
store.Update(16, u1)
|
store.Update(16, u1)
|
||||||
log.Print(store.Get(1))
|
log.Print(store.Get(1))
|
||||||
|
store.Delete(8)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sqlExec(db *sql.DB, s string) {
|
func sqlExec(db *sql.DB, s string) {
|
||||||
|
|
@ -51,10 +52,11 @@ func createUserTable(db *sql.DB) {
|
||||||
|
|
||||||
// Store interface defines the methods any store must satisfy
|
// Store interface defines the methods any store must satisfy
|
||||||
type Store interface {
|
type Store interface {
|
||||||
CreateTable()
|
// CreateTable()
|
||||||
Add(i interface{})
|
Add(i interface{})
|
||||||
Get(id int) model.User
|
Get(id int) (model.User, error)
|
||||||
Find(id int)
|
GetAll(id int) ([]model.User, error)
|
||||||
Update(id int, i interface{})
|
|
||||||
Delete(id int)
|
Delete(id int)
|
||||||
|
// Find(id int)
|
||||||
|
Update(id int, i interface{})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,12 @@ 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 * id 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 = `
|
||||||
|
SELECT * FROM users;
|
||||||
`
|
`
|
||||||
|
|
||||||
// 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.
|
||||||
|
|
|
||||||
|
|
@ -17,18 +17,24 @@ func NewUserStore(db *sql.DB) UserStore {
|
||||||
return UserStore{db: db}
|
return UserStore{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAll retrieves all the users from the database.
|
||||||
|
func (us UserStore) GetAll() ([]model.User, error) {}
|
||||||
|
|
||||||
// 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 {
|
func (us UserStore) Get(uid int) (model.User, error) {
|
||||||
var id, age int
|
var id, age int
|
||||||
var firstName,lastName,email string
|
var firstName, lastName, email string
|
||||||
|
|
||||||
row := us.db.QueryRow(QueryGet, uid)
|
row := us.db.QueryRow(QueryGet, uid)
|
||||||
switch err:=row.Scan(&id,&age,&firstName,&lastName,&email); err {
|
switch err := row.Scan(&id, &age, &firstName, &lastName, &email); err {
|
||||||
case sql.ErrNoRows:
|
case sql.ErrNoRows:
|
||||||
log.Println("No entry returned")
|
log.Println("No entry returned")
|
||||||
|
return model.User{}, err
|
||||||
case nil:
|
case nil:
|
||||||
return model.NewUser(id, age,firstName,lastName,email)
|
return model.NewUser(id, age, firstName, lastName, email), nil
|
||||||
default:
|
default:
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
return model.User{}, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -57,7 +63,6 @@ func (us UserStore) Update(id int, u model.User) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Delete removes user identified by 'id' from the database
|
// Delete removes user identified by 'id' from the database
|
||||||
func (us UserStore) Delete(id int) {
|
func (us UserStore) Delete(id int) {
|
||||||
res, err := us.db.Exec(QueryDelete, id)
|
res, err := us.db.Exec(QueryDelete, id)
|
||||||
|
|
@ -70,4 +75,5 @@ func (us UserStore) Delete(id int) {
|
||||||
}
|
}
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
log.Fatal("Update failed") // too severe
|
log.Fatal("Update failed") // too severe
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue