2017-11-10 03:33:11 +00:00
|
|
|
package common
|
2017-11-06 07:44:08 +00:00
|
|
|
|
|
|
|
import "database/sql"
|
2018-10-27 03:21:02 +00:00
|
|
|
import "github.com/Azareal/Gosora/query_gen"
|
2017-11-06 07:44:08 +00:00
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
var Rstore ReplyStore
|
2017-11-06 07:44:08 +00:00
|
|
|
|
|
|
|
type ReplyStore interface {
|
|
|
|
Get(id int) (*Reply, error)
|
2017-11-08 07:28:33 +00:00
|
|
|
Create(topic *Topic, content string, ipaddress string, uid int) (id int, err error)
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SQLReplyStore struct {
|
|
|
|
get *sql.Stmt
|
|
|
|
create *sql.Stmt
|
|
|
|
}
|
|
|
|
|
2018-05-14 08:56:56 +00:00
|
|
|
func NewSQLReplyStore(acc *qgen.Accumulator) (*SQLReplyStore, error) {
|
2017-11-06 07:44:08 +00:00
|
|
|
return &SQLReplyStore{
|
2017-11-12 03:29:05 +00:00
|
|
|
get: acc.Select("replies").Columns("tid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress, likeCount").Where("rid = ?").Prepare(),
|
|
|
|
create: acc.Insert("replies").Columns("tid, content, parsed_content, createdAt, lastUpdated, ipaddress, words, createdBy").Fields("?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?").Prepare(),
|
2017-11-06 16:24:45 +00:00
|
|
|
}, acc.FirstError()
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *SQLReplyStore) 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, &reply.LikeCount)
|
|
|
|
return &reply, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Write a test for this
|
2017-11-08 07:28:33 +00:00
|
|
|
func (store *SQLReplyStore) Create(topic *Topic, content string, ipaddress string, uid int) (id int, err error) {
|
2017-11-11 04:06:16 +00:00
|
|
|
wcount := WordCount(content)
|
|
|
|
res, err := store.create.Exec(topic.ID, content, ParseMessage(content, topic.ParentID, "forums"), ipaddress, wcount, uid)
|
2017-11-06 07:44:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-11-08 07:28:33 +00:00
|
|
|
|
2017-11-06 07:44:08 +00:00
|
|
|
lastID, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2018-12-27 05:42:41 +00:00
|
|
|
return int(lastID), topic.AddReply(int(lastID), uid)
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|