topic/cleaning #4

Merged
shah merged 23 commits from topic/cleaning into master 2022-05-31 14:53:59 +00:00
9 changed files with 11 additions and 122 deletions
Showing only changes of commit c20c3adf6d - Show all commits

View file

@ -1,5 +1,3 @@
export NAME=genityapp
build:
docker-compose build
@ -8,3 +6,5 @@ run: build
build-binary:
GOOS=linux GOARCH=amd64 go build
.PHONY: build-binary run build

View file

@ -6,6 +6,8 @@ import (
_ "github.com/lib/pq"
"log"
"net/http"
"genityapi"
)
var db *sql.DB

3
db.env
View file

@ -1,3 +0,0 @@
POSTGRES_DB=db_genity
POSTGRES_USER=root_user
POSTGRES_PASSWORD=shahzad12

View file

@ -1 +0,0 @@
CREATE TABLE users (user_id serial PRIMARY KEY, title VARCHAR ( 50 ) UNIQUE NOT NULL, created_on TIMESTAMP NOT NULL);

View file

@ -1 +0,0 @@
INSERT INTO users (title,created_on) VALUES('shahzad','2013-06-01');

View file

@ -1,13 +1,12 @@
version: '3'
version: "3.8"
services:
db:
image: postgres
env_file:
- ./db.env
env_file: ./configs/db.env
volumes:
- ./db_postgres:/var/lib/postgresql/data
- ./ddl_01.sql:/docker-entrypoint-initdb.d/10_schema.sql
- ./dml_01.sql:/docker-entrypoint-initdb.d/20_insert.sql
- ./scripts/db/ddl_01.sql:/docker-entrypoint-initdb.d/10_schema.sql
- ./scripts/db/dml_01.sql:/docker-entrypoint-initdb.d/20_insert.sql
expose:
- "5432"
restart: always
@ -16,8 +15,7 @@ services:
web:
build: ""
command: [ "go", "run", "main.go","api.go"]
env_file:
- ./web.env
env_file: ./configs/web.env
ports:
- "5000:5000"
restart: always

View file

@ -1,100 +0,0 @@
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
type User struct {
UserID int
Title string
CreatedOn time.Time
}
type Users struct {
users []User
}
func UserGetHandler(w http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
users := Users{}
err := queryRepos(&users)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
out, err := json.Marshal(users.users)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
fmt.Fprintf(w, string(out))
} else {
fmt.Fprintf(w, "Sorry, only Get methods are supported for thus api.")
}
}
func UserPostHandler(w http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
if err := req.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
title := req.FormValue("title")
sqlInsert := `
INSERT INTO users (title, created_on)
VALUES ($1, $2)
RETURNING user_id`
if len(title) > 0 {
user_id := 0
row := db.QueryRow(sqlInsert, title, time.Now())
err := row.Scan(&user_id)
if err != nil {
panic(err)
}
fmt.Fprintf(w, "user creted")
} else {
fmt.Fprintf(w, "Invalid data")
}
} else {
fmt.Fprintf(w, "Sorry, only POST methods are supported for thus api.")
}
}
func queryRepos(users *Users) error {
rows, err := db.Query(`
SELECT
user_id,
title,
created_on
FROM users;
`)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
user := User{}
err = rows.Scan(
&user.UserID,
&user.Title,
&user.CreatedOn,
)
if err != nil {
return err
}
users.users = append(users.users, user)
}
err = rows.Err()
if err != nil {
return err
}
return nil
}

2
go.mod
View file

@ -1,4 +1,4 @@
module genity-code-review
module genityapi
go 1.14

View file

@ -1,6 +0,0 @@
COMPOSE_PROJECT_NAME=genitywebapp
dbhost=genityapp_db_1"
dbport=5432
dbuser=root_user
dbpass=shahzad12
dbname=db_genity