From 5831275b7a086f5fa00a8382d17d9cbf6ae2766d Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Tue, 15 Oct 2019 00:16:55 +0300 Subject: [PATCH] updated to make index page working --- package.json | 1 + pages/api/articles/index.js | 21 --------- pages/api/articles/index.ts | 19 ++++++++ src/core/Article.ts | 12 +++--- src/core/Config.js | 37 ---------------- src/core/Config.ts | 44 +++++++++++++++++++ src/core/Image.js | 75 -------------------------------- src/core/Image.ts | 81 +++++++++++++++++++++++++++++++++++ src/core/db.ts | 15 ++++--- src/server/articles.ts | 2 +- src/server/{lib.js => lib.ts} | 2 +- 11 files changed, 160 insertions(+), 149 deletions(-) delete mode 100644 pages/api/articles/index.js create mode 100644 pages/api/articles/index.ts delete mode 100644 src/core/Config.js create mode 100644 src/core/Config.ts delete mode 100644 src/core/Image.js create mode 100644 src/core/Image.ts rename src/server/{lib.js => lib.ts} (83%) diff --git a/package.json b/package.json index 069a7dd7..8a341663 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "scripts": { "start": "NODE_ENV=production node src/server.js", "dev": "ts-node --project server.tsconfig.json src/server.ts", + "next-dev": "next dev", "build": "next build && next export -o dist && npm run sitemap && npm run feed && npm run robots-txt", "robots-txt": "node scripts/robots-txt.js", "sitemap": "node scripts/sitemap.js", diff --git a/pages/api/articles/index.js b/pages/api/articles/index.js deleted file mode 100644 index 4a14b725..00000000 --- a/pages/api/articles/index.js +++ /dev/null @@ -1,21 +0,0 @@ -// import { findArticles } from '../../../src/api/articles' -import { withMiddleware } from '../../../src/middlewares/withMiddleware' -import Article from '../../../src/core/Article' - -export default withMiddleware(async (req, res) => { - // const { query } = req - // const page = parseInt(query.page) - // const pageSize = parseInt(query.pageSize) - - // const data = await findArticles(page, pageSize) - const data = await Article.find({}) - - if (data) { - res.json(data) - } else { - res.status(404) - res.json({ - error: `Article ${id} not found` - }) - } -}) diff --git a/pages/api/articles/index.ts b/pages/api/articles/index.ts new file mode 100644 index 00000000..3592a507 --- /dev/null +++ b/pages/api/articles/index.ts @@ -0,0 +1,19 @@ +import { findArticles } from '../../../src/server/articles' +import { withMiddleware } from '../../../src/middlewares/withMiddleware' + +export default withMiddleware(async (req, res) => { + const { query } = req + const page = parseInt(query.page) + const pageSize = parseInt(query.pageSize) + + const data = await findArticles(page, pageSize) + + if (data) { + res.json(data) + } else { + res.status(404) + res.json({ + error: `Articles not found` + }) + } +}) diff --git a/src/core/Article.ts b/src/core/Article.ts index 8f749d03..bb39d87d 100644 --- a/src/core/Article.ts +++ b/src/core/Article.ts @@ -1,10 +1,9 @@ import Data from './Data' import Image from './Image' import Config from './Config' -import { findOne, total } from '../lib/store' -import * as libStore from '../lib/store' +import { find, findOne, total } from '../lib/store' -const store = () => Data.getStore(Article) +const store = () => Data.getStore('Article') function make(C) { return x => new C(x) @@ -12,10 +11,9 @@ function make(C) { export default class Article { static async find(query, options = {}): Promise { - return [] - // const items = await libStore.find(store(), query, options as any) + const items = await find(store(), query, options as any) - // return Promise.all(items.map(processArticle)) + return Promise.all(items.map(processArticle)) } static async findById(id) { @@ -126,7 +124,7 @@ async function processArticle(article) { if (previewId) { preview = await Image.findById(previewId) } else { - preview = await resolveDefaultPreview() + // preview = await resolveDefaultPreview() } return new Article({ diff --git a/src/core/Config.js b/src/core/Config.js deleted file mode 100644 index bb900cc9..00000000 --- a/src/core/Config.js +++ /dev/null @@ -1,37 +0,0 @@ -const Data = require('./Data') -const {findOne} = require('../lib/store') - -class Config { - static get store() { - return Data.getStore(Config) - } - - static async findConfig() { - const config = await Config.findById('settings') - return config || new Config({}) - } - - static async findById(id) { - const data = await findOne(Config.store, {id}) - if (!data) return null - - return new Config(data) - } - - constructor({hash, file, ...data}) { - this.hash = hash - this.file = file - this.server = data.server - this.articlesPerPage = data.articlesPerPage || 5 - this.collectiveOrder = data.collectiveOrder || [] - this.articleCardDefaultPreview = data.articleCardDefaultPreview - this.collectiveImage = data.collectiveImage - - const redirect = data.redirect || {} - this.redirect = new Map(Object - .entries(redirect) - ) - } -} - -module.exports = Config diff --git a/src/core/Config.ts b/src/core/Config.ts new file mode 100644 index 00000000..dd6ca3f0 --- /dev/null +++ b/src/core/Config.ts @@ -0,0 +1,44 @@ +import Data from './Data' +import { findOne } from '../lib/store' + +export default class Config { + static get store() { + return Data.getStore('Settings') + } + + static async findConfig() { + const config = await Config.findById('settings') + return config || new Config({} as any) + } + + static async findById(id) { + const data = await findOne(Config.store, { id }) + if (!data) return null + + return new Config(data) + } + + public hash: any + public file: any + public server: any + public articlesPerPage: any + public collectiveOrder: any + public articleCardDefaultPreview: any + public collectiveImage: any + public redirect: any + + constructor({ hash, file, ...data }) { + this.hash = hash + this.file = file + this.server = data.server + this.articlesPerPage = data.articlesPerPage || 5 + this.collectiveOrder = data.collectiveOrder || [] + this.articleCardDefaultPreview = data.articleCardDefaultPreview + this.collectiveImage = data.collectiveImage + + const redirect = data.redirect || {} + this.redirect = new Map(Object + .entries(redirect) + ) + } +} diff --git a/src/core/Image.js b/src/core/Image.js deleted file mode 100644 index 2936f19e..00000000 --- a/src/core/Image.js +++ /dev/null @@ -1,75 +0,0 @@ -const Data = require('./Data') -const ImageArtifactType = require('./ImageArtifactType') -const {findOne} = require('../lib/store') -const { mapOf } = require('../server/lib') - -const store = () => Data.getStore(Image) - -class Image { - static async findById(id) { - const data = await findOne(store(), {_id: id}) - if (!data) return null - - const artifacts = data.data - return new Image({...data, artifacts}) - } - - static async findByFile(file) { - const data = await findOne(store(), {file}) - if (!data) return null - - const artifacts = data.data - return new Image({...data, artifacts}) - } - - constructor({hash, file, artifacts}) { - this.hash = hash - this.file = file - - this.artifacts = mapOf(ImageArtifact, artifacts) - } - - getArtifact(size) { - return this.artifacts.get(size) - } - - findArtifact(sizes) { - for (let size of sizes) { - const a = this.getArtifact(size) - if (a) return a - } - - return null - } - - getPicture(size) { - const retinaSize = ImageArtifactType.retina(size) - - const a = this.getArtifact(size) - const a2 = this.getArtifact(retinaSize) - const srcset = !a2 - ? [] - : [{ - url: a2.url, - density: ImageArtifactType.RETINA_DENSITY, - }] - - return { - src: a.url, - width: a.width, - height: a.height, - set: srcset, - } - } -} - -class ImageArtifact { - constructor({url, size, width, height}) { - this.url = url - this.size = size - this.width = width - this.height = height - } -} - -module.exports = Image diff --git a/src/core/Image.ts b/src/core/Image.ts new file mode 100644 index 00000000..79af86b4 --- /dev/null +++ b/src/core/Image.ts @@ -0,0 +1,81 @@ +import Data from './Data' +import ImageArtifactType from './ImageArtifactType' +import { findOne } from '../lib/store' +import { mapOf } from '../server/lib' + +const store = () => Data.getStore('Image') + +export default class Image { + static async findById(id) { + const data = await findOne(store(), { _id: id }) + if (!data) return null + + const artifacts = data.data + return new Image({ ...data, artifacts }) + } + + static async findByFile(file) { + const data = await findOne(store(), { file }) + if (!data) return null + + const artifacts = data.data + return new Image({ ...data, artifacts }) + } + public hash: any + public file: any + public artifacts: any + + constructor({ hash, file, artifacts }) { + this.hash = hash + this.file = file + + this.artifacts = mapOf(ImageArtifact, artifacts) + } + + getArtifact(size) { + return this.artifacts.get(size) + } + + findArtifact(sizes) { + for (let size of sizes) { + const a = this.getArtifact(size) + if (a) return a + } + + return null + } + + getPicture(size) { + const retinaSize = ImageArtifactType.retina(size) + + const a = this.getArtifact(size) + const a2 = this.getArtifact(retinaSize) + const srcset = !a2 + ? [] + : [{ + url: a2.url, + density: ImageArtifactType.RETINA_DENSITY, + }] + + return { + src: a.url, + width: a.width, + height: a.height, + set: srcset, + } + } +} + +class ImageArtifact { + public url: any + public size: any + public width: any + public height: any + + constructor({ url, size, width, height }) { + this.url = url + this.size = size + this.width = width + this.height = height + } +} diff --git a/src/core/db.ts b/src/core/db.ts index 40adf502..4d30ad8f 100644 --- a/src/core/db.ts +++ b/src/core/db.ts @@ -1,11 +1,11 @@ import Data from './Data' -const Page = require('./Page') -const Article = require('./Article') +import Page from './Page' +import Article from './Article' const Album = require('./Album') const Teacher = require('./Teacher') const Document = require('./Document') const Image = require('./Image') -const Settings = require('./Config') +import Settings from './Config' const getConfig = require('next/config').default const { MongoClient, ObjectId } = require('mongodb') @@ -34,11 +34,12 @@ export function id(i) { } function init(db) { - // Data.setStore('Page', db.collection('pages')) - Data.setStore(Page, db.collection('pages')) - Data.setStore(Article, db.collection('articles')) + console.log('INIT DB') + + Data.setStore('Page', db.collection('pages')) + Data.setStore('Article', db.collection('articles')) Data.setStore(Album, db.collection('albums')) - Data.setStore(Image, db.collection('images')) + Data.setStore('Image', db.collection('images')) Data.setStore(Teacher, db.collection('collective')) Data.setStore(Document, db.collection('documents')) Data.setStore(Settings, db.collection('settings')) diff --git a/src/server/articles.ts b/src/server/articles.ts index 5f1d8285..70131abe 100644 --- a/src/server/articles.ts +++ b/src/server/articles.ts @@ -20,7 +20,7 @@ export async function findArticlesNin(nin, skip, limit, sort) { }) } -export async function findArticles(page, pageSize) { +export async function findArticles(page: number, pageSize: number) { const limit = pageSize const skip = (page - 1) * pageSize diff --git a/src/server/lib.js b/src/server/lib.ts similarity index 83% rename from src/server/lib.js rename to src/server/lib.ts index 49261bd7..898a1080 100644 --- a/src/server/lib.js +++ b/src/server/lib.ts @@ -5,7 +5,7 @@ * @param Class * @param object */ -exports.mapOf = function mapOf(Class, object) { +export function mapOf(Class, object) { return Object .entries(object) .reduce((map, i) => (