-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4892006
commit ba1d5a7
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,6 @@ | |
|
||
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 | ||
.glide/ | ||
|
||
#IDE | ||
*.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |