Skip to content

Commit

Permalink
WHAT: Refactor
Browse files Browse the repository at this point in the history
WHY:

* XXXXX

HOW:

* NOTHING TO SAY
  • Loading branch information
ecmadao committed Aug 5, 2018
1 parent 79a86e9 commit a47bb65
Show file tree
Hide file tree
Showing 18 changed files with 438 additions and 496 deletions.
38 changes: 18 additions & 20 deletions app/controllers/github.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* eslint eqeqeq: "off" */
import config from 'config';
import GitHubAPI from '../services/github';
import network from '../services/network';
import { combineReposCommits } from './helper/github';
import { UPDATE_STATUS_TEXT } from '../utils/constant';
import { is, sortBy } from '../utils/helper';
import UserAPI from '../services/user';
import StatAPI from '../services/stat';
import logger from '../utils/logger';

const services = config.get('services.github');
Expand All @@ -14,27 +12,27 @@ const services = config.get('services.github');

const _getUser = async (ctx) => {
const { login } = ctx.params;
const user = await GitHubAPI.getUser(login);
const user = await network.github.getUser(login);
if (!user) {
return ctx.redirect('/404');
}
return user;
};

const _getRepositories = async (login, token) => {
const repositories = await GitHubAPI.getUserRepositories(login, token);
const repositories = await network.github.getUserRepositories(login, token);
repositories.sort(sortBy.star);
return repositories;
};

const _getContributed = async (login, token) => {
const repos = await GitHubAPI.getUserContributed(login, token);
const repos = await network.github.getUserContributed(login, token);
repos.sort(sortBy.star);
return repos;
};

const _getCommits = async (login, token) => {
const commits = await GitHubAPI.getUserCommits(login, token);
const commits = await network.github.getUserCommits(login, token);
const formatCommits = combineReposCommits(commits);
commits.sort(sortBy.x('totalCommits', parseInt));

Expand All @@ -50,7 +48,7 @@ const toggleShare = async (ctx) => {
const { userId } = ctx.session;
const { enable } = ctx.request.body;

await UserAPI.updateUser(userId, { githubShare: Boolean(enable) });
await network.user.updateUser(userId, { githubShare: Boolean(enable) });
const message = Boolean(enable) == true
? 'messages.share.toggleOpen'
: 'messages.share.toggleClose'
Expand All @@ -62,7 +60,7 @@ const toggleShare = async (ctx) => {

const getAllRepositories = async (ctx, next) => {
const { githubLogin, githubToken } = ctx.session;
const repos = await GitHubAPI.getUserRepositories(githubLogin, githubToken);
const repos = await network.github.getUserRepositories(githubLogin, githubToken);
const result = [];

for (const repository of repos) {
Expand Down Expand Up @@ -132,7 +130,7 @@ const getUserLanguages = async (ctx, next) => {
const { login } = ctx.params;
const { githubToken } = ctx.session;

const languages = await GitHubAPI.getUserLanguages(login, githubToken);
const languages = await network.github.getUserLanguages(login, githubToken);
ctx.body = {
success: true,
result: languages
Expand All @@ -144,7 +142,7 @@ const getUserOrganizations = async (ctx, next) => {
const { login } = ctx.params;
const { githubToken } = ctx.session;
const organizations =
await GitHubAPI.getUserOrganizations(login, githubToken);
await network.github.getUserOrganizations(login, githubToken);

ctx.body = {
success: true,
Expand All @@ -157,7 +155,7 @@ const getUser = async (ctx, next) => {
const { login } = ctx.params;
const [user, userInfo] = await Promise.all([
_getUser(ctx),
UserAPI.getUser({ login })
network.user.getUser({ login })
]);

const result = Object.assign({}, user);
Expand Down Expand Up @@ -194,15 +192,15 @@ const getShareRecords = async (ctx) => {

let records = [];
try {
records = await StatAPI.getRecords({
records = await network.stat.getRecords({
login: githubLogin,
type: 'github'
});
} catch (e) {
logger.error(e);
}

const userInfo = await UserAPI.getUser({ login: githubLogin });
const userInfo = await network.user.getUser({ login: githubLogin });

const viewDevices = [];
const viewSources = [];
Expand Down Expand Up @@ -233,7 +231,7 @@ const refreshEnable = (status, lastUpdate) =>

const getUpdateStatus = async (ctx) => {
const { githubLogin, userId } = ctx.session;
const statusResult = await GitHubAPI.getUpdateStatus(githubLogin);
const statusResult = await network.github.getUpdateStatus(githubLogin);
logger.info(`${githubLogin} update status: ${JSON.stringify(statusResult)}`);
const {
status,
Expand All @@ -242,7 +240,7 @@ const getUpdateStatus = async (ctx) => {

const statusCode = parseInt(status, 10);
if (statusCode === 1) {
await UserAPI.updateUser(userId, { initialed: true });
await network.user.updateUser(userId, { initialed: true });
}
const messageKey = UPDATE_STATUS_TEXT[statusCode];

Expand All @@ -262,7 +260,7 @@ const getUpdateStatus = async (ctx) => {

const updateUserData = async (ctx) => {
const { githubToken, githubLogin } = ctx.session;
await GitHubAPI.updateUserData(githubLogin, githubToken);
await network.github.updateUserData(githubLogin, githubToken);

ctx.body = {
success: true,
Expand All @@ -272,7 +270,7 @@ const updateUserData = async (ctx) => {

const getZen = async (ctx) => {
const { githubToken } = ctx.session;
const val = await GitHubAPI.getZen(githubToken);
const val = await network.github.getZen(githubToken);
const result = is.object(val) ? '' : val;

ctx.body = {
Expand All @@ -282,7 +280,7 @@ const getZen = async (ctx) => {
};

const getOctocat = async (ctx) => {
const result = await GitHubAPI.getOctocat();
const result = await network.github.getOctocat();
ctx.body = {
result,
success: true,
Expand All @@ -292,7 +290,7 @@ const getOctocat = async (ctx) => {
const getUserHotmap = async (ctx, next) => {
const { login } = ctx.params;
const { locale } = ctx.session;
const result = await GitHubAPI.getHotmap(login, locale);
const result = await network.github.getHotmap(login, locale);

ctx.body = {
result,
Expand Down
13 changes: 6 additions & 7 deletions app/controllers/helper/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import logger from '../../utils/logger';
import notify from '../../services/notify';
import { getValue } from '../../utils/helper';
import UserAPI from '../../services/user';
import StatAPI from '../../services/stat';
import network from '../../services/network';

const updateViewData = async (ctx, options) => {
const { platform, browser, device } = ctx.state;
Expand All @@ -12,14 +11,14 @@ const updateViewData = async (ctx, options) => {
login = null,
} = options;

await StatAPI.putRecords({
await network.stat.putRecords({
type,
login,
platform,
browser: browser || '',
});
if (type) {
await StatAPI.putStat({
await network.stat.putStat({
type,
action: 'pageview'
});
Expand Down Expand Up @@ -51,10 +50,10 @@ const getUser = async (ctx, source) => {

let user;
if (key === 'hash') {
const resumeInfo = await UserAPI.getResumeInfo({ hash: value });
user = await UserAPI.getUser({ userId: resumeInfo.userId });
const resumeInfo = await network.user.getResumeInfo({ hash: value });
user = await network.user.getUser({ userId: resumeInfo.userId });
} else {
user = await UserAPI.getUser({ [key]: value });
user = await network.user.getUser({ [key]: value });
}

return user;
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/helper/share.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

import { getValue } from '../../utils/helper';
import UserAPI from '../../services/user';
import network from '../../services/network';

const githubEnable = (key = 'params.login') => async (ctx, next) => {
const login = getValue(ctx, key);
const { githubLogin } = ctx.session;
const user = await UserAPI.getUser({ login });
const user = await network.user.getUser({ login });

const userLogin = user.githubLogin;
if (userLogin !== login || (!user.githubShare && userLogin !== githubLogin)) {
Expand Down Expand Up @@ -38,7 +38,7 @@ const resumeParamsFormatter = async (ctx, source) => {
const value = getValue(ctx, source);

if (key === 'login') {
const user = await UserAPI.getUser({ login: value });
const user = await network.user.getUser({ login: value });
return {
userId: user.userId
};
Expand All @@ -54,7 +54,7 @@ const resumeEnable = (source = 'params.login') => async (ctx, next) => {
const { userId } = ctx.session;

const qs = await resumeParamsFormatter(ctx, source);
const resumeInfo = await UserAPI.getResumeInfo(qs);
const resumeInfo = await network.user.getResumeInfo(qs);

if (
(!resumeInfo
Expand Down
16 changes: 7 additions & 9 deletions app/controllers/home.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@

import GitHubAPI from '../services/github';
import network from '../services/network';
import getLanguages from '../config/languages';
import logger from '../utils/logger';
import UserAPI from '../services/user';
import StatAPI from '../services/stat';
import notify from '../services/notify';

const landingPage = async (ctx) => {
const { locale } = ctx.state;
const languages = getLanguages(locale);
const clientId = await GitHubAPI.getVerify();
const clientId = await network.github.getVerify();

await ctx.render('user/login', {
clientId,
Expand Down Expand Up @@ -42,7 +40,7 @@ const dashboard = async (ctx) => {
const { device, browser, platform } = ctx.state;
const { githubLogin, userId } = ctx.session;
const { login, dashboardRoute = 'visualize' } = ctx.params;
const user = await UserAPI.getUser({ userId });
const user = await network.user.getUser({ userId });

logger.debug(`githubLogin: ${githubLogin}, userId: ${userId}`);
logger.debug(user);
Expand Down Expand Up @@ -71,7 +69,7 @@ const dashboard = async (ctx) => {
const initial = async (ctx) => {
const { githubLogin, userId } = ctx.session;
logger.debug(`githubLogin: ${githubLogin}, userId: ${userId}`);
const user = await UserAPI.getUser({ userId });
const user = await network.user.getUser({ userId });

logger.debug(user);

Expand All @@ -96,9 +94,9 @@ const statistic = async (ctx) => {
githubFields,
resumeFields
] = await Promise.all([
UserAPI.getUserCount(),
StatAPI.getStat({ type: 'github' }),
StatAPI.getStat({ type: 'resume' })
network.user.getUserCount(),
network.stat.getStat({ type: 'github' }),
network.stat.getStat({ type: 'resume' })
]);

const github = combineStat(githubFields || []);
Expand Down
Loading

0 comments on commit a47bb65

Please sign in to comment.