diff options
Diffstat (limited to 'server/models/video/video-channel-share.ts')
-rw-r--r-- | server/models/video/video-channel-share.ts | 120 |
1 files changed, 57 insertions, 63 deletions
diff --git a/server/models/video/video-channel-share.ts b/server/models/video/video-channel-share.ts index 2e9b658a3..cdba32fcd 100644 --- a/server/models/video/video-channel-share.ts +++ b/server/models/video/video-channel-share.ts | |||
@@ -1,85 +1,79 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' | ||
3 | import { AccountModel } from '../account/account' | ||
4 | import { VideoChannelModel } from './video-channel' | ||
2 | 5 | ||
3 | import { addMethodsToModel } from '../utils' | 6 | @Table({ |
4 | import { VideoChannelShareAttributes, VideoChannelShareInstance, VideoChannelShareMethods } from './video-channel-share-interface' | 7 | tableName: 'videoChannelShare', |
5 | 8 | indexes: [ | |
6 | let VideoChannelShare: Sequelize.Model<VideoChannelShareInstance, VideoChannelShareAttributes> | ||
7 | let loadAccountsByShare: VideoChannelShareMethods.LoadAccountsByShare | ||
8 | let load: VideoChannelShareMethods.Load | ||
9 | |||
10 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | ||
11 | VideoChannelShare = sequelize.define<VideoChannelShareInstance, VideoChannelShareAttributes>('VideoChannelShare', | ||
12 | { }, | ||
13 | { | 9 | { |
14 | indexes: [ | 10 | fields: [ 'accountId' ] |
15 | { | 11 | }, |
16 | fields: [ 'accountId' ] | 12 | { |
17 | }, | 13 | fields: [ 'videoChannelId' ] |
18 | { | ||
19 | fields: [ 'videoChannelId' ] | ||
20 | } | ||
21 | ] | ||
22 | } | 14 | } |
23 | ) | ||
24 | |||
25 | const classMethods = [ | ||
26 | associate, | ||
27 | load, | ||
28 | loadAccountsByShare | ||
29 | ] | 15 | ] |
30 | addMethodsToModel(VideoChannelShare, classMethods) | 16 | }) |
17 | export class VideoChannelShareModel extends Model<VideoChannelShareModel> { | ||
18 | @CreatedAt | ||
19 | createdAt: Date | ||
31 | 20 | ||
32 | return VideoChannelShare | 21 | @UpdatedAt |
33 | } | 22 | updatedAt: Date |
34 | 23 | ||
35 | // ------------------------------ METHODS ------------------------------ | 24 | @ForeignKey(() => AccountModel) |
25 | @Column | ||
26 | accountId: number | ||
36 | 27 | ||
37 | function associate (models) { | 28 | @BelongsTo(() => AccountModel, { |
38 | VideoChannelShare.belongsTo(models.Account, { | ||
39 | foreignKey: { | 29 | foreignKey: { |
40 | name: 'accountId', | ||
41 | allowNull: false | 30 | allowNull: false |
42 | }, | 31 | }, |
43 | onDelete: 'cascade' | 32 | onDelete: 'cascade' |
44 | }) | 33 | }) |
34 | Account: AccountModel | ||
45 | 35 | ||
46 | VideoChannelShare.belongsTo(models.VideoChannel, { | 36 | @ForeignKey(() => VideoChannelModel) |
37 | @Column | ||
38 | videoChannelId: number | ||
39 | |||
40 | @BelongsTo(() => VideoChannelModel, { | ||
47 | foreignKey: { | 41 | foreignKey: { |
48 | name: 'videoChannelId', | 42 | allowNull: false |
49 | allowNull: true | ||
50 | }, | 43 | }, |
51 | onDelete: 'cascade' | 44 | onDelete: 'cascade' |
52 | }) | 45 | }) |
53 | } | 46 | VideoChannel: VideoChannelModel |
54 | |||
55 | load = function (accountId: number, videoChannelId: number, t: Sequelize.Transaction) { | ||
56 | return VideoChannelShare.findOne({ | ||
57 | where: { | ||
58 | accountId, | ||
59 | videoChannelId | ||
60 | }, | ||
61 | include: [ | ||
62 | VideoChannelShare['sequelize'].models.Account, | ||
63 | VideoChannelShare['sequelize'].models.VideoChannel | ||
64 | ], | ||
65 | transaction: t | ||
66 | }) | ||
67 | } | ||
68 | 47 | ||
69 | loadAccountsByShare = function (videoChannelId: number, t: Sequelize.Transaction) { | 48 | static load (accountId: number, videoChannelId: number, t: Sequelize.Transaction) { |
70 | const query = { | 49 | return VideoChannelShareModel.findOne({ |
71 | where: { | 50 | where: { |
72 | videoChannelId | 51 | accountId, |
73 | }, | 52 | videoChannelId |
74 | include: [ | 53 | }, |
75 | { | 54 | include: [ |
76 | model: VideoChannelShare['sequelize'].models.Account, | 55 | AccountModel, |
77 | required: true | 56 | VideoChannelModel |
78 | } | 57 | ], |
79 | ], | 58 | transaction: t |
80 | transaction: t | 59 | }) |
81 | } | 60 | } |
82 | 61 | ||
83 | return VideoChannelShare.findAll(query) | 62 | static loadAccountsByShare (videoChannelId: number, t: Sequelize.Transaction) { |
84 | .then(res => res.map(r => r.Account)) | 63 | const query = { |
64 | where: { | ||
65 | videoChannelId | ||
66 | }, | ||
67 | include: [ | ||
68 | { | ||
69 | model: AccountModel, | ||
70 | required: true | ||
71 | } | ||
72 | ], | ||
73 | transaction: t | ||
74 | } | ||
75 | |||
76 | return VideoChannelShareModel.findAll(query) | ||
77 | .then(res => res.map(r => r.Account)) | ||
78 | } | ||
85 | } | 79 | } |