Skip to content

Commit

Permalink
feat(event): upload cover
Browse files Browse the repository at this point in the history
fix(event): upload event cover

feat(event): validate cover
  • Loading branch information
hulkoba committed Feb 6, 2023
1 parent 875f41f commit ceb3756
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 177 deletions.
24 changes: 19 additions & 5 deletions app/Controllers/Http/EventsController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import { Attachment } from '@ioc:Adonis/Addons/AttachmentLite'

import Event from 'App/Models/Event'
import CreateEventValidator from 'App/Validators/Event/CreateEventValidator'
import CoverUploadValidator from 'App/Validators/Event/CoverUploadValidator'
import UpdateEventValidator from 'App/Validators/Event/UpdateEventValidator'

export default class EventsController {
Expand All @@ -19,27 +21,39 @@ export default class EventsController {

// post one event
public async store({ request, response }: HttpContextContract) {
const payload = await request.validate(CreateEventValidator)
// TODO set is_public if user.role equal moderator or admin
// TODO create_email must not be handed over, must be taken over via the auth
const payload = await request.validate(CreateEventValidator)
const event = await Event.create(payload)
await event.refresh()

const { cover } = await request.validate(CoverUploadValidator)
event.cover = cover ? Attachment.fromFile(cover) : null

await event.save()
response.json({ success: true, event })
}

// update one event with id
public async update({ request, response }: HttpContextContract) {
const event = await Event.findByOrFail('id', request.param('id'))
const payload = await request.validate(UpdateEventValidator)
const { id } = request.params()
const event = await Event.findOrFail(id)

const payload = await request.validate(UpdateEventValidator)
await event.merge(payload).save()

const { cover } = await request.validate(CoverUploadValidator)
event.cover = cover ? Attachment.fromFile(cover) : null

// Old file will be removed from the disk as well.
await event.save()
response.json({ success: true, event })
}

// delete one event with id
public async destroy({ request, response }: HttpContextContract) {
const event = await Event.findByOrFail('id', request.param('id'))
const { id } = request.params()
const event = await Event.findOrFail(id)

await event?.delete()

response.json({ success: true })
Expand Down
15 changes: 15 additions & 0 deletions app/Validators/Event/CoverUploadValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { schema, CustomMessages } from '@ioc:Adonis/Core/Validator'
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'

export default class CoverUploadValidator {
constructor(protected ctx: HttpContextContract) {}

public schema = schema.create({
cover: schema.file.nullableAndOptional({ size: '2mb', extnames: ['jpg', 'jpeg', 'png'] }),
})

public messages: CustomMessages = {
'file.size': 'The file size must be under {{ options.size }}',
'file.extname': 'The file must have one of {{ options.extnames }} extension names',
}
}
5 changes: 1 addition & 4 deletions app/Validators/Event/CreateEventValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ export default class CreateEventValidator {
box_office: schema.string.optional(),
pre_payment: schema.string.optional(),
alternative_address: schema.string.optional(),
cover: schema.file.optional({ size: '2mb', extnames: ['jpg', 'gif', 'png'] }),
creator_email: schema.string.optional([
rules.email(),
rules.exists({ table: 'users', column: 'email' }),
]),
venue_id: schema.string.optional([rules.exists({ table: 'venues', column: 'id' })]),
links: schema.string.optional(),
event_links: schema.string.optional(),
})

public messages: CustomMessages = {
Expand All @@ -28,7 +27,5 @@ 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',
}
}
5 changes: 1 addition & 4 deletions app/Validators/Event/UpdateEventValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ export default class UpdateEventValidator {
box_office: schema.string.optional(),
pre_payment: schema.string.optional(),
alternative_address: schema.string.optional(),
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' })]),
links: schema.string.optional(),
event_links: schema.string.optional(),
})

public messages: CustomMessages = {
Expand All @@ -26,7 +25,5 @@ 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',
}
}
2 changes: 1 addition & 1 deletion docs/swagger/components/requestBodies/createEventBody.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ components:
createEventBody:
description: Successful create a new Event
content:
application/json:
multipart/form-data:
schema:
$ref: '#/components/schemas/BasicEvent'
6 changes: 3 additions & 3 deletions docs/swagger/components/schemas/event.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ components:
type: string
format: date-time
description: This is the Iso DateTime and says what day the event is and what time the doors open.
example: '2023-01-20T01:06:21.812'
headliner:
type: string
example: Borders Down
Expand All @@ -29,8 +30,7 @@ components:
example: Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
cover:
type: string
format: hostname
description: url link to event cover
format: binary
pre_payment:
type: string
description: Vorkasse (Is string because it should be possible to specify from to)
Expand All @@ -43,7 +43,7 @@ components:
type: string
format: uuid
description: Is the reference to the Venue.
links:
event_links:
type: string
description: list of links
example: "https://facebook.de, https://bandcamp.com"
Expand Down
27 changes: 26 additions & 1 deletion docs/swagger/paths/events.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
paths:
/api/events:
/_api/events:
get:
tags:
- Event
Expand Down Expand Up @@ -27,6 +27,31 @@ paths:
example: true
Event:
$ref: '#/components/schemas/Event'

/_api/events/upload:
post:
type:
- Event
description: upload image
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
cover:
type: string
format: binary
responses:
'200':
description: Successful
content:
application/json:
schema:
type: object
properties:
cover:
type: strings

/api/events/{id}:
get:
Expand Down
Loading

0 comments on commit ceb3756

Please sign in to comment.