diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-12 17:53:50 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-13 16:50:33 +0100 |
commit | 3fd3ab2d34d512b160a5e6084d7609be7b4f4452 (patch) | |
tree | e5ca358287fca6ecacce83defcf23af1e8e9f419 /server/models/video/video-share.ts | |
parent | c893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff) | |
download | PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip |
Move models to typescript-sequelize
Diffstat (limited to 'server/models/video/video-share.ts')
-rw-r--r-- | server/models/video/video-share.ts | 118 |
1 files changed, 56 insertions, 62 deletions
diff --git a/server/models/video/video-share.ts b/server/models/video/video-share.ts index 37e405fa9..01b6d3d34 100644 --- a/server/models/video/video-share.ts +++ b/server/models/video/video-share.ts | |||
@@ -1,84 +1,78 @@ | |||
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 { VideoModel } from './video' | ||
2 | 5 | ||
3 | import { addMethodsToModel } from '../utils' | 6 | @Table({ |
4 | import { VideoShareAttributes, VideoShareInstance, VideoShareMethods } from './video-share-interface' | 7 | tableName: 'videoShare', |
5 | 8 | indexes: [ | |
6 | let VideoShare: Sequelize.Model<VideoShareInstance, VideoShareAttributes> | ||
7 | let loadAccountsByShare: VideoShareMethods.LoadAccountsByShare | ||
8 | let load: VideoShareMethods.Load | ||
9 | |||
10 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | ||
11 | VideoShare = sequelize.define<VideoShareInstance, VideoShareAttributes>('VideoShare', | ||
12 | { }, | ||
13 | { | 9 | { |
14 | indexes: [ | 10 | fields: [ 'accountId' ] |
15 | { | 11 | }, |
16 | fields: [ 'accountId' ] | 12 | { |
17 | }, | 13 | fields: [ 'videoId' ] |
18 | { | ||
19 | fields: [ 'videoId' ] | ||
20 | } | ||
21 | ] | ||
22 | } | 14 | } |
23 | ) | ||
24 | |||
25 | const classMethods = [ | ||
26 | associate, | ||
27 | loadAccountsByShare, | ||
28 | load | ||
29 | ] | 15 | ] |
30 | addMethodsToModel(VideoShare, classMethods) | 16 | }) |
17 | export class VideoShareModel extends Model<VideoShareModel> { | ||
18 | @CreatedAt | ||
19 | createdAt: Date | ||
31 | 20 | ||
32 | return VideoShare | 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 | VideoShare.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 | VideoShare.belongsTo(models.Video, { | 36 | @ForeignKey(() => VideoModel) |
37 | @Column | ||
38 | videoId: number | ||
39 | |||
40 | @BelongsTo(() => VideoModel, { | ||
47 | foreignKey: { | 41 | foreignKey: { |
48 | name: 'videoId', | 42 | allowNull: false |
49 | allowNull: true | ||
50 | }, | 43 | }, |
51 | onDelete: 'cascade' | 44 | onDelete: 'cascade' |
52 | }) | 45 | }) |
53 | } | 46 | Video: VideoModel |
54 | |||
55 | load = function (accountId: number, videoId: number, t: Sequelize.Transaction) { | ||
56 | return VideoShare.findOne({ | ||
57 | where: { | ||
58 | accountId, | ||
59 | videoId | ||
60 | }, | ||
61 | include: [ | ||
62 | VideoShare['sequelize'].models.Account | ||
63 | ], | ||
64 | transaction: t | ||
65 | }) | ||
66 | } | ||
67 | 47 | ||
68 | loadAccountsByShare = function (videoId: number, t: Sequelize.Transaction) { | 48 | static load (accountId: number, videoId: number, t: Sequelize.Transaction) { |
69 | const query = { | 49 | return VideoShareModel.findOne({ |
70 | where: { | 50 | where: { |
71 | videoId | 51 | accountId, |
72 | }, | 52 | videoId |
73 | include: [ | 53 | }, |
74 | { | 54 | include: [ |
75 | model: VideoShare['sequelize'].models.Account, | 55 | AccountModel |
76 | required: true | 56 | ], |
77 | } | 57 | transaction: t |
78 | ], | 58 | }) |
79 | transaction: t | ||
80 | } | 59 | } |
81 | 60 | ||
82 | return VideoShare.findAll(query) | 61 | static loadAccountsByShare (videoId: number, t: Sequelize.Transaction) { |
83 | .then(res => res.map(r => r.Account)) | 62 | const query = { |
63 | where: { | ||
64 | videoId | ||
65 | }, | ||
66 | include: [ | ||
67 | { | ||
68 | model: AccountModel, | ||
69 | required: true | ||
70 | } | ||
71 | ], | ||
72 | transaction: t | ||
73 | } | ||
74 | |||
75 | return VideoShareModel.findAll(query) | ||
76 | .then(res => res.map(r => r.Account)) | ||
77 | } | ||
84 | } | 78 | } |