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
ffa87e8131
commit
75c3a7fd20
3 changed files with 23 additions and 1 deletions
|
|
@ -33,6 +33,7 @@ func UsePSQL() {
|
||||||
// log.Print(u)
|
// log.Print(u)
|
||||||
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))
|
||||||
}
|
}
|
||||||
|
|
||||||
func sqlExec(db *sql.DB, s string) {
|
func sqlExec(db *sql.DB, s string) {
|
||||||
|
|
@ -52,7 +53,7 @@ func createUserTable(db *sql.DB) {
|
||||||
type Store interface {
|
type Store interface {
|
||||||
CreateTable()
|
CreateTable()
|
||||||
Add(i interface{})
|
Add(i interface{})
|
||||||
Get()
|
Get(id int) model.User
|
||||||
Find(id int)
|
Find(id int)
|
||||||
Update(id int, i interface{})
|
Update(id int, i interface{})
|
||||||
Delete(id int)
|
Delete(id int)
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,12 @@ const (
|
||||||
last_name TEXT,
|
last_name TEXT,
|
||||||
email TEXT UNIQUE NOT NULL)
|
email TEXT UNIQUE NOT NULL)
|
||||||
`
|
`
|
||||||
|
|
||||||
|
// QueryGet is the SQL command used to retrieve a user from the table
|
||||||
|
QueryGet = `
|
||||||
|
SELECT * id FROM users WHERE id=$1;
|
||||||
|
`
|
||||||
|
|
||||||
// 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.
|
||||||
// Returning the new ID.
|
// Returning the new ID.
|
||||||
QueryInsert = `
|
QueryInsert = `
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,21 @@ func NewUserStore(db *sql.DB) UserStore {
|
||||||
return UserStore{db: db}
|
return UserStore{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get retrieves the user identified by 'id' from database
|
||||||
|
func (us UserStore) Get(uid int) model.User {
|
||||||
|
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")
|
||||||
|
case nil:
|
||||||
|
return model.NewUser(id, age,firstName,lastName,email)
|
||||||
|
default:
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Add inserts a user in the database.
|
// Add inserts a user in the database.
|
||||||
func (us UserStore) Add(u model.User) {
|
func (us UserStore) Add(u model.User) {
|
||||||
id := 0
|
id := 0
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue