aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video-blacklist.ts
blob: 1f00702c7c955bf369f8f88da072f86a4dc4d069 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { getSort } from './utils'

// ---------------------------------------------------------------------------

module.exports = function (sequelize, DataTypes) {
  const BlacklistedVideo = sequelize.define('BlacklistedVideo',
    {},
    {
      indexes: [
        {
          fields: [ 'videoId' ],
          unique: true
        }
      ],
      classMethods: {
        associate,

        countTotal,
        list,
        listForApi,
        loadById,
        loadByVideoId
      },
      instanceMethods: {
        toFormatedJSON
      },
      hooks: {}
    }
  )

  return BlacklistedVideo
}

// ------------------------------ METHODS ------------------------------

function toFormatedJSON () {
  return {
    id: this.id,
    videoId: this.videoId,
    createdAt: this.createdAt
  }
}

// ------------------------------ STATICS ------------------------------

function associate (models) {
  this.belongsTo(models.Video, {
    foreignKey: 'videoId',
    onDelete: 'cascade'
  })
}

function countTotal (callback) {
  return this.count().asCallback(callback)
}

function list (callback) {
  return this.findAll().asCallback(callback)
}

function listForApi (start, count, sort, callback) {
  const query = {
    offset: start,
    limit: count,
    order: [ getSort(sort) ]
  }

  return this.findAndCountAll(query).asCallback(function (err, result) {
    if (err) return callback(err)

    return callback(null, result.rows, result.count)
  })
}

function loadById (id, callback) {
  return this.findById(id).asCallback(callback)
}

function loadByVideoId (id, callback) {
  const query = {
    where: {
      videoId: id
    }
  }

  return this.find(query).asCallback(callback)
}