45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package models
|
|
|
|
import (
|
|
"database/sql"
|
|
"time"
|
|
)
|
|
|
|
type Post struct {
|
|
ID int
|
|
ThreadID int
|
|
UserID int
|
|
PostTime time.Time
|
|
EditTime *time.Time
|
|
Content string
|
|
AttachmentHash *int64
|
|
AttachmentName *string
|
|
Title string
|
|
ReplyTo int
|
|
}
|
|
|
|
func GetPostsByThreadID(db *sql.DB, threadID int) ([]Post, error) {
|
|
query := "SELECT id, thread_id, user_id, post_time, edit_time, content, attachment_hash, attachment_name, title, reply_to FROM posts WHERE thread_id = ? ORDER BY post_time ASC"
|
|
rows, err := db.Query(query, threadID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var posts []Post
|
|
for rows.Next() {
|
|
post := Post{}
|
|
err := rows.Scan(&post.ID, &post.ThreadID, &post.UserID, &post.PostTime, &post.EditTime, &post.Content, &post.AttachmentHash, &post.AttachmentName, &post.Title, &post.ReplyTo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
posts = append(posts, post)
|
|
}
|
|
return posts, nil
|
|
}
|
|
|
|
func CreatePost(db *sql.DB, post Post) error {
|
|
query := "INSERT INTO posts (thread_id, user_id, content, title, reply_to, post_time) VALUES (?, ?, ?, ?, ?, NOW())"
|
|
_, err := db.Exec(query, post.ThreadID, post.UserID, post.Content, post.Title, post.ReplyTo)
|
|
return err
|
|
} |