2017-05-22 13:58:25 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
2017-09-22 02:13:43 -05:00
|
|
|
import { SortType } from '../../helpers'
|
|
|
|
import { addMethodsToModel, getSortOnModel } from '../utils'
|
|
|
|
import { VideoInstance } from './video-interface'
|
2017-05-22 13:58:25 -05:00
|
|
|
import {
|
|
|
|
BlacklistedVideoInstance,
|
|
|
|
BlacklistedVideoAttributes,
|
|
|
|
|
|
|
|
BlacklistedVideoMethods
|
|
|
|
} from './video-blacklist-interface'
|
|
|
|
|
|
|
|
let BlacklistedVideo: Sequelize.Model<BlacklistedVideoInstance, BlacklistedVideoAttributes>
|
2017-08-25 04:45:31 -05:00
|
|
|
let toFormattedJSON: BlacklistedVideoMethods.ToFormattedJSON
|
2017-05-22 13:58:25 -05:00
|
|
|
let listForApi: BlacklistedVideoMethods.ListForApi
|
|
|
|
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,
|
|
|
|
|
|
|
|
listForApi,
|
|
|
|
loadByVideoId
|
|
|
|
]
|
|
|
|
const instanceMethods = [
|
2017-08-25 04:45:31 -05:00
|
|
|
toFormattedJSON
|
2017-05-22 13:58:25 -05:00
|
|
|
]
|
|
|
|
addMethodsToModel(BlacklistedVideo, classMethods, instanceMethods)
|
|
|
|
|
2017-04-26 14:22:10 -05:00
|
|
|
return BlacklistedVideo
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
|
2017-08-25 04:45:31 -05:00
|
|
|
toFormattedJSON = function (this: BlacklistedVideoInstance) {
|
2017-09-22 02:13:43 -05:00
|
|
|
let video: VideoInstance
|
|
|
|
|
|
|
|
video = this.Video
|
|
|
|
|
2017-04-26 14:22:10 -05:00
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
videoId: this.videoId,
|
2017-09-22 02:13:43 -05:00
|
|
|
createdAt: this.createdAt,
|
|
|
|
updatedAt: this.updatedAt,
|
|
|
|
name: video.name,
|
|
|
|
uuid: video.uuid,
|
|
|
|
description: video.description,
|
|
|
|
duration: video.duration,
|
|
|
|
views: video.views,
|
|
|
|
likes: video.likes,
|
|
|
|
dislikes: video.dislikes,
|
|
|
|
nsfw: video.nsfw
|
2017-04-26 14:22:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ STATICS ------------------------------
|
|
|
|
|
|
|
|
function associate (models) {
|
2017-05-22 13:58:25 -05:00
|
|
|
BlacklistedVideo.belongsTo(models.Video, {
|
2017-07-11 09:01:56 -05:00
|
|
|
foreignKey: {
|
|
|
|
name: 'videoId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'CASCADE'
|
2017-04-26 14:22:10 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-22 02:13:43 -05:00
|
|
|
listForApi = function (start: number, count: number, sort: SortType) {
|
2017-04-26 14:22:10 -05:00
|
|
|
const query = {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
2017-09-22 02:13:43 -05:00
|
|
|
order: [ getSortOnModel(sort.sortModel, sort.sortValue) ],
|
|
|
|
include: [ { model: BlacklistedVideo['sequelize'].models.Video } ]
|
2017-04-26 14:22:10 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return BlacklistedVideo.findAndCountAll(query).then(({ rows, count }) => {
|
|
|
|
return {
|
|
|
|
data: rows,
|
|
|
|
total: count
|
|
|
|
}
|
2017-04-26 14:22:10 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-11 09:01:56 -05:00
|
|
|
loadByVideoId = function (id: number) {
|
2017-04-26 14:22:10 -05:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
videoId: id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return BlacklistedVideo.findOne(query)
|
2017-04-26 14:22:10 -05:00
|
|
|
}
|