views package, add, update in userStore, user model rules, add todo.md

This commit is contained in:
Ruidy Nemausat 2020-03-16 14:59:24 +01:00
parent ffa87e8131
commit 75c3a7fd20
3 changed files with 23 additions and 1 deletions

View file

@ -33,6 +33,7 @@ func UsePSQL() {
// log.Print(u)
u1 := model.NewUser(3, 19, "paul", "newman", "PdsN@FDKML.COM")
store.Update(16, u1)
log.Print(store.Get(1))
}
func sqlExec(db *sql.DB, s string) {
@ -52,7 +53,7 @@ func createUserTable(db *sql.DB) {
type Store interface {
CreateTable()
Add(i interface{})
Get()
Get(id int) model.User
Find(id int)
Update(id int, i interface{})
Delete(id int)

View file

@ -11,6 +11,12 @@ const (
last_name TEXT,
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.
// Returning the new ID.
QueryInsert = `

View file

@ -17,6 +17,21 @@ func NewUserStore(db *sql.DB) UserStore {
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.
func (us UserStore) Add(u model.User) {
id := 0