Skip to content

Commit

Permalink
fix: type annotations in controllers mostly
Browse files Browse the repository at this point in the history
  • Loading branch information
vinitkumar committed Jan 3, 2021
1 parent 3e566f4 commit 7658f95
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 68 deletions.
20 changes: 10 additions & 10 deletions src/controllers/apiv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ const User = mongoose.model("User");
import {Request, Response} from "express";

exports.tweetList = (req: Request, res: Response) => {
const page = (req.query.page > 0 ? req.query.page : 1) - 1;
const perPage = 15;
const page: number = (req.query.page > 0 ? req.query.page : 1) - 1;
const perPage: number = 15;
const options = {
perPage: perPage,
page: page
};
let tweets, count;
let tweets: Array<typeof Tweet>, count: number;
Tweet.limitedList(options)
.then(result => {
.then(function (result: any) {
tweets = result;
return Tweet.countDocuments();
})
.then(result => {
.then(function (result: any) {
count = result;
return res.send(tweets);
})
.catch(error => {
.catch(function (error: any) {
return res.render("pages/500", { errors: error.errors });
});
};
Expand All @@ -34,17 +34,17 @@ exports.usersList = (req: Request, res: Response) => {
perPage: perPage,
page: page
};
let users, count;
let users: Array<typeof User>, count: any;
User.list(options)
.then(result => {
.then(function (result: any) {
users = result;
return User.countDocuments();
})
.then(result => {
.then(function (result: any) {
count = result;
return res.send(users);
})
.catch(error => {
.catch(function (error: any) {
return res.render("pages/500", { errors: error.errors });
});
};
20 changes: 10 additions & 10 deletions src/controllers/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const logger = require("../middlewares/logger");
import {Request, Response, NextFunction} from "express";

exports.chat = (req: Request, res: Response, next: NextFunction, id) => {
Chat.load(id, (err, chat) => {
Chat.load(id, (err: mongoose.Error, chat: typeof Chat) => {
if (err) {
return next(err);
}
Expand All @@ -21,20 +21,20 @@ exports.chat = (req: Request, res: Response, next: NextFunction, id) => {

exports.index = (req: Request, res: Response) => {
// so basically this is going to be a list of all chats the user had till date.
const page = (req.query.page > 0 ? req.query.page : 1) - 1;
const perPage = 10;
const page: number = (req.query.page > 0 ? req.query.page : 1) - 1;
const perPage: number = 10;
const options = {
perPage: perPage,
page: page,
criteria: { github: { $exists: true } }
};
let users, count, pagination;
let users: Array<typeof User>, count, pagination;
User.list(options)
.then(result => {
.then(function (result: any){
users = result;
return User.countDocuments()
})
.then(result => {
.then(function (result: any){
count = result;
pagination = createPagination(req, Math.ceil(result / perPage), page + 1);
res.render("chat/index", {
Expand All @@ -45,7 +45,7 @@ exports.index = (req: Request, res: Response) => {
pages: Math.ceil(count / perPage)
});
})
.catch(error => {
.catch(function (error: any){
return res.render("pages/500", { errors: error.errors });
});
};
Expand All @@ -59,7 +59,7 @@ exports.getChat = (req: Request, res: Response) => {
criteria: { receiver: req.params.userid }
};
let chats;
Chat.list(options).then(result => {
Chat.list(options).then(function (result: any){
chats = result;
res.render("chat/chat", { chats: chats });
});
Expand All @@ -72,14 +72,14 @@ exports.create = (req: Request, res: Response) => {
sender: req.user.id
});
logger.info("chat instance", chat);
chat.save(err => {
chat.save(function (err: mongoose.Error){
const activity = new Activity({
activityStream: "sent a message to",
activityKey: chat.id,
receiver: req.body.receiver,
sender: req.user.id
});
activity.save(err => {
activity.save(function (err: mongoose.Error){
if (err) {
logger.error(err);
res.render("pages/500");
Expand Down
9 changes: 5 additions & 4 deletions src/controllers/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
const utils = require("../../lib/utils");
import mongoose from "mongoose";
const Activity = mongoose.model("Activity");
const Comment = mongoose.model("Comment");
const logger = require("../middlewares/logger");

import {Request, Response} from "express";
import {Request, Response, NextFunction} from "express";

exports.load = (req: Request, res: Response, next, id) => {
exports.load = (req: Request, res: Response, next: NextFunction, id: string) => {
const tweet = req.tweet;
utils.findByParam(tweet.comments, { id: id }, (err, comment) => {
utils.findByParam(tweet.comments, { id: id }, (err: mongoose.Error, comment: typeof Comment) => {
if (err) {
return next(err);
}
Expand Down Expand Up @@ -51,7 +52,7 @@ exports.create = (req: Request, res: Response) => {
exports.destroy = (req: Request, res: Response) => {
// delete a comment here.
const comment = req.comment;
comment.remove(err => {
comment.remove(function (err: mongoose.Error) {
if (err) {
res.send(400);
}
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/follows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ exports.follow = (req: Request, res: Response) => {

const currentId = user.id;

User.findOne({ _id: id }, function(err, user) {
User.findOne({ _id: id }, function(err: mongoose.Error, user: typeof User) {
if (user.followers.indexOf(currentId) === -1) {
user.followers.push(currentId);
}
user.save(err => {
user.save(function (err) {
if (err) {
logger.error(err);
}
Expand All @@ -25,19 +25,19 @@ exports.follow = (req: Request, res: Response) => {
// Over here, we find the id of the user we want to follow
// and add the user to the following list of the current
// logged in user
User.findOne({ _id: currentId }, function(err, user) {
User.findOne({ _id: currentId }, function(err: mongoose.Error, user: typeof User) {
if (user.following.indexOf(id) === -1) {
user.following.push(id);
}
user.save(err => {
user.save(function (err: mongoose.Error) {
const activity = new Activity({
activityStream: "followed by",
activityKey: user,
sender: currentId,
receiver: user
});

activity.save(err => {
activity.save(function (err: mongoose.Error) {
if (err) {
logger.error(err);
res.render("pages/500");
Expand Down
30 changes: 15 additions & 15 deletions src/controllers/tweets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const Analytics = mongoose.model("Analytics");
const _ = require("underscore");
const logger = require("../middlewares/logger");

import { Request, Response } from "express";
import { Request, Response, NextFunction } from "express";

exports.tweet = (req:Request, res:Response, next, id) => {
Tweet.load(id, (err, tweet) => {
exports.tweet = (req:Request, res:Response, next: NextFunction, id: string) => {
Tweet.load(id, (err: mongoose.Error, tweet: typeof Tweet) => {
if (err) {
return next(err);
}
Expand Down Expand Up @@ -41,7 +41,7 @@ exports.create = (req:Request, res:Response) => {
exports.update = (req:Request, res:Response) => {
let tweet = req.tweet;
tweet = _.extend(tweet, { body: req.body.tweet });
tweet.uploadAndSave({}, err => {
tweet.uploadAndSave({}, function (err: mongoose.Error) {
if (err) {
return res.render("pages/500", { error: err });
}
Expand All @@ -52,7 +52,7 @@ exports.update = (req:Request, res:Response) => {
// ### Delete a tweet
exports.destroy = (req:Request, res:Response) => {
const tweet = req.tweet;
tweet.remove(err => {
tweet.remove(function (err: mongoose.Error) {
if (err) {
return res.render("pages/500");
}
Expand All @@ -62,7 +62,7 @@ exports.destroy = (req:Request, res:Response) => {

// ### Parse a hashtag

function parseHashtag(inputText) {
function parseHashtag(inputText: string) {
var regex = /(?:^|\s)(?:#)([a-zA-Z\d]+)/g;
var matches = [];
var match;
Expand All @@ -74,9 +74,9 @@ function parseHashtag(inputText) {

exports.parseHashtag = parseHashtag;

let showTweets = (req:Request, res:Response, criteria) => {
let showTweets = (req:Request, res:Response, criteria: any) => {
const findCriteria = criteria || {};
const page = (req.query.page > 0 ? req.query.page : 1) - 1;
const page: number = (req.query.page > 0 ? req.query.page : 1) - 1;
const perPage = 10;
const options = {
perPage: perPage,
Expand All @@ -85,16 +85,16 @@ let showTweets = (req:Request, res:Response, criteria) => {
};
let followingCount = req.user.following.length;
let followerCount = req.user.followers.length;
let tweets, tweetCount, pageViews, analytics, pagination;
User.countUserTweets(req.user._id).then(result => {
let tweets: Array<typeof Tweet>, tweetCount: number, pageViews: number, analytics: Array<typeof Analytics>, pagination: number;
User.countUserTweets(req.user._id).then(function (result: any) {
tweetCount = result;
});
Tweet.list(options)
.then(result => {
.then(function (result: any) {
tweets = result;
return Tweet.countTweets(findCriteria);
})
.then(result => {
.then(function (result: any) {
pageViews = result;
pagination = createPagination(
req,
Expand All @@ -103,7 +103,7 @@ let showTweets = (req:Request, res:Response, criteria) => {
);
return Analytics.list({ perPage: 15 });
})
.then(result => {
.then(function (result: any) {
analytics = result;
res.render("pages/index", {
title: "List of Tweets",
Expand All @@ -117,7 +117,7 @@ let showTweets = (req:Request, res:Response, criteria) => {
pages: Math.ceil(pageViews / perPage)
});
})
.catch(error => {
.catch(function (error: any) {
logger.error(error);
res.render("pages/500");
});
Expand All @@ -130,5 +130,5 @@ exports.findTag = (req:Request, res:Response) => {
};

exports.index = (req:Request, res:Response) => {
showTweets(req, res);
showTweets(req, res, {});
};
Loading

0 comments on commit 7658f95

Please sign in to comment.