Skip to content

Commit

Permalink
Create user
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseProgram committed Jan 3, 2018
1 parent 4892006 commit ba1d5a7
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

#IDE
*.idea
37 changes: 37 additions & 0 deletions mian.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"net/http"
"github.com/go-ozzo/ozzo-routing"
"github.com/go-ozzo/ozzo-routing/slash"
"databases/user"
"database/sql"
_ "github.com/lib/pq"
)

var db *sql.DB

func main() {
db, err := sql.Open("postgres", "user=postgres password=126126 dbname=dbproj sslmode=disable")
if err != nil {
panic(err)
}
defer db.Close()

router := routing.New()
router.Use(
slash.Remover(http.StatusMovedPermanently),
)

userApi := router.Group("/api/user")
userApi.Post(`/<nickname:[\w+\.]+>/create`, func(c *routing.Context) error {

content, responseCode := user.Create(c, db)
c.Response.Header().Set("Content-Type", "application/json")
c.Response.WriteHeader(responseCode)
return c.Write(content)
})

http.Handle("/", router)
http.ListenAndServe(":5000", nil)
}
72 changes: 72 additions & 0 deletions user/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package user

import (
"encoding/json"
"database/sql"
"github.com/go-ozzo/ozzo-routing"
"time"
"fmt"
)

type UserStruct struct {
About string `json:"about"`
Email string `json:"email"`
Fullname string `json:"fullname"`
Nickname string `json:"nickname"`
}

var POST UserStruct

const insertStatement = "INSERT INTO users (about, email, fullname, nickname) VALUES ($1,$2,$3,$4)"
const selectStatement = "SELECT about, email, fullname, nickname FROM users WHERE email=$1 OR nickname=$2"

func check(err error) {
if err != nil {
panic(err)
}
}

func getPost(c *routing.Context) {
c.Request.ParseForm();
decoder := json.NewDecoder(c.Request.Body)
err := decoder.Decode(&POST)
check(err)
}

func Create(c *routing.Context, db *sql.DB) (string, int) {
getPost(c)
defer c.Request.Body.Close()

about := POST.About
email := POST.Email
fullname := POST.Fullname
nickname := c.Param("nickname")

code := 201
row := db.QueryRow(insertStatement, about, email, fullname, nickname)
err := row.Scan()
if err != nil && err != sql.ErrNoRows {
code = 409
rows, err := db.Query("SELECT about, email, fullname, nickname FROM users WHERE email=" + email + " OR nickname=" + nickname)
check(err)

for rows.Next() {
var uid int
var username string
var department string
var created time.Time
err = rows.Scan(&uid, &username, &department, &created)
check(err)
}
}

res := &UserStruct{
About: about,
Email: email,
Fullname: fullname,
Nickname: nickname,
}

content, _ := json.Marshal(res)
return string(content), code
}

0 comments on commit ba1d5a7

Please sign in to comment.