let's slugifyyyy

This commit is contained in:
les
2021-04-13 18:04:53 +02:00
parent af106787d5
commit 1edf6b1799
4 changed files with 25 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
<template lang="pug"> <template lang="pug">
v-card.h-event.event v-card.h-event.event
nuxt-link(:to='`/event/${event.id}`') nuxt-link(:to='`/event/${event.slug || event.id}`')
v-img.img(:src="`/media/thumb/${event.image_path || 'logo.svg' }`") v-img.img(:src="`/media/thumb/${event.image_path || 'logo.svg' }`")
v-icon.float-right.mr-1(v-if='event.parentId' color='success') mdi-repeat v-icon.float-right.mr-1(v-if='event.parentId' color='success') mdi-repeat
.title.p-name {{event.title}} .title.p-name {{event.title}}
@@ -43,4 +43,4 @@ export default {
}, },
computed: mapState(['settings']) computed: mapState(['settings'])
} }
</script> </script>

View File

@@ -67,6 +67,7 @@
"prom-client": "^13.1.0", "prom-client": "^13.1.0",
"sequelize": "^6.6.2", "sequelize": "^6.6.2",
"sequelize-cli": "^6.2.0", "sequelize-cli": "^6.2.0",
"sequelize-slugify": "^1.5.0",
"sharp": "^0.27.2", "sharp": "^0.27.2",
"sqlite3": "^5.0.2", "sqlite3": "^5.0.2",
"tiptap": "^1.32.0", "tiptap": "^1.32.0",

View File

@@ -86,14 +86,21 @@ const eventController = {
}, },
async get (req, res) { async get (req, res) {
log.error('get')
const format = req.params.format || 'json' const format = req.params.format || 'json'
const is_admin = req.user && req.user.is_admin const is_admin = req.user && req.user.is_admin
const id = Number(req.params.event_id) const slug = req.params.event_id
const id = Number(req.params.event_id) || -1
console.error(slug)
let event let event
try { try {
event = await Event.findByPk(id, { event = await Event.findOne({
where: {
[Op.or]: {
slug,
id
}
},
attributes: { attributes: {
exclude: ['createdAt', 'updatedAt', 'placeId'] exclude: ['createdAt', 'updatedAt', 'placeId']
}, },
@@ -112,6 +119,7 @@ const eventController = {
order: [[Resource, 'id', 'DESC']] order: [[Resource, 'id', 'DESC']]
}) })
} catch (e) { } catch (e) {
console.error(e)
return res.sendStatus(400) return res.sendStatus(400)
} }
@@ -119,6 +127,7 @@ const eventController = {
return res.sendStatus(400) return res.sendStatus(400)
} }
console.error('diocane')
// get prev and next event // get prev and next event
const next = await Event.findOne({ const next = await Event.findOne({
attributes: ['id'], attributes: ['id'],
@@ -446,7 +455,7 @@ const eventController = {
const events = await Event.findAll({ const events = await Event.findAll({
where, where,
attributes: { attributes: {
exclude: ['slug', 'likes', 'boost', 'userId', 'is_visible', 'createdAt', 'updatedAt', 'description', 'resources'] exclude: ['likes', 'boost', 'userId', 'is_visible', 'createdAt', 'updatedAt', 'description', 'resources']
}, },
order: ['start_datetime', Sequelize.literal('(SELECT COUNT("tagTag") FROM event_tags WHERE "tagTag" = tag) DESC')], order: ['start_datetime', Sequelize.literal('(SELECT COUNT("tagTag") FROM event_tags WHERE "tagTag" = tag) DESC')],
include: [ include: [

View File

@@ -3,6 +3,8 @@ const moment = require('dayjs')
const htmlToText = require('html-to-text') const htmlToText = require('html-to-text')
const { Model, DataTypes } = require('sequelize') const { Model, DataTypes } = require('sequelize')
const SequelizeSlugify = require('sequelize-slugify')
const sequelize = require('./index') const sequelize = require('./index')
const Resource = require('./resource') const Resource = require('./resource')
@@ -26,7 +28,11 @@ Event.init({
autoIncrement: true autoIncrement: true
}, },
title: DataTypes.STRING, title: DataTypes.STRING,
slug: DataTypes.STRING, slug: {
type: DataTypes.STRING,
index: true,
unique: true
},
description: DataTypes.TEXT, description: DataTypes.TEXT,
multidate: DataTypes.BOOLEAN, multidate: DataTypes.BOOLEAN,
start_datetime: { start_datetime: {
@@ -62,6 +68,8 @@ Resource.belongsTo(Event)
Event.hasMany(Event, { as: 'child', foreignKey: 'parentId' }) Event.hasMany(Event, { as: 'child', foreignKey: 'parentId' })
Event.belongsTo(Event, { as: 'parent' }) Event.belongsTo(Event, { as: 'parent' })
SequelizeSlugify.slugifyModel(Event, { source: ['title'] })
Event.prototype.toAPNote = function (username, locale, to = []) { Event.prototype.toAPNote = function (username, locale, to = []) {
const tags = this.tags && this.tags.map(t => t.tag.replace(/[ #]/g, '_')) const tags = this.tags && this.tags.map(t => t.tag.replace(/[ #]/g, '_'))
const plainDescription = htmlToText.fromString(this.description && this.description.replace('\n', '').slice(0, 1000)) const plainDescription = htmlToText.fromString(this.description && this.description.replace('\n', '').slice(0, 1000))