Skip to content

Commit

Permalink
no new link genrated for same url for issue coding-blocks#33
Browse files Browse the repository at this point in the history
  • Loading branch information
arvindkalra committed May 15, 2018
1 parent 839b125 commit 50dfc2e
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions utils/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,44 @@ sequelize.sync(); //Normal case


module.exports = {
addUrl: function (code, longURL, alias, done, failed) {
if (!alias) {
URL.findOrCreate({
where: {
code: code
},
defaults: {
code: code,
codeStr: r.to64(code),
longURL: longURL
addUrl: function (code, longURL, alias, done, failed) {
if (!alias) {
URL.findOne({
where: {
longURL: longURL
}
}).then(function (result) {
if(!result){
URL.findOrCreate({
where: {
code: code
},
defaults: {
code: code,
codeStr: r.to64(code),
longURL: longURL
}
}).spread(function (url, created) {
done(url.codeStr, !created, url.longURL);
}).catch(function (error) {
console.log(error);
failed(error);
})
}
else {
done(result.codeStr, true, result.longURL);
}
}).catch(function (error) {
console.log(error);
failed(error);
})
} else {
//TODO: handle longer than 9 with alias map
}
},

}).spread(function (url, created) {
done(url.codeStr, !created, url.longURL);
}).catch(function (error) {
console.log(error);
failed(error);
})
} else {
//TODO: handle longer than 9 with alias map
}
},
fetchUrl: function (code, from, done, failed) {

fetchUrl: function (code, from, done, failed) {
URL.findById(code).then(function (url) {
done(url.longURL);
Event.create({
Expand Down

0 comments on commit 50dfc2e

Please sign in to comment.