diff --git a/README.md b/README.md index 0baaf26..4141f84 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ Wiki web application model built using `Go` + + ## 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. diff --git a/data/database.go b/data/database.go index 9f0d322..07edeb9 100644 --- a/data/database.go +++ b/data/database.go @@ -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) + } } diff --git a/data/fetch.go b/data/fetch.go index 9b3cb27..2243a61 100644 --- a/data/fetch.go +++ b/data/fetch.go @@ -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 } diff --git a/data/queries.go b/data/queries.go new file mode 100644 index 0000000..b93659b --- /dev/null +++ b/data/queries.go @@ -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 + ` +) diff --git a/main.go b/main.go index 15bb6ac..361027c 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/model/user.go b/model/user.go new file mode 100644 index 0000000..bede2b4 --- /dev/null +++ b/model/user.go @@ -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 +}