PeerTube/server/models/user/user-video-rate.ts

80 lines
1.7 KiB
TypeScript
Raw Normal View History

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