Skip to content

Commit

Permalink
tweaks and update to Go 1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
sausheong committed Nov 28, 2015
1 parent 98cd47d commit b1ed316
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 103 deletions.
7 changes: 5 additions & 2 deletions Chapter_2_Go_ChitChat/chitchat/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
"log"
)

func db() (database *sql.DB) {
database, err := sql.Open("postgres", "dbname=chitchat sslmode=disable")
var Db *sql.DB

func init() {
var err error
Db, err = sql.Open("postgres", "dbname=chitchat sslmode=disable")
if err != nil {
log.Fatal(err)
}
Expand Down
38 changes: 8 additions & 30 deletions Chapter_2_Go_ChitChat/chitchat/data/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ func (post *Post) CreatedAtDate() string {

// get the number of posts in a thread
func (thread *Thread) NumReplies() (count int) {
db := db()
defer db.Close()
rows, err := db.Query("SELECT count(*) FROM posts where thread_id = $1", thread.Id)
rows, err := Db.Query("SELECT count(*) FROM posts where thread_id = $1", thread.Id)
if err != nil {
return
}
Expand All @@ -49,9 +47,7 @@ func (thread *Thread) NumReplies() (count int) {

// get posts to a thread
func (thread *Thread) Posts() (posts []Post, err error) {
db := db()
defer db.Close()
rows, err := db.Query("SELECT id, uuid, body, user_id, thread_id, created_at FROM posts where thread_id = $1", thread.Id)
rows, err := Db.Query("SELECT id, uuid, body, user_id, thread_id, created_at FROM posts where thread_id = $1", thread.Id)
if err != nil {
return
}
Expand All @@ -68,45 +64,33 @@ func (thread *Thread) Posts() (posts []Post, err error) {

// Create a new thread
func (user *User) CreateThread(topic string) (conv Thread, err error) {
db := db()
defer db.Close()
statement := "insert into threads (uuid, topic, user_id, created_at) values ($1, $2, $3, $4) returning id, uuid, topic, user_id, created_at"
stmt, err := db.Prepare(statement)
stmt, err := Db.Prepare(statement)
if err != nil {
return
}
defer stmt.Close()
// use QueryRow to return a row and scan the returned id into the Session struct
err = stmt.QueryRow(createUUID(), topic, user.Id, time.Now()).Scan(&conv.Id, &conv.Uuid, &conv.Topic, &conv.UserId, &conv.CreatedAt)
if err != nil {
return
}
return
}

// Create a new post to a thread
func (user *User) CreatePost(conv Thread, body string) (post Post, err error) {
db := db()
defer db.Close()
statement := "insert into posts (uuid, body, user_id, thread_id, created_at) values ($1, $2, $3, $4, $5) returning id, uuid, body, user_id, thread_id, created_at"
stmt, err := db.Prepare(statement)
stmt, err := Db.Prepare(statement)
if err != nil {
return
}
defer stmt.Close()
// use QueryRow to return a row and scan the returned id into the Session struct
err = stmt.QueryRow(createUUID(), body, user.Id, conv.Id, time.Now()).Scan(&post.Id, &post.Uuid, &post.Body, &post.UserId, &post.ThreadId, &post.CreatedAt)
if err != nil {
return
}
return
}

// Get all threads in the database and returns it
func Threads() (threads []Thread, err error) {
db := db()
defer db.Close()
rows, err := db.Query("SELECT id, uuid, topic, user_id, created_at FROM threads ORDER BY created_at DESC")
rows, err := Db.Query("SELECT id, uuid, topic, user_id, created_at FROM threads ORDER BY created_at DESC")
if err != nil {
return
}
Expand All @@ -123,30 +107,24 @@ func Threads() (threads []Thread, err error) {

// Get a thread by the UUID
func ThreadByUUID(uuid string) (conv Thread, err error) {
db := db()
defer db.Close()
conv = Thread{}
err = db.QueryRow("SELECT id, uuid, topic, user_id, created_at FROM threads WHERE uuid = $1", uuid).
err = Db.QueryRow("SELECT id, uuid, topic, user_id, created_at FROM threads WHERE uuid = $1", uuid).
Scan(&conv.Id, &conv.Uuid, &conv.Topic, &conv.UserId, &conv.CreatedAt)
return
}

// Get the user who started this thread
func (thread *Thread) User() (user User) {
db := db()
defer db.Close()
user = User{}
db.QueryRow("SELECT id, uuid, name, email, created_at FROM users WHERE id = $1", thread.UserId).
Db.QueryRow("SELECT id, uuid, name, email, created_at FROM users WHERE id = $1", thread.UserId).
Scan(&user.Id, &user.Uuid, &user.Name, &user.Email, &user.CreatedAt)
return
}

// Get the user who wrote the post
func (post *Post) User() (user User) {
db := db()
defer db.Close()
user = User{}
db.QueryRow("SELECT id, uuid, name, email, created_at FROM users WHERE id = $1", post.UserId).
Db.QueryRow("SELECT id, uuid, name, email, created_at FROM users WHERE id = $1", post.UserId).
Scan(&user.Id, &user.Uuid, &user.Name, &user.Email, &user.CreatedAt)
return
}
79 changes: 13 additions & 66 deletions Chapter_2_Go_ChitChat/chitchat/data/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,28 @@ type Session struct {

// Create a new session for an existing user
func (user *User) CreateSession() (session Session, err error) {
db := db()
defer db.Close()
statement := "insert into sessions (uuid, email, user_id, created_at) values ($1, $2, $3, $4) returning id, uuid, email, user_id, created_at"
stmt, err := db.Prepare(statement)
stmt, err := Db.Prepare(statement)
if err != nil {
return
}
defer stmt.Close()
// use QueryRow to return a row and scan the returned id into the Session struct
err = stmt.QueryRow(createUUID(), user.Email, user.Id, time.Now()).Scan(&session.Id, &session.Uuid, &session.Email, &session.UserId, &session.CreatedAt)
if err != nil {
return
}
return
}

// Get the session for an existing user
func (user *User) Session() (session Session, err error) {
db := db()
defer db.Close()
session = Session{}
err = db.QueryRow("SELECT id, uuid, email, user_id, created_at FROM sessions WHERE user_id = $1", user.Id).
err = Db.QueryRow("SELECT id, uuid, email, user_id, created_at FROM sessions WHERE user_id = $1", user.Id).
Scan(&session.Id, &session.Uuid, &session.Email, &session.UserId, &session.CreatedAt)
return
}

// Check if session is valid in the database
func (session *Session) Check() (valid bool, err error) {
db := db()
defer db.Close()
err = db.QueryRow("SELECT id, uuid, email, user_id, created_at FROM sessions WHERE uuid = $1", session.Uuid).
err = Db.QueryRow("SELECT id, uuid, email, user_id, created_at FROM sessions WHERE uuid = $1", session.Uuid).
Scan(&session.Id, &session.Uuid, &session.Email, &session.UserId, &session.CreatedAt)
if err != nil {
valid = false
Expand All @@ -67,119 +58,85 @@ func (session *Session) Check() (valid bool, err error) {

// Delete session from database
func (session *Session) DeleteByUUID() (err error) {
db := db()
defer db.Close()
statement := "delete from sessions where uuid = $1"
stmt, err := db.Prepare(statement)
stmt, err := Db.Prepare(statement)
if err != nil {
return
}
defer stmt.Close()

_, err = stmt.Exec(session.Uuid)
if err != nil {
return
}
return
}

// Get the user from the session
func (session *Session) User() (user User, err error) {
db := db()
defer db.Close()
user = User{}
err = db.QueryRow("SELECT id, uuid, name, email, created_at FROM users WHERE id = $1", session.UserId).
err = Db.QueryRow("SELECT id, uuid, name, email, created_at FROM users WHERE id = $1", session.UserId).
Scan(&user.Id, &user.Uuid, &user.Name, &user.Email, &user.CreatedAt)
return
}

// Delete all sessions from database
func SessionDeleteAll() (err error) {
db := db()
defer db.Close()
statement := "delete from sessions"
_, err = db.Exec(statement)
if err != nil {
return
}
_, err = Db.Exec(statement)
return
}

// Create a new user, save user info into the database
func (user *User) Create() (err error) {
db := db()
defer db.Close()
// Postgres does not automatically return the last insert id, because it would be wrong to assume
// you're always using a sequence.You need to use the RETURNING keyword in your insert to get this
// information from postgres.
statement := "insert into users (uuid, name, email, password, created_at) values ($1, $2, $3, $4, $5) returning id, uuid, created_at"
stmt, err := db.Prepare(statement)
stmt, err := Db.Prepare(statement)
if err != nil {
return
}
defer stmt.Close()

// use QueryRow to return a row and scan the returned id into the User struct
err = stmt.QueryRow(createUUID(), user.Name, user.Email, Encrypt(user.Password), time.Now()).Scan(&user.Id, &user.Uuid, &user.CreatedAt)
if err != nil {
return
}
return
}

// Delete user from database
func (user *User) Delete() (err error) {
db := db()
defer db.Close()
statement := "delete from users where id = $1"
stmt, err := db.Prepare(statement)
stmt, err := Db.Prepare(statement)
if err != nil {
return
}
defer stmt.Close()

_, err = stmt.Exec(user.Id)
if err != nil {
return
}
return
}

// Update user information in the database
func (user *User) Update() (err error) {
db := db()
defer db.Close()
statement := "update users set name = $2, email = $3 where id = $1"
stmt, err := db.Prepare(statement)
stmt, err := Db.Prepare(statement)
if err != nil {
return
}
defer stmt.Close()

_, err = stmt.Exec(user.Id, user.Name, user.Email)
if err != nil {
return
}
return
}

// Delete all users from database
func UserDeleteAll() (err error) {
db := db()
defer db.Close()
statement := "delete from users"
_, err = db.Exec(statement)
if err != nil {
return
}
_, err = Db.Exec(statement)
return
}

// Get all users in the database and returns it
func Users() (users []User, err error) {
db := db()
defer db.Close()
rows, err := db.Query("SELECT id, uuid, name, email, password, created_at FROM users")
rows, err := Db.Query("SELECT id, uuid, name, email, password, created_at FROM users")
if err != nil {
return
}
Expand All @@ -196,26 +153,16 @@ func Users() (users []User, err error) {

// Get a single user given the email
func UserByEmail(email string) (user User, err error) {
db := db()
defer db.Close()
user = User{}
err = db.QueryRow("SELECT id, uuid, name, email, password, created_at FROM users WHERE email = $1", email).
err = Db.QueryRow("SELECT id, uuid, name, email, password, created_at FROM users WHERE email = $1", email).
Scan(&user.Id, &user.Uuid, &user.Name, &user.Email, &user.Password, &user.CreatedAt)
if err != nil {
return
}
return
}

// Get a single user given the UUID
func UserByUUID(uuid string) (user User, err error) {
db := db()
defer db.Close()
user = User{}
err = db.QueryRow("SELECT id, uuid, name, email, password, created_at FROM users WHERE uuid = $1", uuid).
err = Db.QueryRow("SELECT id, uuid, name, email, password, created_at FROM users WHERE uuid = $1", uuid).
Scan(&user.Id, &user.Uuid, &user.Name, &user.Email, &user.Password, &user.CreatedAt)
if err != nil {
return
}
return
}
3 changes: 1 addition & 2 deletions Chapter_2_Go_ChitChat/chitchat/route_auth.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package main

import (
"chitchat/data"
"github.com/sausheong/gwp/Chapter_2_Go_ChitChat/chitchat/data"
"net/http"
)

// GET /login
// Show the login page
func login(writer http.ResponseWriter, request *http.Request) {

t := parseTemplateFiles("login.layout", "public.navbar", "login")
t.Execute(writer, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion Chapter_2_Go_ChitChat/chitchat/route_main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"chitchat/data"
"github.com/sausheong/gwp/Chapter_2_Go_ChitChat/chitchat/data"
"net/http"
)

Expand Down
2 changes: 1 addition & 1 deletion Chapter_2_Go_ChitChat/chitchat/route_thread.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"chitchat/data"
"fmt"
"github.com/sausheong/gwp/Chapter_2_Go_ChitChat/chitchat/data"
"net/http"
)

Expand Down
2 changes: 1 addition & 1 deletion Chapter_2_Go_ChitChat/chitchat/utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"chitchat/data"
"encoding/json"
"errors"
"fmt"
"github.com/sausheong/gwp/Chapter_2_Go_ChitChat/chitchat/data"
"html/template"
"log"
"net/http"
Expand Down

0 comments on commit b1ed316

Please sign in to comment.