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