updated to make index page working

This commit is contained in:
tmshv committed 2019-10-15 00:16:55 +03:00
1 parent 7363d7c0ec
commit 5831275b7a
11 files changed
+160 -149

No files matched your search

+1
View File
@@ -7,6 +7,7 @@
"scripts": { "scripts": {
"start": "NODE_ENV=production node src/server.js", "start": "NODE_ENV=production node src/server.js",
"dev": "ts-node --project server.tsconfig.json src/server.ts", "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", "build": "next build && next export -o dist && npm run sitemap && npm run feed && npm run robots-txt",
"robots-txt": "node scripts/robots-txt.js", "robots-txt": "node scripts/robots-txt.js",
"sitemap": "node scripts/sitemap.js", "sitemap": "node scripts/sitemap.js",
-21
View File
@@ -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`
})
}
})
+19
View File
@@ -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`
})
}
})
+5 -7
View File
@@ -1,10 +1,9 @@
import Data from './Data' import Data from './Data'
import Image from './Image' import Image from './Image'
import Config from './Config' import Config from './Config'
import { findOne, total } from '../lib/store' import { find, findOne, total } from '../lib/store'
import * as libStore from '../lib/store'
const store = () => Data.getStore(Article) const store = () => Data.getStore('Article')
function make(C) { function make(C) {
return x => new C(x) return x => new C(x)
@@ -12,10 +11,9 @@ function make(C) {
export default class Article { export default class Article {
static async find(query, options = {}): Promise<Article[]> { static async find(query, options = {}): Promise<Article[]> {
return [] const items = await find(store(), query, options as any)
// const items = await libStore.find(store(), query, options as any)
// return Promise.all(items.map(processArticle)) return Promise.all(items.map(processArticle))
} }
static async findById(id) { static async findById(id) {
@@ -126,7 +124,7 @@ async function processArticle(article) {
if (previewId) { if (previewId) {
preview = await Image.findById(previewId) preview = await Image.findById(previewId)
} else { } else {
preview = await resolveDefaultPreview() // preview = await resolveDefaultPreview()
} }
return new Article({ return new Article({
-37
View File
@@ -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
+44
View File
@@ -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)
)
}
}
-75
View File
@@ -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
+81
View File
@@ -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
}
}
+8 -7
View File
@@ -1,11 +1,11 @@
import Data from './Data' import Data from './Data'
const Page = require('./Page') import Page from './Page'
const Article = require('./Article') import Article from './Article'
const Album = require('./Album') const Album = require('./Album')
const Teacher = require('./Teacher') const Teacher = require('./Teacher')
const Document = require('./Document') const Document = require('./Document')
const Image = require('./Image') const Image = require('./Image')
const Settings = require('./Config') import Settings from './Config'
const getConfig = require('next/config').default const getConfig = require('next/config').default
const { MongoClient, ObjectId } = require('mongodb') const { MongoClient, ObjectId } = require('mongodb')
@@ -34,11 +34,12 @@ export function id(i) {
} }
function init(db) { function init(db) {
// Data.setStore('Page', db.collection('pages')) console.log('INIT DB')
Data.setStore(Page, db.collection('pages'))
Data.setStore(Article, db.collection('articles')) Data.setStore('Page', db.collection('pages'))
Data.setStore('Article', db.collection('articles'))
Data.setStore(Album, db.collection('albums')) 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(Teacher, db.collection('collective'))
Data.setStore(Document, db.collection('documents')) Data.setStore(Document, db.collection('documents'))
Data.setStore(Settings, db.collection('settings')) Data.setStore(Settings, db.collection('settings'))
+1 -1
View File
@@ -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 limit = pageSize
const skip = (page - 1) * pageSize const skip = (page - 1) * pageSize
+1 -1
View File
@@ -5,7 +5,7 @@
* @param Class * @param Class
* @param object * @param object
*/ */
exports.mapOf = function mapOf(Class, object) { export function mapOf(Class, object) {
return Object return Object
.entries(object) .entries(object)
.reduce((map, i) => ( .reduce((map, i) => (