start with Event component, associate Tag and Place to event model

This commit is contained in:
lesion 2024-08-26 10:19:11 +02:00
parent 50c500bdc9
commit 16f09094da
No known key found for this signature in database
GPG key ID: 352918250B012177
5 changed files with 113 additions and 6 deletions

44
components/Event.vue Normal file
View file

@ -0,0 +1,44 @@
<template>
<article class='h-event' itemscope itemtype="https://schema.org/Event">
<nuxt-link :to='`/event/${event.slug || event.id}`' itemprop="url">
<!-- <MyPicture v-if='!hide_thumbs' :event='event' thumb :lazy='lazy' /> -->
<v-icon class='float-right mr-1' v-if='event.parentId' color='success' v-text='mdiRepeat' />
<h1 class='title p-name' itemprop="name">{{ event.title }}</h1>
</nuxt-link>
<v-img contain v-if='event?.ap_user?.image' :src='event?.ap_user?.image' max-height=30 max-width=30 style="position: absolute; top: 5px; right: 5px;" />
<v-card-text class='body pt-0 pb-0'>
<!-- <time class='dt-start subtitle-1' :datetime='$time.unixFormat(event.start_datetime, "yyyy-MM-dd HH:mm")'
itemprop="startDate" :content="$time.unixFormat(event.start_datetime, 'yyyy-MM-dd\'T\'HH:mm')"> <v-icon v-text='mdiCalendar' /> {{ $time.when(event) }}
</time>
<time class='d-none dt-end' v-if='event.end_datetime' itemprop="endDate"
:content="$time.unixFormat(event.end_datetime,'yyyy-MM-dd\'T\'HH:mm')"> {{ $time.unixFormat(event.end_datetime)}}</time> -->
<nuxt-link class='place d-block p-location pl-0' text
:to='`/place/${encodeURIComponent(event.place.name)}`'
itemprop="location" itemscope itemtype="https://schema.org/Place">
<v-icon v-text='mdiMapMarker'></v-icon>
<span itemprop='name'>{{ event.place.name }}</span>
</nuxt-link>
<div class='d-none' itemprop='address'>{{ event.place.address }}</div>
</v-card-text>
<v-card-actions class='flex-wrap'>
<v-chip class='ml-1 mt-1' v-for='tag in event.tags.slice(0, 6)' small label :to='`/tag/${encodeURIComponent(tag)}`'
:key='tag' outlined color='primary'>{{ tag }}</v-chip>
</v-card-actions>
</article>
</template>
<script setup lang="ts">
// TODO: hide thumbs
defineProps({
event: { type: Object, default: () => ({})},
lazy: { type: Boolean, default: false }
})
</script>

View file

@ -19,8 +19,7 @@ import type { Announcement, Event } from '#build/types/nitro-imports'
<v-lazy class='event v-card'
v-for='(event, idx) in events' :key='event.id'
:options="{ threshold: .5, rootMargin: '500px' }">
<NuxtLink :to="`e/${event.slug}`">{{ event.title }}</NuxtLink>
<!-- <Event :event='event' :lazy='idx>9' /> -->
<Event :event='event' :lazy='idx>9' />
</v-lazy>
</section>
<!-- <section class='text-center' v-else> -->

View file

@ -1,6 +1,6 @@
import { Event } from "~/server/utils/sequelize"
import { Event, Place, Tag } from "~/server/utils/sequelize"
export default defineEventHandler((event) => {
return Event.findAll()
return Event.findAll({ include: [Place, Tag] })
})

View file

@ -28,6 +28,9 @@ import {
Index,
Unique,
BeforeSave,
AllowNull,
BelongsToMany,
BelongsTo,
} from "@sequelize/core/decorators-legacy"
// import type { SqliteDialect } from "@sequelize/sqlite3";
@ -94,12 +97,73 @@ InferCreationAttributes<Event>
@Attribute(DataTypes.TEXT)
declare description: string
@Attribute({
type: DataTypes.INTEGER.UNSIGNED,
references: { table: "places", key: "id" },
onUpdate: "CASCADE",
onDelete: "CASCADE",
})
declare placeId: number;
// @BelongsTo(() => Place, 'id')
// declare places
@BelongsToMany(() => Tag, { through: 'event_tags '})
declare tags?: NonAttribute<Tag[]>
}
@Table({
tableName: 'tags'
})
export class Tag extends Model<
InferAttributes<User>,
InferCreationAttributes<User>
> {
@Attribute(DataTypes.STRING)
@Index
@Unique
declare tag: string;
// @BelongsToMany(() => Event, { through: 'event_tags' })
// declare events?: NonAttribute<Event[]>
}
@Table({
tableName: 'places'
})
export class Place extends Model<
InferAttributes<User>,
InferCreationAttributes<User>
> {
@Attribute(DataTypes.INTEGER.UNSIGNED)
@PrimaryKey
@AutoIncrement
declare readonly id: CreationOptional<number>;
@Attribute(DataTypes.STRING)
@Index
@Unique
declare name: string;
@Attribute(DataTypes.STRING)
declare address: string;
@Attribute(DataTypes.FLOAT)
declare latitude: number;
@Attribute(DataTypes.FLOAT)
declare longitude: number;
@HasMany(() => Event, /* foreign key */ 'placeId')
declare events?: NonAttribute<Event[]>;
}
type Role = "admin" | "editor" | "user"
@Table({
tableName: "Users",
tableName: "users",
indexes: [{ fields: ["email"], unique: true }],
})
export class User extends Model<
@ -214,4 +278,4 @@ type Role = "admin" | "editor" | "user"
// declare author?: NonAttribute<User>;
// }
sequelize.addModels([Announcement, Event]);
sequelize.addModels([Announcement, Place, Event, Tag]);