Skip to content

Commit

Permalink
feat(event): make cover an attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
hulkoba committed Jan 31, 2023
1 parent f647bb4 commit 875f41f
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .adonisrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"@adonisjs/auth",
"adonis5-swagger",
"@adonisjs/shield",
"@adonisjs/session"
"@adonisjs/session",
"@adonisjs/attachment-lite"
],
"aceProviders": [
"@adonisjs/repl"
Expand Down
8 changes: 5 additions & 3 deletions app/Models/Event.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { DateTime } from 'luxon'
import { BaseModel, column, HasOne, hasOne } from '@ioc:Adonis/Lucid/Orm'
import { attachment, AttachmentContract } from '@ioc:Adonis/Addons/AttachmentLite'

import User from './User'
import Venue from './Venue'

Expand All @@ -22,8 +24,8 @@ export default class Event extends BaseModel {
@column()
public description: string

@column()
public cover: string
@attachment({ preComputeUrl: true }) // Generating URLs for the API response
public cover: AttachmentContract | null

@column()
public prePayment: string
Expand All @@ -38,7 +40,7 @@ export default class Event extends BaseModel {
public alternativeAddress: string

@column()
public links: string
public eventLinks: string

@column()
public creatorEmail: string
Expand Down
4 changes: 3 additions & 1 deletion app/Validators/Event/CreateEventValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class CreateEventValidator {
box_office: schema.string.optional(),
pre_payment: schema.string.optional(),
alternative_address: schema.string.optional(),
cover: schema.string.optional([rules.url()]),
cover: schema.file.optional({ size: '2mb', extnames: ['jpg', 'gif', 'png'] }),
creator_email: schema.string.optional([
rules.email(),
rules.exists({ table: 'users', column: 'email' }),
Expand All @@ -28,5 +28,7 @@ export default class CreateEventValidator {
'venue_id.exists': 'Referenced venue does not exist',
'creator_email.email': 'Please enter a valid email address',
'creator_email.exists': 'Referenced user does not exist',
'file.size': 'The file size must be under {{ options.size }}',
'file.extname': 'The file must have one of {{ options.extnames }} extension names',
}
}
4 changes: 3 additions & 1 deletion app/Validators/Event/UpdateEventValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class UpdateEventValidator {
box_office: schema.string.optional(),
pre_payment: schema.string.optional(),
alternative_address: schema.string.optional(),
cover: schema.string.optional([rules.url()]),
cover: schema.file.optional({ size: '2mb', extnames: ['jpg', 'gif', 'png'] }),
// TODO: only allowed if the create a moderator or admin
is_public: schema.boolean.optional(),
venue_id: schema.string.optional([rules.exists({ table: 'venues', column: 'id' })]),
Expand All @@ -26,5 +26,7 @@ export default class UpdateEventValidator {
'venue_id.exists': 'Referenced venue does not exist',
'creator_email.email': 'Please enter a valid email address',
'creator_email.exists': 'Referenced user does not exist',
'file.size': 'The file size must be under {{ options.size }}',
'file.extname': 'The file must have one of {{ options.extnames }} extension names',
}
}
25 changes: 21 additions & 4 deletions database/factories/EventFactory.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
import Event from '../../app/Models/Event'
import Factory from '@ioc:Adonis/Lucid/Factory'
import Drive from '@ioc:Adonis/Core/Drive'
import { file } from '@ioc:Adonis/Core/Helpers'
import { Attachment } from '@ioc:Adonis/Addons/AttachmentLite'

import Event from '../../app/Models/Event'

export default Factory.define(Event, ({ faker }: any) => {
export default Factory.define(Event, async ({ faker }: any) => {
const dateFrom = new Date()
const dateTo = new Date()
dateTo.setDate(dateTo.getDate() + Math.round(5))
const support = new Array(5).fill(null).map(() => faker.random.word())

// Create an instance of attachment and mark image as persisted
const coverImage = new Attachment({
extname: 'png',
mimeType: 'image/png',
size: 10 * 1000,
name: `${faker.random.alphaNumeric(10)}.png`,
})
coverImage.isPersisted = true

// Persist the file using Drive.
await Drive.put(coverImage.name, (await file.generatePng('1mb')).contents)

return {
title: faker.random.word(),
date: faker.date.between(dateFrom, dateTo).toISOString(),
headliner: faker.random.word(),
support: JSON.stringify(support),
description: faker.random.word(),
cover: faker.random.numeric(),
cover: coverImage,
pre_payment: (faker.random.numeric(5) + faker.random.numeric(3)).toString(),
box_office: (faker.random.numeric(5) + faker.random.numeric(3)).toString(),
venue: faker.random.numeric(),
links: faker.random.words(),
event_links: faker.random.words(),
creator: faker.random.numeric(),
is_public: true,
}
Expand Down
19 changes: 19 additions & 0 deletions database/migrations/1675154622577_events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import BaseSchema from '@ioc:Adonis/Lucid/Schema'

export default class extends BaseSchema {
protected tableName = 'events'

public async up() {
this.schema.alterTable(this.tableName, (table) => {
table.renameColumn('links', 'event_links')
table.json('cover').alter()
})
}

public async down() {
this.schema.alterTable(this.tableName, (table) => {
table.renameColumn('event_links', 'links')
table.string('cover').alter()
})
}
}
76 changes: 75 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"youch-terminal": "^2.1.5"
},
"dependencies": {
"@adonisjs/attachment-lite": "^1.0.7",
"@adonisjs/auth": "^8.2.3",
"@adonisjs/core": "^5.8.0",
"@adonisjs/lucid": "^18.3.0",
Expand Down
10 changes: 5 additions & 5 deletions tests/functional/event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test.group('Events', (group) => {
'box_office',
'venue_id',
'alternative_address',
'links',
'event_links',
'creator_email',
'is_public',
'created_at',
Expand Down Expand Up @@ -65,7 +65,7 @@ test.group('Events', (group) => {
'box_office',
'venue_id',
'alternative_address',
'links',
'event_links',
'creator_email',
'is_public',
'created_at',
Expand Down Expand Up @@ -96,7 +96,7 @@ test.group('Events', (group) => {
cover: 'https://link-to-cover.de',
pre_payment: '33 Euro',
box_office: '34',
links: 'https://www.test-location.de',
event_links: 'https://www.test-location.de',
alternative_address: 'test',
creator_email: user.email,
venue_id: venue.id,
Expand All @@ -113,7 +113,7 @@ test.group('Events', (group) => {
'box_office',
'venue_id',
'alternative_address',
'links',
'event_links',
'is_public',
'creator_email',
'created_at',
Expand Down Expand Up @@ -240,7 +240,7 @@ test.group('Events', (group) => {
cover: 'https://link-to-ohter-webseit.de',
pre_payment: '24 €',
box_office: '25 Tacken',
links: 'https://www.new-test-location.de',
event_links: 'https://www.new-test-location.de',
alternative_address: 'somewhere else',
is_public: false,
venue_id: venue.id,
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"@adonisjs/auth",
"adonis5-swagger",
"@adonisjs/shield",
"@adonisjs/session"
"@adonisjs/session",
"@adonisjs/attachment-lite"
]
}
}

0 comments on commit 875f41f

Please sign in to comment.