mirror of
https://github.com/yanislav-igonin/micrach
synced 2025-07-01 17:01:14 +03:00
feat: add threads archivation
This commit is contained in:
parent
e15aa86ad8
commit
371cfe638c
@ -140,9 +140,20 @@ func CreateThread(c *gin.Context) {
|
||||
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
||||
return
|
||||
}
|
||||
|
||||
if threadsCount >= Config.App.ThreadsMaxCount {
|
||||
// получить дату последнего неархивируемого треда
|
||||
// сделать SET для тредов старше этой даты
|
||||
oldestThreadUpdatedAt, err := Repositories.Posts.GetOldestThreadUpdatedAt()
|
||||
if err != nil {
|
||||
log.Println("error:", err)
|
||||
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
||||
return
|
||||
}
|
||||
err = Repositories.Posts.ArchiveThreadsFrom(oldestThreadUpdatedAt)
|
||||
if err != nil {
|
||||
log.Println("error:", err)
|
||||
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
tx, err := conn.Begin(context.TODO())
|
||||
|
@ -210,7 +210,7 @@ func (r *PostsRepository) CreateInTx(tx pgx.Tx, p Post) (int, error) {
|
||||
return createdPost.ID, nil
|
||||
}
|
||||
|
||||
func (r *PostsRepository) GetOldestThreadUpdateAt() (time.Time, error) {
|
||||
func (r *PostsRepository) GetOldestThreadUpdatedAt() (time.Time, error) {
|
||||
sql := `
|
||||
SELECT updated_at
|
||||
FROM posts
|
||||
@ -237,10 +237,8 @@ func (r *PostsRepository) ArchiveThreadsFrom(t time.Time) error {
|
||||
UPDATE posts
|
||||
SET is_archived = true
|
||||
WHERE
|
||||
is_parent = true
|
||||
AND is_deleted != true
|
||||
AND is_archived != true
|
||||
AND updated_at > $1
|
||||
is_archived != true
|
||||
AND updated_at <= $1
|
||||
`
|
||||
|
||||
_, err := Db.Pool.Exec(context.TODO(), sql, t)
|
||||
|
Loading…
Reference in New Issue
Block a user