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)
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())

View File

@ -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)