feat: add threads archivation

This commit is contained in:
Yanislav Igonin 2021-11-20 19:11:30 +02:00
parent e15aa86ad8
commit 371cfe638c
2 changed files with 16 additions and 7 deletions

View File

@ -140,9 +140,20 @@ func CreateThread(c *gin.Context) {
c.HTML(http.StatusInternalServerError, "500.html", nil) c.HTML(http.StatusInternalServerError, "500.html", nil)
return return
} }
if threadsCount >= Config.App.ThreadsMaxCount { if threadsCount >= Config.App.ThreadsMaxCount {
// получить дату последнего неархивируемого треда oldestThreadUpdatedAt, err := Repositories.Posts.GetOldestThreadUpdatedAt()
// сделать SET для тредов старше этой даты 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()) tx, err := conn.Begin(context.TODO())

View File

@ -210,7 +210,7 @@ func (r *PostsRepository) CreateInTx(tx pgx.Tx, p Post) (int, error) {
return createdPost.ID, nil return createdPost.ID, nil
} }
func (r *PostsRepository) GetOldestThreadUpdateAt() (time.Time, error) { func (r *PostsRepository) GetOldestThreadUpdatedAt() (time.Time, error) {
sql := ` sql := `
SELECT updated_at SELECT updated_at
FROM posts FROM posts
@ -237,10 +237,8 @@ func (r *PostsRepository) ArchiveThreadsFrom(t time.Time) error {
UPDATE posts UPDATE posts
SET is_archived = true SET is_archived = true
WHERE WHERE
is_parent = true is_archived != true
AND is_deleted != true AND updated_at <= $1
AND is_archived != true
AND updated_at > $1
` `
_, err := Db.Pool.Exec(context.TODO(), sql, t) _, err := Db.Pool.Exec(context.TODO(), sql, t)