Skip to content

Commit 1c63762

Browse files
Share post and Remove share
1 parent 743b5d9 commit 1c63762

9 files changed

+1789
-1577
lines changed

Codelink.postman_collection.json

+1,697-1,572
Large diffs are not rendered by default.

app/Http/Controllers/Api/Home/CommentController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function deleteComment(Post $post, Comment $comment)
121121
// Delete the comment
122122
$comment->delete();
123123

124-
return $this->successMessage('Comment deleted successfully', 200);
124+
return $this->data(['post_id' => $post->id],'Comment deleted successfully', 200);
125125
} catch (\Exception $e) {
126126
return $this->errorMessage([], 'An error occurred while deleting the comment', 500);
127127
}

app/Http/Controllers/Api/Home/LikesController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function likePost(Post $post)
2525
// Update the likes_count in the posts table
2626
$post->decrement('likes_count');
2727

28-
return $this->successMessage('Post unliked successfully', 200);
28+
return $this->data(['post_id' => $post->id],'Post unliked successfully', 200);
2929
}
3030

3131
// Create a new like record for the authenticated user and the post
@@ -37,7 +37,7 @@ public function likePost(Post $post)
3737
// Update the likes_count in the posts table
3838
$post->increment('likes_count');
3939

40-
return $this->successMessage('Post liked successfully', 200);
40+
return $this->data(['post_id' => $post->id],'Post liked successfully', 200);
4141
}
4242

4343
public function getLikesForPost(Post $post)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Home;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\Request;
7+
use App\Models\Share;
8+
use App\Models\Post;
9+
use Illuminate\Support\Facades\Auth;
10+
use App\Traits\ApiTrait;
11+
12+
class ShareController extends Controller
13+
{
14+
use ApiTrait;
15+
16+
public function sharePost(Request $request, Post $post)
17+
{
18+
try {
19+
$user = Auth::user();
20+
21+
// Create a new share record for the authenticated user and the post
22+
$share = new Share();
23+
$share->user_id = $user->id;
24+
$share->post_id = $post->id;
25+
$share->owneruser_id = $post->user_id; // Set the owneruser_id to the original post owner's ID
26+
$share->save();
27+
28+
// Increment the shares_count in the posts table
29+
$post->increment('shares_count');
30+
31+
$postData = [
32+
'id' => $share->id,
33+
'post_id' => $post->id,
34+
"content" => $post->content,
35+
"image_path" => $post->image_path,
36+
'user_id' => $user->id,
37+
'user_name' => $user->name,
38+
'user_image' => $user->imageUrl,
39+
'owneruser_id' => $post->user_id,
40+
'owner_name' => $post->user->name,
41+
'owner_image' => $post->user->imageUrl,
42+
];
43+
44+
return $this->data($postData, 'Post shared successfully', 200);
45+
} catch (\Exception $e) {
46+
return $this->errorMessage([], 'An error occurred while sharing the post', 500);
47+
}
48+
}
49+
50+
51+
public function removeShare(Share $share)
52+
{
53+
$user = Auth::user();
54+
55+
// Check if the authenticated user owns this share
56+
if ($share->user_id === $user->id) {
57+
$post = $share->post;
58+
59+
// Delete the share
60+
$share->delete();
61+
62+
// Decrement the shares_count in the posts table
63+
$post->decrement('shares_count');
64+
65+
return $this->data(['share_id' => $share->id,'post_id' => $post->id], 'Share removed successfully', 200);
66+
}
67+
68+
return $this->errorMessage([], 'Share not found', 404);
69+
}
70+
}

app/Models/Post.php

+5
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ public function comments()
2929
{
3030
return $this->hasMany(Comment::class);
3131
}
32+
33+
public function shares()
34+
{
35+
return $this->hasMany(Share::class);
36+
}
3237
}

app/Models/User.php

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public function comments()
9494
return $this->hasMany(PostComment::class);
9595
}
9696

97+
public function shares()
98+
{
99+
return $this->hasMany(Share::class);
100+
}
101+
97102
public function communities()
98103
{
99104
return $this->belongsToMany(Community::class, 'community_users');

database/migrations/2023_08_31_215542_create_posts_table.php

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public function up()
1919
$table->unsignedInteger('likes_count')->default(0);
2020
$table->unsignedInteger('shares_count')->default(0);
2121
$table->unsignedBigInteger('user_id');
22-
$table->unsignedBigInteger('shareduser_id')->default(0);
2322
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
2423
$table->softDeletes();
2524
$table->timestamps();

database/migrations/2023_09_06_155854_create_shares_table.php

+2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ public function up()
1717
$table->id();
1818
$table->unsignedBigInteger('user_id');
1919
$table->unsignedBigInteger('post_id');
20+
$table->unsignedBigInteger('owneruser_id');
2021
$table->timestamps();
2122

2223
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
2324
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
25+
$table->foreign('owneruser_id')->references('id')->on('users')->onDelete('cascade');
2426
});
2527
}
2628

routes/api.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Http\Controllers\Api\Home\PostController;
1010
use App\Http\Controllers\Api\Home\CommentController;
1111
use App\Http\Controllers\Api\Home\LikesController;
12+
use App\Http\Controllers\Api\Home\ShareController;
1213
use App\Http\Controllers\Api\TeamController;
1314
use App\Http\Controllers\Api\TeamRequestController;
1415
use App\Http\Controllers\Api\TrackController;
@@ -103,7 +104,6 @@
103104
Route::post('/edit/{id}', 'editPost');
104105
Route::post('/delete/{id}', 'deletePost');
105106
Route::get('/user/{id}', 'getUserPosts');
106-
107107
});
108108

109109
// --------------------------------- Comment Controller ------------------------------------------
@@ -120,6 +120,12 @@
120120
Route::get('/likes', 'getLikesForPost');
121121
Route::post('/like', 'likePost');
122122
});
123+
// --------------------------------- Share Controller ------------------------------------------
124+
125+
Route::group(['middleware' => ['auth:sanctum'],'controller' => ShareController::class], function () {
126+
Route::post('posts/{post}/share', 'sharePost');
127+
Route::post('shares/{share}', 'removeShare');
128+
});
123129
// --------------------------------- Track Controller ------------------------------------------
124130

125131
Route::group(['prefix' => 'tracks', 'middleware' => ['auth:sanctum'], 'controller' => TrackController::class], function () {

0 commit comments

Comments
 (0)