Created
March 20, 2019 14:30
-
-
Save LordRahl90/0edc361dfe3888753c9767f48d3b088d to your computer and use it in GitHub Desktop.
Testing Psql With console go application. This is a simple console address book. I know the db details aren't supposed to be in the code. I'm just trying it out,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"database/sql" | |
"fmt" | |
"os" | |
"strconv" | |
_ "github.com/lib/pq" | |
) | |
type db interface { | |
Create() | |
Count() | |
ListAll() | |
} | |
type contact struct { | |
id int `db:"id"` | |
age int `db:"age"` | |
firstName string `db:"first_name"` | |
lastName string `db:"last_name"` | |
email string `db:"email"` | |
} | |
func main() { | |
db := connectDB() | |
for opt := buildOptions(); opt != 0; { | |
switch opt { | |
case 0: | |
fmt.Println("Quitting") | |
return | |
case 1: | |
c := buildContact() | |
c.addNewUser(db) | |
fmt.Println("User Added With ID ", c.id) | |
break | |
case 2: | |
fmt.Println("List All Users") | |
listAllUsers(db) | |
case 3: | |
fmt.Println("Edit a user") | |
case 4: | |
fmt.Println("Remove a user") | |
case 5: | |
fmt.Println("Find Contact") | |
buildFindUser(db) | |
} | |
opt = buildOptions() | |
} | |
} | |
func connectDB() *sql.DB { | |
const ( | |
host = "localhost" | |
port = 5432 | |
user = "golang" | |
password = "golang_password" | |
dbName = "storekeeper" | |
) | |
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbName) | |
db, err := sql.Open("postgres", dsn) | |
if err != nil { | |
panic(err) | |
} | |
err = db.Ping() | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("Connection established") | |
return db | |
} | |
func buildOptions() int { | |
defer recover() | |
fmt.Printf("Welcome to Your Personal Organizer\n***Options***\nNew Contact (1)\tAll Contacts (2)\tEdit Contact (3)\tRemove Contact (4)\tFind User (5)\n") | |
r := bufio.NewScanner(os.Stdin) | |
opCode := 0 | |
if r.Scan() { | |
opt, err := strconv.Atoi(r.Text()) | |
if err != nil { | |
panic(err) | |
} | |
opCode = opt | |
} | |
return opCode | |
} | |
func buildContact() *contact { | |
defer recover() | |
var c contact | |
r := bufio.NewScanner(os.Stdin) | |
fmt.Println("Enter Firstname") | |
if r.Scan() { | |
c.firstName = r.Text() | |
} | |
fmt.Println("Enter Lastname") | |
if r.Scan() { | |
c.lastName = r.Text() | |
} | |
fmt.Println("Enter Email") | |
if r.Scan() { | |
c.email = r.Text() | |
} | |
fmt.Println("Enter Age:") | |
if r.Scan() { | |
age, err := strconv.Atoi(r.Text()) | |
if err != nil { | |
panic(err) | |
} | |
c.age = age | |
} | |
return &c | |
} | |
func buildFindUser(db *sql.DB) { | |
r := bufio.NewScanner(os.Stdin) | |
fmt.Println("How do you want to find The User?") | |
var id int | |
if r.Scan() { | |
i, err := strconv.Atoi(r.Text()) | |
if err != nil { | |
panic(err) | |
} | |
id = i | |
} | |
findAUser(id, db) | |
} | |
func (c *contact) addNewUser(db *sql.DB) { | |
//create user at this point. | |
stmt := "INSERT INTO users (first_name,last_name,email,age) VALUES($1,$2,$3,$4)" | |
res, err := db.Exec(stmt, c.firstName, c.lastName, c.email, c.age) | |
if err != nil { | |
panic(err) | |
} | |
lastID, err := res.LastInsertId() | |
if err != nil { | |
panic(err) | |
} | |
c.id = int(lastID) | |
fmt.Println("User Added Successfully.") | |
} | |
func listAllUsers(db *sql.DB) { | |
stmt := "SELECT id,first_name,last_name,email,age FROM users" | |
res, err := db.Query(stmt) | |
if err != nil { | |
panic(err) | |
} | |
for res.Next() { | |
var c contact | |
res.Scan(&c.id, &c.firstName, &c.lastName, &c.email, &c.age) | |
fmt.Printf("Fullname: %s\t Email: %s\tAge: %d\n", c.firstName+" "+c.lastName, c.email, c.age) | |
} | |
} | |
func findAUser(id int, db *sql.DB) { | |
var c contact | |
stmt := "SELECT id,first_name,last_name,email,age FROM users WHERE ID=$1" | |
row := db.QueryRow(stmt, id) | |
switch err := row.Scan(&c.id, &c.firstName, &c.lastName, &c.email, &c.age); err { | |
case sql.ErrNoRows: | |
fmt.Println("No User found with the provided ID.") | |
case nil: | |
fmt.Printf("User Found Name: %s \tEmail: %s\tAge:%d\n", c.firstName+" "+c.lastName, c.email, c.age) | |
default: | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment