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