Skip to content

Commit

Permalink
Forum works
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseProgram committed Jan 3, 2018
1 parent 1e4c5a0 commit b1d62e7
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 31 deletions.
6 changes: 6 additions & 0 deletions common/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ package common

type ErrStruct struct {
Message string `json:"message"`
}

func Check(err error) {
if err != nil {
panic(err)
}
}
104 changes: 104 additions & 0 deletions forum/forum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package forum

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

type ForumStruct struct {
User string `json:"user"`
Title string `json:"title"`
Slug string `json:"slug"`
Posts int `json:"posts_count"`
Threads int `json:"threads_count"`
}

const insertStatement = "INSERT INTO forums (owner_id, owner_nickname, title, slug) VALUES ($1,$2,$3,$4)"
const selectStatementNickname = "SELECT id, nickname FROM users WHERE nickname=$1"
const selectStatementSlug = "SELECT slug, title FROM forums WHERE slug=$1"
const selectStatementSlugAll = "SELECT slug, title, owner_nickname, posts_count, threads_count FROM forums WHERE slug=$1"

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

func getUser(nickname string, db *sql.DB) (int, string){
row := db.QueryRow(selectStatementNickname, nickname)
var id int
err := row.Scan(&id, &nickname)
switch err {
case sql.ErrNoRows:
return -1, ""
case nil:
return id, nickname
default:
panic(err)
}
}

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

slug := POST.Slug
title := POST.Title
user := POST.User
user_id := -1

user_id, user = getUser(user, db)
if user_id >= 0 {
var res ForumStruct
row := db.QueryRow(insertStatement, user_id, user, title, slug)
err := row.Scan()
if err != nil && err != sql.ErrNoRows {
row := db.QueryRow(selectStatementSlug, slug)
err := row.Scan(&res.Slug, &res.Title)
res.User = user
switch err {
case nil:
content, _ := json.Marshal(res)
return string(content), 409
default:
panic(err)
}
}
res.User = user
res.Slug = slug
res.Title = title
res.Threads = 0;
res.Posts = 0;
content, _ := json.Marshal(res)
return string(content), 201
}
var res common.ErrStruct
res.Message = "Can not create forum. User-creator not found"
content, _ := json.Marshal(res)
return string(content), 404
}

func Details(c *routing.Context, db *sql.DB) (string, int) {
slug := c.Param("slug")
var res ForumStruct

row := db.QueryRow(selectStatementSlugAll, slug)
err := row.Scan(&res.Slug, &res.Title, &res.User, &res.Posts, &res.Threads)
switch err {
case nil:
content, _ := json.Marshal(res)
return string(content), 200
case sql.ErrNoRows:
var res common.ErrStruct
res.Message = "Forum not found!"
content, _ := json.Marshal(res)
return string(content), 404
default:
panic(err)
}
}
23 changes: 23 additions & 0 deletions forum/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package forum

import (
"github.com/go-ozzo/ozzo-routing"
"database/sql"
)

func Route(router *routing.Router, db *sql.DB) {
forumApi := router.Group("/api/forum")
forumApi.Post(`/create`, func(c *routing.Context) error {
content, responseCode := Create(c, db)
c.Response.Header().Set("Content-Type", "application/json")
c.Response.WriteHeader(responseCode)
return c.Write(content)
})

forumApi.Get(`/<slug:[\w+\.\-\_]+>/details`, func(c *routing.Context) error {
content, responseCode := Details(c, db)
c.Response.Header().Set("Content-Type", "application/json")
c.Response.WriteHeader(responseCode)
return c.Write(content)
})
}
25 changes: 4 additions & 21 deletions mian.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"technoparkdb/user"
"database/sql"
_ "github.com/lib/pq"
"technoparkdb/forum"
)

var db *sql.DB
var router *routing.Router

func main() {
db, err := sql.Open("postgres", "user=postgres password=126126 dbname=dbproj sslmode=disable")
Expand All @@ -23,27 +25,8 @@ func main() {
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)
})

userApi.Get(`/<nickname:[\w+\.]+>/profile`, func(c *routing.Context) error {
content, responseCode := user.Profile(c, db)
c.Response.Header().Set("Content-Type", "application/json")
c.Response.WriteHeader(responseCode)
return c.Write(content)
})

userApi.Post(`/<nickname:[\w+\.]+>/profile`, func(c *routing.Context) error {
content, responseCode := user.Update(c, db)
c.Response.Header().Set("Content-Type", "application/json")
c.Response.WriteHeader(responseCode)
return c.Write(content)
})
user.Route(router, db)
forum.Route(router, db)

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

import (
"github.com/go-ozzo/ozzo-routing"
"database/sql"
)

func Route(router *routing.Router, db *sql.DB) {
userApi := router.Group("/api/user")
userApi.Post(`/<nickname:[\w+\.]+>/create`, func(c *routing.Context) error {
content, responseCode := Create(c, db)
c.Response.Header().Set("Content-Type", "application/json")
c.Response.WriteHeader(responseCode)
return c.Write(content)
})

userApi.Get(`/<nickname:[\w+\.]+>/profile`, func(c *routing.Context) error {
content, responseCode := Profile(c, db)
c.Response.Header().Set("Content-Type", "application/json")
c.Response.WriteHeader(responseCode)
return c.Write(content)
})

userApi.Post(`/<nickname:[\w+\.]+>/profile`, func(c *routing.Context) error {
content, responseCode := Update(c, db)
c.Response.Header().Set("Content-Type", "application/json")
c.Response.WriteHeader(responseCode)
return c.Write(content)
})
}

13 changes: 3 additions & 10 deletions user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@ const insertStatement = "INSERT INTO users (about, email, fullname, nickname) VA
const selectStatement = "SELECT about, email, fullname, nickname FROM users WHERE email=$1 OR nickname=$2"
const selectStatementNickname = "SELECT about, email, fullname, nickname FROM users WHERE nickname=$1"

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

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

Expand All @@ -45,16 +39,15 @@ func Create(c *routing.Context, db *sql.DB) (string, int) {
row := db.QueryRow(insertStatement, about, email, fullname, nickname)
err := row.Scan()
if err != nil && err != sql.ErrNoRows {

rows, selerr := db.Query("SELECT about, email, fullname, nickname FROM users WHERE email='" + email + "' OR nickname='" + nickname + "'")
check(selerr)
common.Check(selerr)

var res []UserStruct

for rows.Next() {
var tus UserStruct
err = rows.Scan(&tus.About, &tus.Email, &tus.Fullname, &tus.Nickname)
check(err)
common.Check(err)
res = append(res, tus)
}
content, _ := json.Marshal(res)
Expand Down

0 comments on commit b1d62e7

Please sign in to comment.