user struct created, queries, connect create table and insert into db ops

This commit is contained in:
Ruidy Nemausat 2020-03-16 12:28:53 +01:00
parent 36a83524a5
commit 9044e6ed21
6 changed files with 139 additions and 10 deletions

View file

@ -2,6 +2,8 @@
Wiki web application model built using `Go`
<!-- img.shields.io -->
## Getting Started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

View file

@ -4,24 +4,67 @@ import (
"database/sql"
"log"
_ "github.com/lib/pq"
_ "github.com/lib/pq" // postgresql database package
"github.com/rjNemo/go-wiki/model"
"github.com/rjNemo/go-wiki/settings"
)
// Connect read the connection parameters to establish a connection to the
// database.
func Connect() {
log.Print("Inside connect func")
connStr := settings.ConnStr
db, err := sql.Open("postgres", connStr)
log.Print("opened connect")
if err != nil {
log.Fatal(err)
}
defer db.Close()
log.Print("try to ping")
err = db.Ping()
if err != nil {
log.Fatal(err)
}
log.Println("Database successfully connected!")
createTable(db)
u := model.TestUser()
insertUser(db, u)
log.Print(u)
}
func sqlExec(db *sql.DB, s string) {
if _, err := db.Exec(s); err != nil {
log.Fatal(err)
return
}
log.Printf("Command successfully executed!: %s", s)
}
func createTable(db *sql.DB) {
sqlExec(db, QueryCreateTable)
log.Print("Table successfully created!")
}
func insertUser(db *sql.DB, u model.User) {
id := 0
err := db.QueryRow(QueryInsertUser, u.Age(), u.Email(), u.FirstName(), u.LastName()).Scan(&id)
if err != nil {
log.Fatal(err)
}
// u.SetID(id)
log.Println("New User ID is:", id)
}
func insertUsers(db *sql.DB) {
query := `
INSERT INTO users (age, email, first_name, last_name)
VALUES ($1, $2, $3, $4)`
_, err := db.Exec(query, 32, "ruidy.nemausat@gmail.com", "Ruidy", "Nemausat")
if err != nil {
log.Fatal(err)
}
}

View file

@ -9,14 +9,14 @@ import (
"time"
)
var exJson string = "https://jsonplaceholder.typicode.com/todos/1"
var exJSON string = "https://jsonplaceholder.typicode.com/todos/1"
func FetchMain() {
b := FetchApi(exJson)
b := FetchAPI(exJSON)
fmt.Println(b)
}
func FetchApi(s string) Data {
func FetchAPI(s string) Data {
r, err := http.NewRequest(http.MethodGet, s, nil)
if err != nil {
log.Fatal(err)
@ -35,8 +35,8 @@ func FetchApi(s string) Data {
}
type Data struct {
UserId int
Id int
UserID int
ID int
Title string
Completed bool
}

21
data/queries.go Normal file
View file

@ -0,0 +1,21 @@
package data
const (
// QueryCreateTable is the SQL command used to create the users table if it
// not exists.
QueryCreateTable = `
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
age INT,
first_name TEXT,
last_name TEXT,
email TEXT UNIQUE NOT NULL)
`
// QueryInsertUser is the SQL command used to insert a user in the table.
// Returning the new ID.
QueryInsertUser = `
INSERT INTO users (age, email, first_name, last_name)
VALUES ($1, $2, $3, $4)
RETURNING id
`
)

View file

@ -4,19 +4,19 @@ import (
"log"
"net/http"
"github.com/rjNemo/go-wiki/controller"
"github.com/rjNemo/go-wiki/data"
"github.com/rjNemo/go-wiki/settings"
)
func main() {
startServer(settings.Port, controller.Router)
data.Connect()
// startServer(settings.Port, controller.Router)
}
func startServer(p string, r func()) {
log.Printf("Start Go-wiki server on http://localhost:%s", p)
data.Connect()
port := ":" + p
r()
log.Fatal(http.ListenAndServe(port, nil))
}
// appBuilder

63
model/user.go Normal file
View file

@ -0,0 +1,63 @@
package model
// User represent a go-wiki user. It encapsulate its unique identifier, first and
// last names, email and age
type User struct {
id int
firstName string
lastName string
email string
age int
}
// TestUser constructs a sample John Doe user. For Dev and Test only
func TestUser() User {
return User{
id: 42,
firstName: "John",
lastName: "Doe",
email: "jddd@mail.com",
age: 42,
}
}
// NewUser is a constructor
func NewUser(id, age int, first, last, email string) User {
return User{
id: id,
firstName: first,
lastName: last,
email: email,
age: age,
}
}
// ID is a getter
func (u User) ID() int {
return u.id
}
// // SetID is a setter
// func (u *User) SetID(id int) {
// u.id = id
// }
// FirstName is a getter
func (u User) FirstName() string {
return u.firstName
}
// LastName is a getter
func (u User) LastName() string {
return u.lastName
}
// Email is a getter
func (u User) Email() string {
return u.email
}
// FirstName is a getter
func (u User) Age() int {
return u.age
}