Last active
August 29, 2015 14:17
-
-
Save jochasinga/9df604d69a1175b73784 to your computer and use it in GitHub Desktop.
Database Adapter Functions for blog API
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"time" | |
"encoding/json" | |
"strconv" | |
"github.com/garyburd/redigo/redis" | |
) | |
var currentPostId int | |
var currentUserId int | |
func RedisConnect() redis.Conn { | |
c, err := redis.Dial("tcp", ":6379") | |
HandleError(err) | |
return c | |
} | |
// Give us some seed data | |
func init() { | |
CreatePost(Post{ | |
User: User{ | |
Username: "pieohpah", | |
Email: "[email protected]", | |
}, | |
Topic: "My First Post", | |
Text: "Hello everyone! This is awesome.", | |
}) | |
CreatePost(Post{ | |
User: User{ | |
Username: "IronMan", | |
Email: "[email protected]", | |
}, | |
Topic: "My Fight with Thor Today", | |
Text: "This is the third time I beat him.", | |
}) | |
} | |
func FindAll() Posts { | |
var posts Post | |
c := RedisConnect() | |
defer c.Close() | |
keys, err := c.Do("KEYS", "post:*") | |
HandleError(err) | |
for _, k := range keys.([]interface{}) { | |
var post Post | |
reply, err := c.Do("GET", k.([]byte)) | |
HandleError(err) | |
if err := json.Unmarshal(reply.([]byte), &post); err != nil { | |
panic(err) | |
} | |
posts = append(posts, post) | |
} | |
return posts | |
} | |
func FindPost(id int) Post { | |
var post Post | |
c := RedisConnect() | |
defer c.Close() | |
reply, err := c.Do("GET", "post:" + strconv.Itoa(id)) | |
HandleError(err) | |
fmt.Println("GET OK") | |
if err = json.Unmarshal(reply.([]byte), &post); err != nil { | |
panic(err) | |
} | |
return post | |
} | |
func CreatePost(p Post) { | |
currentPostId += 1 | |
currentUserId += 1 | |
p.Id = currentPostId | |
p.User.Id = currentUserId | |
p.Timestamp = time.Now() | |
c := RedisConnect() | |
defer c.Close() | |
b, err := json.Marshal(p) | |
HandleError(err) | |
// Save JSON blob to Redis | |
reply, err := c.Do("SET", "post:" + strconv.Itoa(p.Id), b) | |
HandleError(err) | |
fmt.Println("GET ", reply) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment