Skip to content

Commit

Permalink
Updated API EventController
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko259 committed Aug 6, 2024
1 parent efcbba6 commit 56fa80b
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions app/Http/Controllers/API/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public function store(Request $request)
'start_date' => 'required|date_format:Y-m-d H:i|after_or_equal:today',
'end_date' => 'required|date_format:Y-m-d H:i|after_or_equal:start_date',
'event_type' => 'integer',
'recurrence_interval' => 'nullable|integer',
'recurrence_unit' => 'nullable|string|max:255',
'recurrence_end_date' => 'nullable|date_format:Y-m-d H:i|after_or_equal:end_date',
'recurrence_interval' => 'nullable|required_if:event_type,1|integer',
'recurrence_unit' => 'nullable|required_if:event_type,1|string|max:255',
'recurrence_end_date' => 'nullable|required_if:event_type,1|date_format:Y-m-d H:i|after_or_equal:end_date',
'image' => 'nullable|image|mimes:jpeg,jpg,png|max:2048',
]);

Expand All @@ -62,7 +62,7 @@ public function store(Request $request)
if ($request->hasFile('image')) {
$image = $request->file('image');
$imageName = now()->format('Y-m-d') . '-' . uniqid() . '.' . $image->getClientOriginalExtension();

// Get image dimensions
list($width, $height) = getimagesize($image->getPathName());
if (round($width / $height, 2) != round(16 / 9, 2)) {
Expand Down Expand Up @@ -93,7 +93,7 @@ public function store(Request $request)
]);

// Ensure user association
$event->user()->associate($user);
$event->user()->associate(\Auth::user());
$event->save();

// Generate and save recurrences if the event is recurring
Expand Down Expand Up @@ -129,9 +129,9 @@ public function update(Request $request, Event $event)
'start_date' => 'required|date_format:Y-m-d H:i|after_or_equal:today',
'end_date' => 'required|date_format:Y-m-d H:i|after_or_equal:start_date',
'event_type' => 'integer',
'recurrence_interval' => 'nullable|integer',
'recurrence_unit' => 'nullable|string|max:255',
'recurrence_end_date' => 'nullable|date_format:Y-m-d H:i|after_or_equal:end_date',
'recurrence_interval' => 'nullable|required_if:event_type,1|integer',
'recurrence_unit' => 'nullable|required_if:event_type,1|string|max:255',
'recurrence_end_date' => 'nullable|required_if:event_type,1|date_format:Y-m-d H:i|after_or_equal:end_date',
'image' => 'nullable|image|mimes:jpeg,jpg,png|max:2048',
]);

Expand Down Expand Up @@ -183,7 +183,10 @@ public function update(Request $request, Event $event)
'image' => $imageName,
]);

$event->user()->associate($user);
// Ensure user association
$event->user()->associate(\Auth::user());

// Save the event before handling recurrences
$event->save();

// Check if the event is a recurring event and delete old recurrences if they exist
Expand All @@ -206,6 +209,11 @@ public function update(Request $request, Event $event)
*/
public function destroy(Event $event)
{
// Delete the old image if it exists
if ($event->image && $event->parent()->isEmpty()) {
Storage::disk('public')->delete('banners/' . $event->image);
}

$event->delete();

$event->children()->delete();
Expand Down

0 comments on commit 56fa80b

Please sign in to comment.