mirror of
https://github.com/rjNemo/go-wiki
synced 2026-06-06 02:36:40 +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")
|
||||
store.Update(16, u1)
|
||||
log.Print(store.Get(1))
|
||||
store.Delete(8)
|
||||
}
|
||||
|
||||
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
|
||||
type Store interface {
|
||||
CreateTable()
|
||||
// CreateTable()
|
||||
Add(i interface{})
|
||||
Get(id int) model.User
|
||||
Find(id int)
|
||||
Update(id int, i interface{})
|
||||
Get(id int) (model.User, error)
|
||||
GetAll(id int) ([]model.User, error)
|
||||
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 = `
|
||||
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.
|
||||
|
|
|
|||
|
|
@ -17,18 +17,24 @@ func NewUserStore(db *sql.DB) UserStore {
|
|||
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
|
||||
func (us UserStore) Get(uid int) model.User {
|
||||
func (us UserStore) Get(uid int) (model.User, error) {
|
||||
var id, age int
|
||||
var firstName, lastName, email string
|
||||
|
||||
row := us.db.QueryRow(QueryGet, uid)
|
||||
switch err := row.Scan(&id, &age, &firstName, &lastName, &email); err {
|
||||
case sql.ErrNoRows:
|
||||
log.Println("No entry returned")
|
||||
return model.User{}, err
|
||||
case nil:
|
||||
return model.NewUser(id, age,firstName,lastName,email)
|
||||
return model.NewUser(id, age, firstName, lastName, email), nil
|
||||
default:
|
||||
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
|
||||
func (us UserStore) Delete(id int) {
|
||||
res, err := us.db.Exec(QueryDelete, id)
|
||||
|
|
@ -71,3 +76,4 @@ func (us UserStore) Delete(id int) {
|
|||
if count == 0 {
|
||||
log.Fatal("Update failed") // too severe
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue