2017-11-10 03:33:11 +00:00
|
|
|
package common
|
2017-11-06 07:44:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2017-11-11 04:06:16 +00:00
|
|
|
|
|
|
|
"../query_gen/lib"
|
2017-11-06 07:44:08 +00:00
|
|
|
)
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
var Prstore ProfileReplyStore
|
2017-11-06 07:44:08 +00:00
|
|
|
|
|
|
|
type ProfileReplyStore interface {
|
2018-01-20 06:50:29 +00:00
|
|
|
Get(id int) (*ProfileReply, 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
|
|
|
}
|
|
|
|
|
2018-05-14 08:56:56 +00:00
|
|
|
func NewSQLProfileReplyStore(acc *qgen.Accumulator) (*SQLProfileReplyStore, error) {
|
2017-11-06 07:44:08 +00:00
|
|
|
return &SQLProfileReplyStore{
|
2017-11-12 03:29:05 +00:00
|
|
|
get: acc.Select("users_replies").Columns("uid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress").Where("rid = ?").Prepare(),
|
|
|
|
create: acc.Insert("users_replies").Columns("uid, content, parsed_content, createdAt, createdBy, ipaddress").Fields("?,?,?,UTC_TIMESTAMP(),?,?").Prepare(),
|
2017-11-06 16:24:45 +00:00
|
|
|
}, acc.FirstError()
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 06:50:29 +00:00
|
|
|
func (store *SQLProfileReplyStore) Get(id int) (*ProfileReply, error) {
|
|
|
|
reply := ProfileReply{ID: id}
|
2017-11-06 07:44:08 +00:00
|
|
|
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) {
|
2017-11-11 04:06:16 +00:00
|
|
|
res, err := store.create.Exec(profileID, content, ParseMessage(content, 0, ""), createdBy, ipaddress)
|
2017-11-06 16:24:45 +00:00
|
|
|
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
|
|
|
|
}
|