mirror of
https://github.com/rjNemo/go-wiki
synced 2026-06-12 13:36:38 +00:00
user struct created, queries, connect create table and insert into db ops
This commit is contained in:
parent
36a83524a5
commit
9044e6ed21
6 changed files with 139 additions and 10 deletions
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
Wiki web application model built using `Go`
|
Wiki web application model built using `Go`
|
||||||
|
|
||||||
|
<!-- img.shields.io -->
|
||||||
|
|
||||||
## Getting Started
|
## 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.
|
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.
|
||||||
|
|
|
||||||
|
|
@ -4,24 +4,67 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq" // postgresql database package
|
||||||
|
"github.com/rjNemo/go-wiki/model"
|
||||||
"github.com/rjNemo/go-wiki/settings"
|
"github.com/rjNemo/go-wiki/settings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Connect read the connection parameters to establish a connection to the
|
// Connect read the connection parameters to establish a connection to the
|
||||||
// database.
|
// database.
|
||||||
func Connect() {
|
func Connect() {
|
||||||
|
log.Print("Inside connect func")
|
||||||
connStr := settings.ConnStr
|
connStr := settings.ConnStr
|
||||||
db, err := sql.Open("postgres", connStr)
|
db, err := sql.Open("postgres", connStr)
|
||||||
|
log.Print("opened connect")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
log.Print("try to ping")
|
||||||
err = db.Ping()
|
err = db.Ping()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Database successfully connected!")
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,14 +9,14 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var exJson string = "https://jsonplaceholder.typicode.com/todos/1"
|
var exJSON string = "https://jsonplaceholder.typicode.com/todos/1"
|
||||||
|
|
||||||
func FetchMain() {
|
func FetchMain() {
|
||||||
b := FetchApi(exJson)
|
b := FetchAPI(exJSON)
|
||||||
fmt.Println(b)
|
fmt.Println(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchApi(s string) Data {
|
func FetchAPI(s string) Data {
|
||||||
r, err := http.NewRequest(http.MethodGet, s, nil)
|
r, err := http.NewRequest(http.MethodGet, s, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
@ -35,8 +35,8 @@ func FetchApi(s string) Data {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Data struct {
|
type Data struct {
|
||||||
UserId int
|
UserID int
|
||||||
Id int
|
ID int
|
||||||
Title string
|
Title string
|
||||||
Completed bool
|
Completed bool
|
||||||
}
|
}
|
||||||
|
|
|
||||||
21
data/queries.go
Normal file
21
data/queries.go
Normal 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
|
||||||
|
`
|
||||||
|
)
|
||||||
8
main.go
8
main.go
|
|
@ -4,19 +4,19 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/rjNemo/go-wiki/controller"
|
|
||||||
"github.com/rjNemo/go-wiki/data"
|
"github.com/rjNemo/go-wiki/data"
|
||||||
"github.com/rjNemo/go-wiki/settings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
startServer(settings.Port, controller.Router)
|
data.Connect()
|
||||||
|
// startServer(settings.Port, controller.Router)
|
||||||
}
|
}
|
||||||
|
|
||||||
func startServer(p string, r func()) {
|
func startServer(p string, r func()) {
|
||||||
log.Printf("Start Go-wiki server on http://localhost:%s", p)
|
log.Printf("Start Go-wiki server on http://localhost:%s", p)
|
||||||
data.Connect()
|
|
||||||
port := ":" + p
|
port := ":" + p
|
||||||
r()
|
r()
|
||||||
log.Fatal(http.ListenAndServe(port, nil))
|
log.Fatal(http.ListenAndServe(port, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// appBuilder
|
||||||
|
|
|
||||||
63
model/user.go
Normal file
63
model/user.go
Normal 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
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue