1
0
Fork 0
peertube/server/models/user-video-rate.ts

81 lines
1.6 KiB
TypeScript
Raw Normal View History

2017-03-08 15:35:43 -05:00
/*
User rates per video.
*/
2017-05-15 16:22:03 -04:00
import { values } from 'lodash'
2017-05-22 14:58:25 -04:00
import * as Sequelize from 'sequelize'
2017-03-08 15:35:43 -05:00
2017-05-15 16:22:03 -04:00
import { VIDEO_RATE_TYPES } from '../initializers'
2017-03-08 15:35:43 -05:00
2017-05-22 14:58:25 -04:00
import { addMethodsToModel } from './utils'
import {
UserVideoRateClass,
UserVideoRateInstance,
UserVideoRateAttributes,
2017-03-08 15:35:43 -05:00
2017-05-22 14:58:25 -04:00
UserVideoRateMethods
} from './user-video-rate-interface'
let UserVideoRate: Sequelize.Model<UserVideoRateInstance, UserVideoRateAttributes>
let load: UserVideoRateMethods.Load
export default function (sequelize, DataTypes) {
UserVideoRate = sequelize.define('UserVideoRate',
2017-03-08 15:35:43 -05:00
{
type: {
2017-05-15 16:22:03 -04:00
type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)),
2017-03-08 15:35:43 -05:00
allowNull: false
}
},
{
indexes: [
{
fields: [ 'videoId', 'userId', 'type' ],
unique: true
}
2017-05-22 14:58:25 -04:00
]
2017-03-08 15:35:43 -05:00
}
)
2017-05-22 14:58:25 -04:00
const classMethods = [
associate,
load
]
addMethodsToModel(UserVideoRate, classMethods)
2017-03-08 15:35:43 -05:00
return UserVideoRate
}
// ------------------------------ STATICS ------------------------------
function associate (models) {
2017-05-22 14:58:25 -04:00
UserVideoRate.belongsTo(models.Video, {
2017-03-08 15:35:43 -05:00
foreignKey: {
name: 'videoId',
allowNull: false
},
onDelete: 'CASCADE'
})
2017-05-22 14:58:25 -04:00
UserVideoRate.belongsTo(models.User, {
2017-03-08 15:35:43 -05:00
foreignKey: {
name: 'userId',
allowNull: false
},
onDelete: 'CASCADE'
})
}
2017-05-22 14:58:25 -04:00
load = function (userId, videoId, transaction, callback) {
const options: Sequelize.FindOptions = {
2017-03-08 15:35:43 -05:00
where: {
userId,
videoId
}
}
if (transaction) options.transaction = transaction
2017-05-22 14:58:25 -04:00
return UserVideoRate.findOne(options).asCallback(callback)
2017-03-08 15:35:43 -05:00
}