added event attachments support

This commit is contained in:
2023-04-01 07:23:26 +10:00
parent 8305f16dae
commit eb32c99764
5 changed files with 177 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Conductors;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\InvalidCastException;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class EventConductor extends Conductor class EventConductor extends Conductor
@@ -89,4 +90,21 @@ class EventConductor extends Conductor
$user = auth()->user(); $user = auth()->user();
return ($user !== null && $user->hasPermission('admin/events') === true); return ($user !== null && $user->hasPermission('admin/events') === true);
} }
/**
* Transform the model
*
* @param Model $model The model to transform.
* @return array The transformed model.
* @throws InvalidCastException Cannot cast item to model.
*/
public function transform(Model $model)
{
$result = $model->toArray();
$result['attachments'] = $model->attachments()->get()->map(function ($attachment) {
return MediaConductor::model(request(), $attachment->media);
});
return $result;
}
} }

View File

@@ -5,7 +5,9 @@ namespace App\Http\Controllers\Api;
use App\Enum\HttpResponseCodes; use App\Enum\HttpResponseCodes;
use App\Models\Event; use App\Models\Event;
use App\Conductors\EventConductor; use App\Conductors\EventConductor;
use App\Conductors\MediaConductor;
use App\Http\Requests\EventRequest; use App\Http\Requests\EventRequest;
use App\Models\Media;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class EventController extends ApiController class EventController extends ApiController
@@ -32,7 +34,8 @@ class EventController extends ApiController
return $this->respondAsResource( return $this->respondAsResource(
$collection, $collection,
['isCollection' => true, ['isCollection' => true,
'appendData' => ['total' => $total]] 'appendData' => ['total' => $total]
]
); );
} }
@@ -103,4 +106,133 @@ class EventController extends ApiController
return $this->respondForbidden(); return $this->respondForbidden();
} }
} }
/**
* Get a list of attachments related to this model.
*
* @param Request $request The user request.
* @param Post $post The post model.
* @return JsonResponse Returns the post attachments.
* @throws InvalidFormatException
* @throws BindingResolutionException
* @throws InvalidCastException
*/
public function getAttachments(Request $request, Event $event)
{
if (EventConductor::viewable($event) === true) {
$medium = $event->attachments->map(function ($attachment) {
return $attachment->media;
});
return $this->respondAsResource(MediaConductor::collection($request, $medium), ['isCollection' => true, 'resourceName' => 'attachment']);
}
return $this->respondForbidden();
}
/**
* Store an attachment related to this model.
*
* @param Request $request The user request.
* @param Post $post The post model.
* @return JsonResponse The response.
* @throws BindingResolutionException
* @throws MassAssignmentException
*/
public function storeAttachment(Request $request, Event $event)
{
if (EventConductor::updatable($event) === true) {
if ($request->has("medium") && Media::find($request->medium)) {
$event->attachments()->create(['media_id' => $request->medium]);
return $this->respondCreated();
}
return $this->respondWithErrors(['media' => 'The media ID was not found']);
}
return $this->respondForbidden();
}
/**
* Update/replace attachments related to this model.
*
* @param Request $request The user request.
* @param Post $post The related model.
* @return JsonResponse
* @throws BindingResolutionException
* @throws MassAssignmentException
*/
public function updateAttachments(Request $request, Event $event)
{
if (EventConductor::updatable($event) === true) {
$mediaIds = $request->attachments;
if (is_array($mediaIds) === false) {
$mediaIds = explode(',', $request->attachments);
}
$mediaIds = array_map('trim', $mediaIds); // trim each media ID
$attachments = $event->attachments;
// Delete attachments that are not in $mediaIds
foreach ($attachments as $attachment) {
if (!in_array($attachment->media_id, $mediaIds)) {
$attachment->delete();
}
}
// Create new attachments for media IDs that are not already in $post->attachments()
foreach ($mediaIds as $mediaId) {
$found = false;
foreach ($attachments as $attachment) {
if ($attachment->media_id == $mediaId) {
$found = true;
break;
}
}
if (!$found) {
$event->attachments()->create(['media_id' => $mediaId]);
}
}
return $this->respondNoContent();
}//end if
return $this->respondForbidden();
}
/**
* Delete a specific related attachment.
* @param Request $request The user request.
* @param Post $post The model.
* @param Media $medium The attachment medium.
* @return JsonResponse
* @throws BindingResolutionException
*/
public function deleteAttachment(Request $request, Event $event, Media $medium)
{
if (EventConductor::updatable($event) === true) {
$attachments = $event->attachments;
$deleted = false;
foreach ($attachments as $attachment) {
if ($attachment->media_id === $medium->id) {
$attachment->delete();
$deleted = true;
break;
}
}
if ($deleted) {
// Attachment was deleted successfully
return $this->respondNoContent();
} else {
// Attachment with matching media ID was not found
return $this->respondNotFound();
}
}
return $this->respondForbidden();
}
} }

View File

@@ -32,11 +32,12 @@ class Event extends Model
'ages', 'ages',
]; ];
/** /**
* Get all of the post's attachments. * Get all of the post's attachments.
*/ */
public function attachments() public function attachments()
{ {
return $this->morphMany('App\Attachment', 'attachable'); return $this->morphMany('App\Models\Attachment', 'attachable');
} }
} }

View File

@@ -18,6 +18,7 @@
<div class="sm-workshop-body"> <div class="sm-workshop-body">
<h2 class="sm-workshop-title">{{ event.title }}</h2> <h2 class="sm-workshop-title">{{ event.title }}</h2>
<SMHTML :html="event.content" class="sm-workshop-content" /> <SMHTML :html="event.content" class="sm-workshop-content" />
<SMAttachments :attachments="event.attachments || []" />
</div> </div>
<div class="sm-workshop-info"> <div class="sm-workshop-info">
<div <div
@@ -119,6 +120,7 @@ import { useRoute } from "vue-router";
import SMButton from "../components/SMButton.vue"; import SMButton from "../components/SMButton.vue";
import SMHTML from "../components/SMHTML.vue"; import SMHTML from "../components/SMHTML.vue";
import SMMessage from "../components/SMMessage.vue"; import SMMessage from "../components/SMMessage.vue";
import SMAttachments from "../components/SMAttachments.vue";
import { api } from "../helpers/api"; import { api } from "../helpers/api";
import { Event, EventResponse, MediaResponse } from "../helpers/api.types"; import { Event, EventResponse, MediaResponse } from "../helpers/api.types";
import { SMDate } from "../helpers/datetime"; import { SMDate } from "../helpers/datetime";

View File

@@ -299,6 +299,12 @@ const loadData = async () => {
form.controls.hero.value = data.event.hero; form.controls.hero.value = data.event.hero;
form.controls.price.value = data.event.price; form.controls.price.value = data.event.price;
form.controls.ages.value = data.event.ages; form.controls.ages.value = data.event.ages;
attachments.value = (data.event.attachments || []).map(function (
attachment
) {
return attachment.id.toString();
});
} catch (err) { } catch (err) {
pageError.value = err.response.status; pageError.value = err.response.status;
} finally { } finally {
@@ -334,7 +340,10 @@ const handleSubmit = async () => {
ages: form.controls.ages.value, ages: form.controls.ages.value,
}; };
let event_id = "";
if (route.params.id) { if (route.params.id) {
event_id = route.params.id as string;
await api.put({ await api.put({
url: "/events/{id}", url: "/events/{id}",
params: { params: {
@@ -343,12 +352,24 @@ const handleSubmit = async () => {
body: data, body: data,
}); });
} else { } else {
await api.post({ let result = await api.post({
url: "/events", url: "/events",
body: data, body: data,
}); });
if (result.data) {
const data = result.data as EventResponse;
event_id = data.event.id;
}
} }
await api.put({
url: `/events/${event_id}/attachments`,
body: {
attachments: attachments.value,
},
});
useToastStore().addToast({ useToastStore().addToast({
title: route.params.id ? "Event Updated" : "Event Created", title: route.params.id ? "Event Updated" : "Event Created",
content: route.params.id content: route.params.id