diff --git a/profile_reply_store.go b/profile_reply_store.go new file mode 100644 index 00000000..59b7087c --- /dev/null +++ b/profile_reply_store.go @@ -0,0 +1,35 @@ +package main + +import ( + "database/sql" + + "./query_gen/lib" +) + +var prstore ProfileReplyStore + +type ProfileReplyStore interface { + Get(id int) (*Reply, error) +} + +// TODO: Refactor this to stop using the global stmt store +// TODO: Add more methods to this like Create() +type SQLProfileReplyStore struct { + get *sql.Stmt +} + +func NewSQLProfileReplyStore() (*SQLProfileReplyStore, error) { + getUserReplyStmt, err := qgen.Builder.SimpleSelect("users_replies", "uid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress", "rid = ?", "", "") + if err != nil { + return nil, err + } + return &SQLProfileReplyStore{ + get: getUserReplyStmt, + }, nil +} + +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 +} diff --git a/reply.go b/reply.go index 105b6d7a..744a726a 100644 --- a/reply.go +++ b/reply.go @@ -7,17 +7,10 @@ package main import ( - "database/sql" "errors" "time" - - "./query_gen/lib" ) -// ? - Should we add a reply store to centralise all the reply logic? Would this cover profile replies too or would that be separate? -var rstore ReplyStore -var prstore ProfileReplyStore - type ReplyUser struct { ID int ParentID int @@ -102,84 +95,3 @@ func (reply *Reply) Delete() error { func (reply *Reply) Copy() Reply { return *reply } - -// TODO: Refactor this to stop hitting the global stmt store -type ReplyStore interface { - Get(id int) (*Reply, error) - Create(tid int, content string, ipaddress string, fid int, uid int) (id int, err error) -} - -type SQLReplyStore struct { - get *sql.Stmt - create *sql.Stmt -} - -func NewSQLReplyStore() (*SQLReplyStore, error) { - getReplyStmt, err := qgen.Builder.SimpleSelect("replies", "tid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress, likeCount", "rid = ?", "", "") - if err != nil { - return nil, err - } - createReplyStmt, err := qgen.Builder.SimpleInsert("replies", "tid, content, parsed_content, createdAt, lastUpdated, ipaddress, words, createdBy", "?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?") - if err != nil { - return nil, err - } - return &SQLReplyStore{ - get: getReplyStmt, - create: createReplyStmt, - }, nil -} - -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 -func (store *SQLReplyStore) Create(tid int, content string, ipaddress string, fid int, uid int) (id int, err error) { - wcount := wordCount(content) - res, err := store.create.Exec(tid, content, parseMessage(content, fid, "forums"), ipaddress, wcount, uid) - if err != nil { - return 0, err - } - lastID, err := res.LastInsertId() - if err != nil { - return 0, err - } - - _, err = stmts.addRepliesToTopic.Exec(1, uid, tid) - if err != nil { - return int(lastID), err - } - tcache, ok := topics.(TopicCache) - if ok { - tcache.CacheRemove(tid) - } - return int(lastID), err -} - -type ProfileReplyStore interface { - Get(id int) (*Reply, error) -} - -// TODO: Refactor this to stop using the global stmt store -// TODO: Add more methods to this like Create() -type SQLProfileReplyStore struct { - get *sql.Stmt -} - -func NewSQLProfileReplyStore() (*SQLProfileReplyStore, error) { - getUserReplyStmt, err := qgen.Builder.SimpleSelect("users_replies", "uid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress", "rid = ?", "", "") - if err != nil { - return nil, err - } - return &SQLProfileReplyStore{ - get: getUserReplyStmt, - }, nil -} - -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 -} diff --git a/reply_store.go b/reply_store.go new file mode 100644 index 00000000..8fccc8ca --- /dev/null +++ b/reply_store.go @@ -0,0 +1,60 @@ +package main + +import "database/sql" +import "./query_gen/lib" + +var rstore ReplyStore + +type ReplyStore interface { + Get(id int) (*Reply, error) + Create(tid int, content string, ipaddress string, fid int, uid int) (id int, err error) +} + +type SQLReplyStore struct { + get *sql.Stmt + create *sql.Stmt +} + +func NewSQLReplyStore() (*SQLReplyStore, error) { + getReplyStmt, err := qgen.Builder.SimpleSelect("replies", "tid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress, likeCount", "rid = ?", "", "") + if err != nil { + return nil, err + } + createReplyStmt, err := qgen.Builder.SimpleInsert("replies", "tid, content, parsed_content, createdAt, lastUpdated, ipaddress, words, createdBy", "?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?") + if err != nil { + return nil, err + } + return &SQLReplyStore{ + get: getReplyStmt, + create: createReplyStmt, + }, nil +} + +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 +func (store *SQLReplyStore) Create(tid int, content string, ipaddress string, fid int, uid int) (id int, err error) { + wcount := wordCount(content) + res, err := store.create.Exec(tid, content, parseMessage(content, fid, "forums"), ipaddress, wcount, uid) + if err != nil { + return 0, err + } + lastID, err := res.LastInsertId() + if err != nil { + return 0, err + } + + _, err = stmts.addRepliesToTopic.Exec(1, uid, tid) + if err != nil { + return int(lastID), err + } + tcache, ok := topics.(TopicCache) + if ok { + tcache.CacheRemove(tid) + } + return int(lastID), err +}