Skip to content

Commit

Permalink
feat: blog
Browse files Browse the repository at this point in the history
  • Loading branch information
hubcarl committed Nov 8, 2019
1 parent 1c8f213 commit 0b74a62
Show file tree
Hide file tree
Showing 72 changed files with 7,120 additions and 10,469 deletions.
34 changes: 34 additions & 0 deletions app/controller/admin/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';
const egg = require('egg');
module.exports = class AdminController extends egg.Controller {
async login(ctx) {
await ctx.renderClient('admin/login/login.js', {});
}
async home(ctx) {
const url = ctx.url.replace(/\/admin/, '');
const { mode } = ctx.query;
if (mode === 'csr') {
await ctx.renderClient('admin.js', { ctx, url, title: 'easy-admin' });
} else {
await ctx.render('admin.js', { ctx, url, title: 'easy-admin' });
}
}

async list(ctx) {
this.ctx.body = ctx.service.article.getArtilceList(ctx.request.body);
}

async add(ctx) {
ctx.body = this.service.article.saveArticle(ctx.request.body);
}

async del(ctx) {
const { id } = ctx.params;
ctx.body = this.service.article.deleteArticle(id);
}

async detail(ctx) {
const id = ctx.params.id;
ctx.body = this.service.article.getArticle(Number(id));
}
};
8 changes: 8 additions & 0 deletions app/controller/category/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
module.exports = app => {
return class CategoryController extends app.Controller {
async index(ctx) {
await ctx.render('category.js', { message: 'Egg React Server Side Render: Category' });
}
};
};
31 changes: 0 additions & 31 deletions app/controller/home.js

This file was deleted.

21 changes: 21 additions & 0 deletions app/controller/home/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'usestrict';
const egg = require('egg');
module.exports = class IndexController extends egg.Controller {

async ssr(ctx) {
await ctx.render('home.js', { url: ctx.url });
}

async csr(ctx) {
const result = this.service.article.getArtilceList();
await ctx.renderClient('home.js', result);
}

async list() {
this.ctx.body = this.service.article.getArtilceList();
}

async detail() {
const id = this.ctx.query.id;
}
};
11 changes: 11 additions & 0 deletions app/extend/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
const Factory = require('../lib/db/factory');
const DBSymbol = Symbol('Application#db');
module.exports = {
get db() {
if (!this[DBSymbol]) {
this[DBSymbol] = Factory();
}
return this[DBSymbol];
},
};
6 changes: 6 additions & 0 deletions app/extend/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';
module.exports = {
get db() {
return this.app.db;
}
};
25 changes: 25 additions & 0 deletions app/lib/db/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';
const shortid = require('shortid');
module.exports = class DB {
constructor(name = 'blog.json') {
this.name = name;
}
getUniqueId() {
return shortid.generate();
}
get(collectionName) {
return null;
}
add(collectionName, json) {
return null;
}
update(collectionName, where, json) {
return null;
}
delete(collectionName, field) {
return null;
}
getPager(collectionName, query) {
return null;
}
};
31 changes: 31 additions & 0 deletions app/lib/db/collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
module.exports = class Collection {
constructor(db, name) {
this.db = db;
this.name = name;
}
get() {
return this.db.get(this.name);
}
getByWhere(json) {
return this.db.get(this.name).find(json);
}
add(json) {
return this.db.add(this.name, json);
}
update(where, json) {
return this.db.update(this.name, where, json);
}
delete(field) {
return this.db.delete(this.name, field);
}
getPager(query) {
return this.db.getPager(this.name, query);
}
getOrderAscByField(field) {
return this.get().orderBy(field, 'asc').value();
}
getOrderDescByField(field) {
return this.get().orderBy(field, 'desc').value();
}
};
14 changes: 14 additions & 0 deletions app/lib/db/factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
const File = require('./file');
const MySQL = require('./mysql');
const MongoDB = require('./mongo');
module.exports = type => {
switch (type) {
case 'mysql':
return new MySQL();
case 'mongo':
return new MongoDB();
default:
return new File();
}
};
60 changes: 60 additions & 0 deletions app/lib/db/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';
const lowdb = require('lowdb');
const lodashid = require('lodash-id');
const FileSync = require('lowdb/adapters/FileSync');
const Base = require('./base');
module.exports = class FileDB extends Base {
constructor(name) {
super(name);
const file = new FileSync(this.name);
this.instance = lowdb(file);
this.instance._.mixin(lodashid);
this.create();
}
create() {
this.instance.defaults({
article: [],
user: {}
}).write();
}
get(collectionName) {
return this.instance.get(collectionName);
}
add(collectionName, json) {
return this.get(collectionName)
.push(json)
.write();
}
update(collectionName, where, json) {
return this.get(collectionName).find(where).assign(json).write();
}
delete(collectionName, field) {
return this.get(collectionName).remove(field).write();
}
getPager(collectionName, json) {
const {
where,
like,
pageIndex,
pageSize,
orderByField,
orderBy
} = json;
const start = (pageIndex - 1) * pageSize;
const end = pageIndex * pageSize;
const result = this.get(collectionName)
.filter(where)
.filter(item => {
return Object.keys(like).reduce((isLike, key) => {
return isLike && item[key] && item[key].indexOf(like[key]) > -1;
}, true);
})
.orderBy(orderByField, orderBy);
const total = result.size().value();
const list = result.slice(start, end).value();
return {
total,
list
};
}
};
3 changes: 3 additions & 0 deletions app/lib/db/mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';
const Base = require('./base');
module.exports = class MongoDB extends Base {};
3 changes: 3 additions & 0 deletions app/lib/db/mysql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';
const Base = require('./base');
module.exports = class MySQLDB extends Base {};
44 changes: 44 additions & 0 deletions app/lib/db/query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';
module.exports = class Query {
constructor() {
this.orderByField = 'createTime';
this.orderBy = 'desc';
this._pageIndex = 1;
this._pageSize = 10;
this.where = new Proxy({}, {
set(target, key, value, proxy) {
if (value === undefined) {
return true;
}
return Reflect.set(target, key, value, proxy);
}
});

this.like = new Proxy({}, {
set(target, key, value, proxy) {
if (value === undefined) {
return true;
}
return Reflect.set(target, key, value, proxy);
}
});
}

get pageIndex() {
return this._pageIndex;
}
set pageIndex(value) {
if (value !== undefined) {
this._pageIndex = value;
}
}

get pageSize() {
return this._pageSize;
}
set pageSize(value) {
if (value !== undefined) {
this._pageSize = value;
}
}
};
7 changes: 7 additions & 0 deletions app/middleware/locals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = () => {
return async function locale(ctx, next) {
ctx.locals.locale = ctx.query.locale || 'cn';
ctx.locals.origin = ctx.request.origin;
await next();
};
};
13 changes: 13 additions & 0 deletions app/model/article.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

module.exports = class Model {
constructor() {
super();
this.id = void 0;
this.title = undefined;
this.summary = undefined;
this.tag = undefined;
this.hits = 0;
this.createTime = Date.now();
}
};
2 changes: 2 additions & 0 deletions app/model/artilcedetail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
module.exports = class ArticleDetail {};
9 changes: 9 additions & 0 deletions app/model/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';
module.exports = class User {
constructor() {
this.id = null;
this.name = null;
this.password = null;
this.roleId = null;
}
};
7 changes: 7 additions & 0 deletions app/model/utils/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';
const Pagination = require('./pagination');
module.exports = class Model {
constructor() {
this.pagination = new Pagination();
}
};
16 changes: 11 additions & 5 deletions app/router.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@

module.exports = app => {
app.get('/', app.controller.home.index);
app.get('/about', app.controller.home.index);
app.get('/js', app.controller.home.js);
app.get('/jsx', app.controller.home.jsx);
app.get('/api/article/list', app.controller.home.pager);
const { router, controller } = app;
router.get('/', controller.home.index.ssr);
router.get('/csr', controller.home.index.csr);
router.get('/api/list', controller.home.index.list);
router.get('/category', controller.category.category.index);
router.get('/login', controller.admin.admin.login);
router.post('/admin/api/article/list', controller.admin.admin.list);
router.post('/admin/api/article/add', controller.admin.admin.add);
router.get('/admin/api/article/del/:id', controller.admin.admin.del);
router.get('/admin/api/article/:id', controller.admin.admin.detail);
router.get('/admin(/.+)?', controller.admin.admin.home);
};
Loading

0 comments on commit 0b74a62

Please sign in to comment.