Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

formating, removed mongoose as a req for the api since its bundled in…

… lib
  • Loading branch information...
commit ee33ef3d4e087080f2711e3779c2fbe0dae0ba3d 1 parent 002c634
Edward Hotchkiss authored
Showing with 41 additions and 44 deletions.
  1. +0 −1  examples/api.js
  2. +0 −2  lib/short.js
  3. +41 −41 models/ShortURL.js
View
1  examples/api.js
@@ -1,5 +1,4 @@
-var mongoose = require("mongoose");
var short = require("../lib/short");
short.connect("mongodb://localhost/short");
View
2  lib/short.js
@@ -1,11 +1,9 @@
-// core
var mongoose = require("mongoose");
var base = require("base-converter");
var ShortURL = require(__dirname + "/../models/ShortURL.js");
-// setup base62 hash
function hasher(URL) {
var id = Math.floor(Math.random() * (100000 - 9999999 + 1) + 9999999);
var hash = base.decTo62(id);
View
82 models/ShortURL.js
@@ -11,11 +11,11 @@ var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var shortURL_schema = {
- id : ObjectId,
- URL : String,
- hash : { type : String },
- hits : { type : Number, default: 0 },
- created_at : { type : Date, default: Date.now }
+ id : ObjectId,
+ URL : String,
+ hash : { type : String },
+ hits : { type : Number, default: 0 },
+ created_at : { type : Date, default: Date.now }
};
var ShortURLSchema = new Schema(shortURL_schema);
@@ -23,50 +23,50 @@ var ShortURL = mongoose.model("ShortURL", ShortURLSchema);
// return count, if it exists, try another
ShortURL.checkExists = function(hash, callback) {
- var query = ShortURL.find({});
- query.where("hash", hash);
- query.exec(function(error, shortenedURLS) {
- if (error) {
- callback(error, null);
- } else {
- callback(null, shortenedURLS);
- };
- });
+ var query = ShortURL.find({});
+ query.where("hash", hash);
+ query.exec(function(error, shortenedURLS) {
+ if (error) {
+ callback(error, null);
+ } else {
+ callback(null, shortenedURLS);
+ };
+ });
};
// search for a URL by its hash
ShortURL.findByHash = function(hash, callback) {
- ShortURL.find({ hash: hash }, function(error, URL) {
- if (error) {
- callback(error, null);
- } else {
- if (URL.length !== 0) {
- var id = URL[0]._id;
- ShortURL.updateHitsById(id, function(error) {
- if (error) {
- callback(error, null);
- } else {
- callback(null, URL);
- };
- });
- } else {
- callback(null, null);
- }
- };
- });
+ ShortURL.find({ hash: hash }, function(error, URL) {
+ if (error) {
+ callback(error, null);
+ } else {
+ if (URL.length !== 0) {
+ var id = URL[0]._id;
+ ShortURL.updateHitsById(id, function(error) {
+ if (error) {
+ callback(error, null);
+ } else {
+ callback(null, URL);
+ };
+ });
+ } else {
+ callback(null, null);
+ }
+ };
+ });
};
// increment hits
ShortURL.updateHitsById = function(id, callback) {
- ShortURL.findById(id, function (error, URL) {
- var hits = URL.hits + 1;
- if (!error) {
- URL.hits = hits;
- URL.save(function(error) {
- callback(error);
- });
- }
- });
+ ShortURL.findById(id, function (error, URL) {
+ var hits = URL.hits + 1;
+ if (!error) {
+ URL.hits = hits;
+ URL.save(function(error) {
+ callback(error);
+ });
+ }
+ });
};
module.exports = ShortURL;
Please sign in to comment.
Something went wrong with that request. Please try again.