dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

tableDownloads.go (1017B)


      1 package database
      2 
      3 import (
      4 	"github.com/sirupsen/logrus"
      5 	"time"
      6 )
      7 
      8 // Download table that keep tracks of downloaded files by the users.
      9 type Download struct {
     10 	ID        int64
     11 	UserID    UserID
     12 	User      User
     13 	Filename  string
     14 	CreatedAt time.Time
     15 }
     16 
     17 func (d *DkfDB) DeleteOldDownloads() {
     18 	if err := d.db.Delete(Download{}, "created_at < date('now', '-90 Day')").Error; err != nil {
     19 		logrus.Error(err)
     20 	}
     21 }
     22 
     23 // CreateDownload ...
     24 func (d *DkfDB) CreateDownload(userID UserID, filename string) (out Download, err error) {
     25 	out = Download{UserID: userID, Filename: filename}
     26 	err = d.db.Create(&out).Error
     27 	return
     28 }
     29 
     30 // UserNbDownloaded returns how many times a user downloaded a file
     31 func (d *DkfDB) UserNbDownloaded(userID UserID, filename string) (out int64) {
     32 	d.db.Table("downloads").Where("user_id = ? AND filename = ?", userID, filename).Count(&out)
     33 	return
     34 }
     35 
     36 func (d *DkfDB) DeleteDownloadByID(downloadID int64) (err error) {
     37 	err = d.db.Unscoped().Delete(Download{}, "id = ?", downloadID).Error
     38 	return
     39 }