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

Product comment #655

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 64 additions & 3 deletions app/Http/Controllers/Api/V1/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace App\Http\Controllers\Api\V1;

use App\Models\OrganisationUser;
use App\Http\Requests\UpdateProductRequest;
use App\Models\Product;
use Illuminate\Http\Request;
use App\Models\ProductComment;
use App\Models\OrganisationUser;
use App\Http\Controllers\Controller;
use App\Http\Requests\CreateProductRequest;
use App\Http\Resources\ProductResource;
use App\Http\Requests\CreateProductRequest;
use App\Http\Requests\UpdateProductRequest;


class ProductController extends Controller
Expand Down Expand Up @@ -397,4 +398,64 @@ public function delete($product_id)
// 'data' => $product
], 204);
}

public function createComment(Request $request, $org_id, $product_id)
{
$product = Product::find($product_id);
if (!$product) {
return response()->json([
'status' => 'error',
"message" => "Product not found",
'status_code' => 404,
]);
}

$request->validate([
'comment' => 'required|string|max:500',
]);

$comment = ProductComment::create([
'user_id' => auth()->id(),
'product_id' => $product_id,
'comment' => $request->input('comment'),
]);

return response()->json([
'status_code' => 201,
'message' => 'Comment added successfully',
'comment' => $comment
], 201);
}

public function updateComment(Request $request, $product_id, $comment_id)
{
$comment = ProductComment::find($comment_id);
if (!$comment) {
return response()->json([
'status' => 'error',
"message" => "comment not found",
'status_code' => 404,
]);
}

$request->validate([
'comment' => 'required|string|max:500',
]);

$comment = ProductComment::where('id', $comment_id)->where('user_id', auth()->id())->first();

if (!$comment) {
return response()->json(['message' => 'Comment not found or not authorized to update'], 403);
}

$comment->update([
'comment' => $request->input('comment'),
]);

return response()->json([
'status_code' => 200,
'message' => 'Comment updated successfully',
'comment' => $comment
], 200);
}
}
28 changes: 28 additions & 0 deletions app/Models/ProductComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ProductComment extends Model
{
use HasFactory, HasUuids;

protected $fillable = [
'comment',
'product_id',
'user_id',
];

public function product()
{
return $this->belongsTo(Product::class);
}

public function user()
{
return $this->belongsTo(User::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('product_comments', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->longText('comment');
$table->foreignUuid('product_id')->constrained()->references('product_id')->on('products')->onDelete('cascade')->onUpdate('cascade');
$table->foreignUuid('user_id')->constrained()->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('product_comments');
}
};
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@
Route::post('/organisations/{org_id}/products', [ProductController::class, 'store']);
Route::patch('/organisations/{org_id}/products/{product_id}', [ProductController::class, 'update']);
Route::delete('/organisations/{org_id}/products/{product_id}', [ProductController::class, 'destroy']);
Route::post('/organisations/{org_id}/products/{product_id}/comments', [ProductController::class, 'createComment']);
Route::put('/organisations/{product_id}/comments/{comment_id}', [ProductController::class, 'updateComment']);
});


Expand Down
111 changes: 111 additions & 0 deletions tests/Feature/ProductCommentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Tests\Feature;

use Tests\TestCase;
use App\Models\User;
use App\Models\Product;
use App\Models\Organisation;
use App\Models\OrganisationUser;
use App\Models\ProductComment;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ProductCommentTest extends TestCase
{
use RefreshDatabase;

/**
* Test creating a new product comment.
*/
public function test_it_can_create_a_new_product_comment(): void
{
// Create a user and authenticate
$user = User::factory()->create();
$this->actingAs($user);

// Create an organisation
$organisation = Organisation::factory()->create();

// Associate the user with the organization as an owner
OrganisationUser::create([
'org_id' => $organisation->org_id,
'user_id' => $user->id,
]);

// Create a product
$product = Product::factory()->create([
'org_id' => $organisation->org_id,
'user_id' => $user->id,
]);

// Create a product comment
$response = $this->postJson("/api/v1/organisations/{$organisation->org_id}/products/{$product->product_id}/comments", [
'product_id' => $product->product_id,
'comment' => 'This is a test comment',
]);

// Assert the response status and structure
$response->assertStatus(201 )
->assertJson([
'status_code' => 201,
'message' => 'Comment added successfully',
]);

// Assert the comment exists in the database
$this->assertDatabaseHas('product_comments', [
'product_id' => $product->product_id,
'comment' => 'This is a test comment',
]);
}

/**
* Test updating a product comment.
*/
public function test_it_can_update_product_comment(): void
{
// Create a user and authenticate
$user = User::factory()->create();
$this->actingAs($user);

// Create an organisation
$organisation = Organisation::factory()->create();

// Associate the user with the organization as an owner
OrganisationUser::create([
'org_id' => $organisation->org_id,
'user_id' => $user->id,
]);

// Create a product
$product = Product::factory()->create([
'org_id' => $organisation->org_id,
'user_id' => $user->id,
]);

// Create a product comment
$comment = ProductComment::create([
'product_id' => $product->product_id,
'user_id' => $user->id,
'comment' => 'Initial comment',
]);

// Update the product comment
$response = $this->putJson("/api/v1/organisations/{$product->product_id}/comments/{$comment->id}", [
'comment' => 'Updated comment',
]);

// Assert the response status and structure
$response->assertStatus(200)
->assertJson([
'status_code' => 200,
'message' => 'Comment updated successfully',
]);

// Assert the comment was updated in the database
$this->assertDatabaseHas('product_comments', [
'id' => $comment->id,
'comment' => 'Updated comment',
]);
}
}
Loading