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

79 lines
1.7 KiB
TypeScript
Raw Normal View History

2017-03-08 14:35:43 -06:00
/*
2017-11-09 10:51:58 -06:00
Account rates per video.
2017-03-08 14:35:43 -06:00
*/
2017-05-15 15:22:03 -05:00
import { values } from 'lodash'
2017-05-22 13:58:25 -05:00
import * as Sequelize from 'sequelize'
2017-03-08 14:35:43 -06:00
2017-06-16 02:45:46 -05:00
import { VIDEO_RATE_TYPES } from '../../initializers'
2017-03-08 14:35:43 -06:00
2017-06-16 02:45:46 -05:00
import { addMethodsToModel } from '../utils'
2017-05-22 13:58:25 -05:00
import {
2017-11-09 10:51:58 -06:00
AccountVideoRateInstance,
AccountVideoRateAttributes,
2017-03-08 14:35:43 -06:00
2017-11-09 10:51:58 -06:00
AccountVideoRateMethods
} from './account-video-rate-interface'
2017-05-22 13:58:25 -05:00
2017-11-09 10:51:58 -06:00
let AccountVideoRate: Sequelize.Model<AccountVideoRateInstance, AccountVideoRateAttributes>
let load: AccountVideoRateMethods.Load
2017-05-22 13:58:25 -05:00
2017-06-11 10:35:32 -05:00
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
2017-11-09 10:51:58 -06:00
AccountVideoRate = sequelize.define<AccountVideoRateInstance, AccountVideoRateAttributes>('AccountVideoRate',
2017-03-08 14:35:43 -06:00
{
type: {
2017-05-15 15:22:03 -05:00
type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)),
2017-03-08 14:35:43 -06:00
allowNull: false
}
},
{
indexes: [
{
fields: [ 'videoId', 'accountId' ],
2017-03-08 14:35:43 -06:00
unique: true
}
2017-05-22 13:58:25 -05:00
]
2017-03-08 14:35:43 -06:00
}
)
2017-05-22 13:58:25 -05:00
const classMethods = [
associate,
load
]
2017-11-09 10:51:58 -06:00
addMethodsToModel(AccountVideoRate, classMethods)
2017-05-22 13:58:25 -05:00
2017-11-09 10:51:58 -06:00
return AccountVideoRate
2017-03-08 14:35:43 -06:00
}
// ------------------------------ STATICS ------------------------------
function associate (models) {
2017-11-09 10:51:58 -06:00
AccountVideoRate.belongsTo(models.Video, {
2017-03-08 14:35:43 -06:00
foreignKey: {
name: 'videoId',
allowNull: false
},
onDelete: 'CASCADE'
})
2017-11-09 10:51:58 -06:00
AccountVideoRate.belongsTo(models.Account, {
2017-03-08 14:35:43 -06:00
foreignKey: {
2017-11-09 10:51:58 -06:00
name: 'accountId',
2017-03-08 14:35:43 -06:00
allowNull: false
},
onDelete: 'CASCADE'
})
}
2017-11-09 10:51:58 -06:00
load = function (accountId: number, videoId: number, transaction: Sequelize.Transaction) {
const options: Sequelize.FindOptions<AccountVideoRateAttributes> = {
2017-03-08 14:35:43 -06:00
where: {
2017-11-09 10:51:58 -06:00
accountId,
2017-03-08 14:35:43 -06:00
videoId
}
}
if (transaction) options.transaction = transaction
2017-11-09 10:51:58 -06:00
return AccountVideoRate.findOne(options)
2017-03-08 14:35:43 -06:00
}