2017-11-06 07:44:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"./query_gen/lib"
|
|
|
|
)
|
|
|
|
|
|
|
|
var prstore ProfileReplyStore
|
|
|
|
|
|
|
|
type ProfileReplyStore interface {
|
|
|
|
Get(id int) (*Reply, error)
|
2017-11-06 16:24:45 +00:00
|
|
|
Create(profileID int, content string, createdBy int, ipaddress string) (id int, err error)
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Refactor this to stop using the global stmt store
|
|
|
|
// TODO: Add more methods to this like Create()
|
|
|
|
type SQLProfileReplyStore struct {
|
2017-11-06 16:24:45 +00:00
|
|
|
get *sql.Stmt
|
|
|
|
create *sql.Stmt
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSQLProfileReplyStore() (*SQLProfileReplyStore, error) {
|
2017-11-06 16:24:45 +00:00
|
|
|
acc := qgen.Builder.Accumulator()
|
2017-11-06 07:44:08 +00:00
|
|
|
return &SQLProfileReplyStore{
|
2017-11-06 16:24:45 +00:00
|
|
|
get: acc.SimpleSelect("users_replies", "uid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress", "rid = ?", "", ""),
|
|
|
|
create: acc.SimpleInsert("users_replies", "uid, content, parsed_content, createdAt, createdBy, ipaddress", "?,?,?,UTC_TIMESTAMP(),?,?"),
|
|
|
|
}, acc.FirstError()
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *SQLProfileReplyStore) Get(id int) (*Reply, error) {
|
|
|
|
reply := Reply{ID: id}
|
|
|
|
err := store.get.QueryRow(id).Scan(&reply.ParentID, &reply.Content, &reply.CreatedBy, &reply.CreatedAt, &reply.LastEdit, &reply.LastEditBy, &reply.IPAddress)
|
|
|
|
return &reply, err
|
|
|
|
}
|
2017-11-06 16:24:45 +00:00
|
|
|
|
|
|
|
func (store *SQLProfileReplyStore) Create(profileID int, content string, createdBy int, ipaddress string) (id int, err error) {
|
|
|
|
res, err := store.create.Exec(profileID, content, parseMessage(content, 0, ""), createdBy, ipaddress)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
lastID, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should we reload the user?
|
|
|
|
return int(lastID), err
|
|
|
|
}
|