2017-05-22 14:58:25 -04:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
2017-06-16 03:45:46 -04:00
|
|
|
import { addMethodsToModel, getSort } from '../utils'
|
2017-05-22 14:58:25 -04:00
|
|
|
import {
|
|
|
|
BlacklistedVideoClass,
|
|
|
|
BlacklistedVideoInstance,
|
|
|
|
BlacklistedVideoAttributes,
|
|
|
|
|
|
|
|
BlacklistedVideoMethods
|
|
|
|
} from './video-blacklist-interface'
|
|
|
|
|
|
|
|
let BlacklistedVideo: Sequelize.Model<BlacklistedVideoInstance, BlacklistedVideoAttributes>
|
|
|
|
let toFormatedJSON: BlacklistedVideoMethods.ToFormatedJSON
|
|
|
|
let countTotal: BlacklistedVideoMethods.CountTotal
|
|
|
|
let list: BlacklistedVideoMethods.List
|
|
|
|
let listForApi: BlacklistedVideoMethods.ListForApi
|
|
|
|
let loadById: BlacklistedVideoMethods.LoadById
|
|
|
|
let loadByVideoId: BlacklistedVideoMethods.LoadByVideoId
|
|
|
|
|
2017-06-11 11:35:32 -04:00
|
|
|
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
|
|
|
|
BlacklistedVideo = sequelize.define<BlacklistedVideoInstance, BlacklistedVideoAttributes>('BlacklistedVideo',
|
2017-04-26 15:22:10 -04:00
|
|
|
{},
|
|
|
|
{
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'videoId' ],
|
|
|
|
unique: true
|
|
|
|
}
|
2017-05-22 14:58:25 -04:00
|
|
|
]
|
2017-04-26 15:22:10 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
const classMethods = [
|
|
|
|
associate,
|
|
|
|
|
|
|
|
countTotal,
|
|
|
|
list,
|
|
|
|
listForApi,
|
|
|
|
loadById,
|
|
|
|
loadByVideoId
|
|
|
|
]
|
|
|
|
const instanceMethods = [
|
|
|
|
toFormatedJSON
|
|
|
|
]
|
|
|
|
addMethodsToModel(BlacklistedVideo, classMethods, instanceMethods)
|
|
|
|
|
2017-04-26 15:22:10 -04:00
|
|
|
return BlacklistedVideo
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
toFormatedJSON = function () {
|
2017-04-26 15:22:10 -04:00
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
videoId: this.videoId,
|
|
|
|
createdAt: this.createdAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ STATICS ------------------------------
|
|
|
|
|
|
|
|
function associate (models) {
|
2017-05-22 14:58:25 -04:00
|
|
|
BlacklistedVideo.belongsTo(models.Video, {
|
2017-04-26 15:22:10 -04:00
|
|
|
foreignKey: 'videoId',
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
countTotal = function (callback: BlacklistedVideoMethods.CountTotalCallback) {
|
2017-05-22 14:58:25 -04:00
|
|
|
return BlacklistedVideo.count().asCallback(callback)
|
2017-04-26 15:22:10 -04:00
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
list = function (callback: BlacklistedVideoMethods.ListCallback) {
|
2017-05-22 14:58:25 -04:00
|
|
|
return BlacklistedVideo.findAll().asCallback(callback)
|
2017-04-26 15:22:10 -04:00
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
listForApi = function (start: number, count: number, sort: string, callback: BlacklistedVideoMethods.ListForApiCallback) {
|
2017-04-26 15:22:10 -04:00
|
|
|
const query = {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
2017-05-15 16:22:03 -04:00
|
|
|
order: [ getSort(sort) ]
|
2017-04-26 15:22:10 -04:00
|
|
|
}
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
return BlacklistedVideo.findAndCountAll(query).asCallback(function (err, result) {
|
2017-04-26 15:22:10 -04:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
return callback(null, result.rows, result.count)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
loadById = function (id: number, callback: BlacklistedVideoMethods.LoadByIdCallback) {
|
2017-05-22 14:58:25 -04:00
|
|
|
return BlacklistedVideo.findById(id).asCallback(callback)
|
2017-04-26 15:22:10 -04:00
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
loadByVideoId = function (id: string, callback: BlacklistedVideoMethods.LoadByIdCallback) {
|
2017-04-26 15:22:10 -04:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
videoId: id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
return BlacklistedVideo.find(query).asCallback(callback)
|
2017-04-26 15:22:10 -04:00
|
|
|
}
|