-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added ability to add users to a specific map
- Loading branch information
Showing
12 changed files
with
460 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,97 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Resources\UserResource; | ||
use App\Models\Map; | ||
use Illuminate\Http\Request; | ||
|
||
class MapUserController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the resource. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function index(Request $request, Map $map) | ||
{ | ||
// We'll use the "delete map" policy to check if the user can delete the map. If they can delete it, they can also view the users of it. We need to explicitly pass the policy name specifically | ||
$this->authorize('delete', $map); | ||
|
||
// Return the users of the map | ||
return UserResource::collection($map->users); | ||
} | ||
/** | ||
* Display a listing of the resource. The map uuid will be in the URL | ||
* Show the form for creating a new resource. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function store(Request $request, Map $map) | ||
{ | ||
// We'll use the "delete map" policy to check if the user can delete the map. If they can delete it, they can also add users to it. We need to explicitly pass the policy name specifically | ||
$this->authorize('delete', $map); | ||
|
||
$request->validate([ | ||
'username' => 'required|exists:users,username', | ||
'can_create_markers' => 'boolean', | ||
]); | ||
|
||
// Attach the user to the map. We need to find the user by the username | ||
$user = \App\Models\User::where('username', $request->username)->firstOrFail(); | ||
|
||
// The map hasMany users, so we can use the users() relationship to attach the user to the map | ||
$map->users()->attach($user->id, [ | ||
'can_create_markers' => $request->can_create_markers ?? false, | ||
]); | ||
|
||
// Return a 201 response | ||
return response()->json(null, 201); | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function update(Request $request, Map $map, $username) | ||
{ | ||
// We'll use the "delete map" policy to check if the user can delete the map. If they can delete it, they can also update users of it. We need to explicitly pass the policy name specifically | ||
$this->authorize('delete', $map); | ||
|
||
$request->validate([ | ||
'can_create_markers' => 'boolean', | ||
]); | ||
|
||
// Find the user by the username | ||
$user = \App\Models\User::where('username', $username)->firstOrFail(); | ||
|
||
// Update the user's can_create_markers attribute | ||
$map->users()->updateExistingPivot($user->id, [ | ||
'can_create_markers' => $request->can_create_markers ?? false, | ||
]); | ||
|
||
// Return a 204 response | ||
return response()->json(null, 204); | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function destroy(Request $request, Map $map, $username) | ||
{ | ||
// We'll use the "delete map" policy to check if the user can delete the map. If they can delete it, they can also remove users from it. We need to explicitly pass the policy name specifically | ||
$this->authorize('delete', $map); | ||
|
||
// Find the user by the username | ||
$user = \App\Models\User::where('username', $username)->firstOrFail(); | ||
|
||
// Detach the user from the map | ||
$map->users()->detach($user->id); | ||
|
||
// Return a 204 response | ||
return response()->json(null, 204); | ||
} | ||
} |
This file contains 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 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 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 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,54 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Relations\Pivot; | ||
|
||
class MapUser extends Pivot | ||
{ | ||
use HasFactory; | ||
|
||
public $incrementing = true; | ||
|
||
protected $table = 'map_users'; | ||
|
||
protected $fillable = [ | ||
'map_id', | ||
'user_id', | ||
'can_create_markers', | ||
'added_by_user_id', | ||
]; | ||
|
||
protected $hidden = ['added_by_user_id', 'map_id', 'user_id']; | ||
|
||
protected $casts = [ | ||
'can_create_markers' => 'boolean', | ||
]; | ||
|
||
// On create, by default we set the created_by_user_id to the user_id | ||
protected static function booted() | ||
{ | ||
static::creating(function ($mapUser) { | ||
$mapUser->added_by_user_id = $mapUser->added_by_user_id ?? optional(request()->user())->id; | ||
}); | ||
} | ||
|
||
// Define the relationship with the Map model | ||
public function map() | ||
{ | ||
return $this->belongsTo(Map::class); | ||
} | ||
|
||
// Define the relationship with the User model | ||
public function user() | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
|
||
// Define the relationship with the User model for the user who added this map user | ||
public function addedByUser() | ||
{ | ||
return $this->belongsTo(User::class, 'added_by_user_id'); | ||
} | ||
} |
This file contains 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 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 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
42 changes: 42 additions & 0 deletions
42
database/migrations/2024_04_02_195445_create_map_users_table.php
This file contains 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,42 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('map_users', function (Blueprint $table) { | ||
$table->id(); | ||
$table->integer('user_id')->unsigned(); | ||
$table->integer('map_id')->unsigned(); | ||
$table->integer('added_by_user_id')->unsigned(); | ||
$table->boolean('can_create_markers')->default(false); | ||
$table->timestamps(); | ||
|
||
$table->foreign('user_id')->references('id')->on('users'); | ||
$table->foreign('map_id')->references('id')->on('maps'); | ||
$table->foreign('added_by_user_id')->references('id')->on('users'); | ||
|
||
// We need to make sure that the user_id and map_id are unique together | ||
$table->unique(['user_id', 'map_id']); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('map_users'); | ||
} | ||
}; |
This file contains 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.