2017-11-15 10:56:21 -06:00
|
|
|
import * as Sequelize from 'sequelize'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
|
|
|
|
import { AccountModel } from '../account/account'
|
|
|
|
import { VideoModel } from './video'
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@Table({
|
|
|
|
tableName: 'videoShare',
|
|
|
|
indexes: [
|
2017-11-15 10:56:21 -06:00
|
|
|
{
|
2017-12-12 10:53:50 -06:00
|
|
|
fields: [ 'accountId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'videoId' ]
|
2017-11-15 10:56:21 -06:00
|
|
|
}
|
|
|
|
]
|
2017-12-12 10:53:50 -06:00
|
|
|
})
|
|
|
|
export class VideoShareModel extends Model<VideoShareModel> {
|
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@ForeignKey(() => AccountModel)
|
|
|
|
@Column
|
|
|
|
accountId: number
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@BelongsTo(() => AccountModel, {
|
2017-11-15 10:56:21 -06:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2017-12-12 10:53:50 -06:00
|
|
|
Account: AccountModel
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => VideoModel, {
|
2017-11-15 10:56:21 -06:00
|
|
|
foreignKey: {
|
2017-12-12 10:53:50 -06:00
|
|
|
allowNull: false
|
2017-11-15 10:56:21 -06:00
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2017-12-12 10:53:50 -06:00
|
|
|
Video: VideoModel
|
2017-11-27 07:44:51 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static load (accountId: number, videoId: number, t: Sequelize.Transaction) {
|
|
|
|
return VideoShareModel.findOne({
|
|
|
|
where: {
|
|
|
|
accountId,
|
|
|
|
videoId
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
AccountModel
|
|
|
|
],
|
|
|
|
transaction: t
|
|
|
|
})
|
2017-11-16 08:55:01 -06:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static loadAccountsByShare (videoId: number, t: Sequelize.Transaction) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
videoId
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: AccountModel,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
transaction: t
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoShareModel.findAll(query)
|
|
|
|
.then(res => res.map(r => r.Account))
|
|
|
|
}
|
2017-11-16 08:55:01 -06:00
|
|
|
}
|