diff options
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/author.js | 29 | ||||
-rw-r--r-- | server/models/tag.js | 40 |
2 files changed, 67 insertions, 2 deletions
diff --git a/server/models/author.js b/server/models/author.js index 5835ada99..7d15fb6ec 100644 --- a/server/models/author.js +++ b/server/models/author.js | |||
@@ -29,7 +29,9 @@ module.exports = function (sequelize, DataTypes) { | |||
29 | } | 29 | } |
30 | ], | 30 | ], |
31 | classMethods: { | 31 | classMethods: { |
32 | associate | 32 | associate, |
33 | |||
34 | findOrCreateAuthor | ||
33 | } | 35 | } |
34 | } | 36 | } |
35 | ) | 37 | ) |
@@ -56,3 +58,28 @@ function associate (models) { | |||
56 | onDelete: 'cascade' | 58 | onDelete: 'cascade' |
57 | }) | 59 | }) |
58 | } | 60 | } |
61 | |||
62 | function findOrCreateAuthor (name, podId, userId, transaction, callback) { | ||
63 | if (!callback) { | ||
64 | callback = transaction | ||
65 | transaction = null | ||
66 | } | ||
67 | |||
68 | const author = { | ||
69 | name, | ||
70 | podId, | ||
71 | userId | ||
72 | } | ||
73 | |||
74 | const query = { | ||
75 | where: author, | ||
76 | defaults: author | ||
77 | } | ||
78 | |||
79 | if (transaction) query.transaction = transaction | ||
80 | |||
81 | this.findOrCreate(query).asCallback(function (err, result) { | ||
82 | // [ instance, wasCreated ] | ||
83 | return callback(err, result[0]) | ||
84 | }) | ||
85 | } | ||
diff --git a/server/models/tag.js b/server/models/tag.js index 27eecdc84..145e090c1 100644 --- a/server/models/tag.js +++ b/server/models/tag.js | |||
@@ -1,5 +1,7 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const each = require('async/each') | ||
4 | |||
3 | // --------------------------------------------------------------------------- | 5 | // --------------------------------------------------------------------------- |
4 | 6 | ||
5 | module.exports = function (sequelize, DataTypes) { | 7 | module.exports = function (sequelize, DataTypes) { |
@@ -19,7 +21,9 @@ module.exports = function (sequelize, DataTypes) { | |||
19 | } | 21 | } |
20 | ], | 22 | ], |
21 | classMethods: { | 23 | classMethods: { |
22 | associate | 24 | associate, |
25 | |||
26 | findOrCreateTags | ||
23 | } | 27 | } |
24 | } | 28 | } |
25 | ) | 29 | ) |
@@ -36,3 +40,37 @@ function associate (models) { | |||
36 | onDelete: 'cascade' | 40 | onDelete: 'cascade' |
37 | }) | 41 | }) |
38 | } | 42 | } |
43 | |||
44 | function findOrCreateTags (tags, transaction, callback) { | ||
45 | if (!callback) { | ||
46 | callback = transaction | ||
47 | transaction = null | ||
48 | } | ||
49 | |||
50 | const self = this | ||
51 | const tagInstances = [] | ||
52 | |||
53 | each(tags, function (tag, callbackEach) { | ||
54 | const query = { | ||
55 | where: { | ||
56 | name: tag | ||
57 | }, | ||
58 | defaults: { | ||
59 | name: tag | ||
60 | } | ||
61 | } | ||
62 | |||
63 | if (transaction) query.transaction = transaction | ||
64 | |||
65 | self.findOrCreate(query).asCallback(function (err, res) { | ||
66 | if (err) return callbackEach(err) | ||
67 | |||
68 | // res = [ tag, isCreated ] | ||
69 | const tag = res[0] | ||
70 | tagInstances.push(tag) | ||
71 | return callbackEach() | ||
72 | }) | ||
73 | }, function (err) { | ||
74 | return callback(err, tagInstances) | ||
75 | }) | ||
76 | } | ||