Skip to content
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

Update MemoriesController and MemoriesControllerTest for CRUD functionality #19

Closed
wants to merge 2 commits into from

Conversation

FaerieAI
Copy link

@FaerieAI FaerieAI commented Dec 1, 2023

Summary of changes:

  • Updated MemoriesController.php to use Memory::create() and Memory::update() methods instead of manually setting each property.
  • Added validation and error handling for read, update, and delete methods.
  • Updated Memory model to use guarded instead of fillable.
  • Added tests for create, read, update, and delete methods in MemoriesControllerTest.php.

Individual changes:

  • In MemoriesController.php, updated create method to use Memory::create() method instead of manually setting each property. This improves readability and reduces the amount of code needed.
  • In MemoriesController.php, added validation and error handling for read, update, and delete methods to ensure proper handling of invalid requests.
  • In MemoriesController.php, updated Memory model to use guarded instead of fillable to prevent mass assignment vulnerabilities.
  • In MemoriesControllerTest.php, updated test_can_create_memory method to use Memory::factory()->create() to generate a memory object instead of manually setting each property. This improves readability and reduces the amount of code needed.
  • In MemoriesControllerTest.php, added tests for read, update, and delete methods to ensure proper functionality of each API endpoint.
  • In MemoriesControllerTest.php, added assertDatabaseHas and assertDeleted assertions to verify that the database was properly updated after each operation.

For additional context, here were my instructions:


A description of your next task is:
Sure. Here are the steps for 1.2, the CRUD operations via the MemoriesController.

Step 1.2.1: In app/Http/Controllers/MemoriesController.php, generate controller code like this:

<?php

namespace App\Http\Controllers;

use App\Models\Memory;
use Illuminate\Http\Request;

class MemoriesController extends Controller
{
    public function create(Request $request)
    {
        // Validation and creation logics go here
    }

    public function read(int $memoryId)
    {
        // Retrieval logic goes here
    }

    public function update(Request $request, int $memoryId)
    {
        // Update logic goes here
    }

    public function delete(int $memoryId)
    {
        // Delete logic goes here
    }
}

Step 1.2.2: In routes/api.php, add these routes:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MemoriesController;

Route::post('/memories', [MemoriesController::class, 'create']);
Route::get('/memories/{id}', [MemoriesController::class, 'read']);
Route::put('/memories/{id}', [MemoriesController::class, 'update']);
Route::delete('/memories/{id}', [MemoriesController::class, 'delete']);

Step 1.2.3: Update the Memory model in app/Models/Memory.php to use guarded instead of fillable:

class Memory extends Model
{
    use HasFactory;

    public $guarded = [];
}

Step 1.2.4: Under the tests/Feature/ directory, create new feature test MemoriesControllerTest.php:

<?php

namespace Tests\Feature;

use App\Models\Memory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class MemoriesControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_can_create_memory()
    {
        // Test the `create` method here
    }

    public function test_can_read_memory()
    {
        // Test the `read` method here
    }

    public function test_can_update_memory()
    {
        // Test the `update` method here
    }

    public function test_can_delete_memory()
    {
        // Test the `delete` method here
    }
}

Fill out the test methods for create, read, update, and delete. These tests will verify that the API endpoints for each operation work as expected.

Once done, run the tests to make sure everything operates as intended. After confirming the tests pass, encapsulate the changes in a new commit with a descriptive commit message.

Remember, the tests here are placeholders and need to be filled with specific tests based on the implementation of the controller methods. They ensure that your controller actions work as expected.

Copy link

github-actions bot commented Dec 1, 2023

⚠️ No test summary file found.

@AtlantisPleb AtlantisPleb deleted the vid32test4 branch December 1, 2023 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants