-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec1bc01
commit f9da2f6
Showing
12 changed files
with
419 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
node_modules | ||
*.log | ||
public/* | ||
storage/logs | ||
storage/city | ||
storage/*/* | ||
*.xml | ||
apidoc | ||
www | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { join } from "path"; | ||
import { articlePicMediaPath } from "../config/config"; | ||
import { models } from "../models"; | ||
import { reloadPaths } from "../services/nextRenderService"; | ||
|
||
export async function getAllArticles(req, res, next) { | ||
try { | ||
const articles = await models.Article.findAll(); | ||
return res.status(200).send(articles); | ||
} catch (e) { | ||
next(e); | ||
} | ||
} | ||
|
||
export async function getArticleBySlug(req, res, next) { | ||
try { | ||
const article = await models.Article.findByPk(req.params.article_slug); | ||
|
||
if (!article) { | ||
return res.sendStatus(404); | ||
} | ||
|
||
return res.status(200).send(article); | ||
} catch (e) { | ||
next(e); | ||
} | ||
} | ||
|
||
export async function createArticle(req, res, next) { | ||
try { | ||
let slug = req.body.title.replace(/\b\w+'(?=[A-Za-zÀ-ÖØ-öø-ÿ])/gi, ""); | ||
slug = slug.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); | ||
slug = slug.replace(/[^\w\s]/gi, ""); | ||
slug = slug.replace(/\s+/g, "-"); | ||
slug = slug.toLowerCase(); | ||
|
||
const article = await models.Article.create( | ||
{ ...req.body, slug }, | ||
{ | ||
individualHooks: true, | ||
hooks: true, | ||
}, | ||
); | ||
await reloadPaths("/blog"); | ||
return res.status(200).send(article); | ||
} catch (e) { | ||
next(e); | ||
} | ||
} | ||
|
||
export async function deleteArticle(req, res, next) { | ||
try { | ||
await models.Article.destroy({ | ||
where: { slug: req.params.article_slug }, | ||
individualHooks: true, | ||
hooks: true, | ||
}); | ||
await reloadPaths("/blog"); | ||
return res.sendStatus(200); | ||
} catch (e) { | ||
next(e); | ||
} | ||
} | ||
|
||
export async function updateArticle(req, res, next) { | ||
try { | ||
let slug = req.body.title.replace(/\b\w+'(?=[A-Za-zÀ-ÖØ-öø-ÿ])/gi, ""); | ||
slug = slug.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); | ||
slug = slug.replace(/[^\w\s]/gi, ""); | ||
slug = slug.replace(/\s+/g, "-"); | ||
slug = slug.toLowerCase(); | ||
|
||
await models.Article.update( | ||
{ ...req.body, slug }, | ||
{ | ||
where: { slug: req.params.article_slug }, | ||
individualHooks: true, | ||
hooks: true, | ||
}, | ||
); | ||
await reloadPaths("/blog"); | ||
next(); | ||
} catch (e) { | ||
next(e); | ||
} | ||
} | ||
|
||
export async function accessArticlePicture(req, res, next) { | ||
try { | ||
const article = await models.Article.findByPk(req.params.article_slug, { | ||
fields: [req.params.image_type], | ||
}); | ||
if (!article || !article[req.params.image_type]) return res.sendStatus(404); | ||
|
||
req.file = { | ||
path: join( | ||
articlePicMediaPath, | ||
article.getDataValue(req.params.image_type), | ||
), | ||
}; | ||
next(); | ||
} catch (error) { | ||
next(error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { join } from "path"; | ||
import sequelize from "sequelize"; | ||
const { DataTypes, Model } = sequelize; | ||
import { | ||
ignoreURL, | ||
modelFileDeleter, | ||
modelFileWriter, | ||
} from "../services/fileService"; | ||
import { apiPath, back, articlePicMediaPath } from "../config/config"; | ||
|
||
export default function (sequelize) { | ||
class Article extends Model {} | ||
|
||
Article.init( | ||
{ | ||
slug: { | ||
type: DataTypes.STRING(220), | ||
allowNull: false, | ||
primaryKey: true, | ||
}, | ||
title: { | ||
type: DataTypes.STRING(200), | ||
allowNull: false, | ||
}, | ||
description: { | ||
type: DataTypes.STRING(400), | ||
allowNull: false, | ||
}, | ||
content: { | ||
type: DataTypes.TEXT, | ||
allowNull: false, | ||
}, | ||
readingTime: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false, | ||
}, | ||
image: { | ||
type: DataTypes.STRING(60), | ||
allowNull: false, | ||
get() { | ||
const picture = this.getDataValue("image"); | ||
return picture | ||
? new URL( | ||
join( | ||
apiPath, | ||
"articles", | ||
String(this.getDataValue("slug")), | ||
"image", | ||
), | ||
back, | ||
) | ||
: picture; | ||
}, | ||
}, | ||
thumbnail: { | ||
type: DataTypes.STRING(70), | ||
get() { | ||
const picture = this.getDataValue("thumbnail"); | ||
return picture | ||
? new URL( | ||
join( | ||
apiPath, | ||
"articles", | ||
String(this.getDataValue("slug")), | ||
"thumbnail", | ||
), | ||
back, | ||
) | ||
: picture; | ||
}, | ||
}, | ||
}, | ||
{ | ||
sequelize, | ||
}, | ||
); | ||
|
||
const pictureWriter = modelFileWriter( | ||
"image", | ||
articlePicMediaPath, | ||
"picture_", | ||
(mime) => { | ||
if (!mime.startsWith("image")) | ||
return Promise.reject(new Error("file not accepted")); | ||
}, | ||
{ quality: 70, width: 1500 }, | ||
true, | ||
); | ||
|
||
Article.beforeCreate(ignoreURL("image", pictureWriter)); | ||
|
||
Article.beforeUpdate(ignoreURL("image", pictureWriter)); | ||
Article.beforeUpdate(ignoreURL("thumbnail")); | ||
|
||
Article.beforeDestroy( | ||
modelFileDeleter("image", articlePicMediaPath), | ||
modelFileDeleter("thumbnail", articlePicMediaPath), | ||
); | ||
|
||
return Article; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import express from "express"; | ||
import { | ||
getAllArticles, | ||
getArticleBySlug, | ||
updateArticle, | ||
deleteArticle, | ||
createArticle, | ||
accessArticlePicture, | ||
} from "../controllers/articleController"; | ||
import { downloadFile } from "../services/fileService"; | ||
|
||
const articlesRouter = express.Router(); | ||
|
||
articlesRouter.route("/").get(getAllArticles); | ||
articlesRouter.route("/:article_slug").get(getArticleBySlug); | ||
|
||
articlesRouter | ||
.route("/its-a-fucking-route-to-manage-article") | ||
.post(createArticle); | ||
|
||
articlesRouter | ||
.route("/its-a-fucking-route-to-manage-article/:article_slug") | ||
.put(updateArticle, getArticleBySlug) | ||
.delete(deleteArticle); | ||
|
||
articlesRouter | ||
.route("/:article_slug/:image_type") | ||
.get(accessArticlePicture, downloadFile); | ||
|
||
export default articlesRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.