]>
Commit | Line | Data |
---|---|---|
d8465018 | 1 | import * as Sequelize from 'sequelize' |
3fd3ab2d C |
2 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' |
3 | import { AccountModel } from '../account/account' | |
4 | import { VideoChannelModel } from './video-channel' | |
d8465018 | 5 | |
3fd3ab2d C |
6 | @Table({ |
7 | tableName: 'videoChannelShare', | |
8 | indexes: [ | |
d8465018 | 9 | { |
3fd3ab2d C |
10 | fields: [ 'accountId' ] |
11 | }, | |
12 | { | |
13 | fields: [ 'videoChannelId' ] | |
d8465018 | 14 | } |
d8465018 | 15 | ] |
3fd3ab2d C |
16 | }) |
17 | export class VideoChannelShareModel extends Model<VideoChannelShareModel> { | |
18 | @CreatedAt | |
19 | createdAt: Date | |
d8465018 | 20 | |
3fd3ab2d C |
21 | @UpdatedAt |
22 | updatedAt: Date | |
d8465018 | 23 | |
3fd3ab2d C |
24 | @ForeignKey(() => AccountModel) |
25 | @Column | |
26 | accountId: number | |
d8465018 | 27 | |
3fd3ab2d | 28 | @BelongsTo(() => AccountModel, { |
d8465018 | 29 | foreignKey: { |
d8465018 C |
30 | allowNull: false |
31 | }, | |
32 | onDelete: 'cascade' | |
33 | }) | |
3fd3ab2d | 34 | Account: AccountModel |
d8465018 | 35 | |
3fd3ab2d C |
36 | @ForeignKey(() => VideoChannelModel) |
37 | @Column | |
38 | videoChannelId: number | |
39 | ||
40 | @BelongsTo(() => VideoChannelModel, { | |
d8465018 | 41 | foreignKey: { |
3fd3ab2d | 42 | allowNull: false |
d8465018 C |
43 | }, |
44 | onDelete: 'cascade' | |
45 | }) | |
3fd3ab2d | 46 | VideoChannel: VideoChannelModel |
4e50b6a1 | 47 | |
3fd3ab2d C |
48 | static load (accountId: number, videoChannelId: number, t: Sequelize.Transaction) { |
49 | return VideoChannelShareModel.findOne({ | |
50 | where: { | |
51 | accountId, | |
52 | videoChannelId | |
53 | }, | |
54 | include: [ | |
55 | AccountModel, | |
56 | VideoChannelModel | |
57 | ], | |
58 | transaction: t | |
59 | }) | |
d7d5611c C |
60 | } |
61 | ||
3fd3ab2d C |
62 | static loadAccountsByShare (videoChannelId: number, t: Sequelize.Transaction) { |
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 | } | |
d7d5611c | 79 | } |