forked from TheRestartProject/restarters.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Feature: Soft Delete Groups and Events instead of Hard Deleting #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6495981
feat(db): add soft deletes migration for groups table
ardelato 33202f8
feat(model): add SoftDeletes trait to Group model
ardelato 58c2603
refactor(group): replace hard-delete with soft-delete in GroupController
ardelato f6e4def
refactor(event): remove destructive hard-delete of devices and associ…
ardelato f38cf71
feat(api): add soft-delete, restore, and deleted filter to groups adm…
ardelato 48e6f94
feat(api): add EventsController for event listing and restore
ardelato 6af44e4
feat(admin-ui): add deleted groups filter and restore action to admin…
ardelato 802acf2
test: update and add soft-delete tests for groups and events
ardelato e97a026
fix(stats): use Math.round for waste total and add null guards to Sta…
ardelato File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\Controllers\API; | ||
|
|
||
| use App\Http\Controllers\Controller; | ||
| use App\Models\Group; | ||
| use App\Models\Party; | ||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Http\Request; | ||
| use Illuminate\Support\Facades\Log; | ||
|
|
||
| class EventsController extends Controller | ||
| { | ||
| public function index(Request $request): JsonResponse | ||
| { | ||
| try { | ||
| $query = Party::with(['theGroup']); | ||
|
|
||
| // Apply search filter | ||
| if ($request->filled('search')) { | ||
| $search = $request->input('search'); | ||
| $query->where(function ($q) use ($search) { | ||
| $q->where('venue', 'like', "%{$search}%") | ||
| ->orWhere('location', 'like', "%{$search}%"); | ||
| }); | ||
| } | ||
|
|
||
| // Apply deleted filter | ||
| if ($request->filled('deleted')) { | ||
| $deletedFilter = $request->input('deleted'); | ||
| if ($deletedFilter === 'only') { | ||
| $query->onlyTrashed(); | ||
| } elseif ($deletedFilter === 'all') { | ||
| $query->withTrashed(); | ||
| } | ||
| // Default shows only non-deleted | ||
| } | ||
|
|
||
| // Handle sorting | ||
| $sortBy = $request->input('sort_by', 'event_start_utc'); | ||
| $sortDirection = $request->input('sort_direction', 'desc'); | ||
|
|
||
| if (!in_array(strtolower($sortDirection), ['asc', 'desc'])) { | ||
| $sortDirection = 'desc'; | ||
| } | ||
|
|
||
| $sortableColumns = [ | ||
| 'event_start_utc' => 'event_start_utc', | ||
| 'venue' => 'venue', | ||
| 'location' => 'location', | ||
| 'created_at' => 'created_at', | ||
| ]; | ||
|
|
||
| $sortColumn = $sortableColumns[$sortBy] ?? 'event_start_utc'; | ||
| $query->orderBy($sortColumn, $sortDirection); | ||
|
|
||
| $perPage = min($request->input('per_page', 100), 500); | ||
| $events = $query->paginate($perPage); | ||
|
|
||
| return response()->json([ | ||
| 'success' => true, | ||
| 'data' => $events->getCollection(), | ||
| 'current_page' => $events->currentPage(), | ||
| 'last_page' => $events->lastPage(), | ||
| 'per_page' => $events->perPage(), | ||
| 'total' => $events->total(), | ||
| 'from' => $events->firstItem(), | ||
| 'to' => $events->lastItem(), | ||
| ]); | ||
| } catch (\Exception $e) { | ||
| Log::error('Error fetching events: ' . $e->getMessage()); | ||
| return response()->json([ | ||
| 'success' => false, | ||
| 'message' => 'Failed to fetch events', | ||
| ], 500); | ||
| } | ||
| } | ||
|
|
||
| public static function performSingleAction(int $event_id, string $action): JsonResponse | ||
| { | ||
| try { | ||
| $event = Party::withTrashed()->findOrFail($event_id); | ||
| $result = self::performAction($event, $action); | ||
|
|
||
| return response()->json([ | ||
| 'success' => true, | ||
| 'message' => $result['message'], | ||
| 'event' => $result, | ||
| ]); | ||
| } catch (\Exception $e) { | ||
| Log::error('Error performing event action: ' . $e->getMessage()); | ||
|
|
||
| $statusCode = $e->getCode() === 409 ? 409 : 500; | ||
|
|
||
| return response()->json([ | ||
| 'success' => false, | ||
| 'message' => $e->getMessage(), | ||
| ], $statusCode); | ||
| } | ||
| } | ||
|
|
||
| private static function performAction(Party $event, string $action): array | ||
| { | ||
| switch ($action) { | ||
| case 'restore': | ||
| // Check if parent group is soft-deleted | ||
| $group = Group::withTrashed()->find($event->group); | ||
| if ($group && $group->trashed()) { | ||
| throw new \Exception("Cannot restore event: the parent group '{$group->name}' is deleted. Restore the group first.", 409); | ||
| } | ||
|
|
||
| $event->restore(); | ||
| break; | ||
|
|
||
| default: | ||
| throw new \Exception("Invalid action: {$action}"); | ||
| } | ||
|
|
||
| return [ | ||
| 'id' => $event->idevents, | ||
| 'venue' => $event->venue, | ||
| 'message' => "Event has been {$action}d successfully.", | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
database/migrations/2026_02_18_000000_add_soft_deletes_to_groups_table.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| public function up(): void | ||
| { | ||
| Schema::table('groups', function (Blueprint $table) { | ||
| $table->softDeletes()->index(); | ||
| }); | ||
| } | ||
|
|
||
| public function down(): void | ||
| { | ||
| Schema::table('groups', function (Blueprint $table) { | ||
| $table->dropSoftDeletes(); | ||
| }); | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's pretty cool.