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

Contribution controller #3

Merged
merged 10 commits into from
Jul 28, 2021
2 changes: 1 addition & 1 deletion app/Http/Controllers/SaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function index(): AnonymousResourceCollection
{
$this->authorize("viewAny", Save::class);

return SimpleSaveResource::collection(Save::with("contributorss")->simplePaginate());
return SimpleSaveResource::collection(Save::with("contributors")->simplePaginate());
}

/**
Expand Down
95 changes: 79 additions & 16 deletions app/Http/Controllers/SharedSaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,126 @@

namespace App\Http\Controllers;

use App\Http\Resources\SharedSaveResource;
use App\Models\Save;
use App\Models\SharedSave;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\Response;

class SharedSaveController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
* @return AnonymousResourceCollection
*/
public function index()
{
//
$this->authorize("viewAny", SharedSave::class);

return SharedSaveResource::collection(SharedSave::simplePaginate());
}


public function indexSave(Save $save)
{
$this->authorize("viewOfSave", [SharedSave::class, $save]);
return SharedSaveResource::collection($save->sharedSaves()->simplePaginate());
}


public function indexUser(User $user, User $model)
{
$this->authorize("viewOfUser", [SharedSave::class, $model]);
return SharedSaveResource::collection($user->sharedSaves()->simplePaginate());
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
* @param Request $request
* @return Response
*/
public function store(Request $request)
public function store(Request $request, Save $save, User $user)
{
//
$validated = $request->validate([
"permission" => ["required", "integer", "min:0", "max:2"]
]);
$this->authorize("create", [SharedSave::class, $save]);
$shared_save = new SharedSave($validated);
$shared_save->user_id = $user->id;
$shared_save->accepted = false;
$save->invitations()->save($shared_save);
return response()->created("contribution", $shared_save);
}

/**
* Display the specified resource.
*
* @param \App\Models\SharedSave $sharedSave
* @return \Illuminate\Http\Response
* @param SharedSave $sharedSave
* @return SharedSaveResource
*/
public function show(SharedSave $sharedSave)
{
//
$this->authorize("view", $sharedSave);

return new SharedSaveResource($sharedSave);
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\SharedSave $sharedSave
* @return \Illuminate\Http\Response
* @param Request $request
* @param SharedSave $sharedSave
* @return Response
*/
public function update(Request $request, SharedSave $sharedSave)
{
//
$this->authorize("update", $sharedSave);
$validated = $request->validate([
"permission" => ["integer", "min:0", "min:2"],
"revoked" => ["boolean"],
]);
$sharedSave->fill($validated);
$sharedSave->save();
return \response()->noContent(Response::HTTP_OK);
}


public function accept(Request $request, SharedSave $sharedSave)
{
$this->authorize("acceptDecline", $sharedSave);
if (!$sharedSave->revoked) {
$sharedSave->declined = false;
$sharedSave->accepted = true;
$sharedSave->save();
return \response()->noContent(Response::HTTP_OK);
} else {
return \response(null, Response::HTTP_CONFLICT);
}
}

public function decline(Request $request, SharedSave $sharedSave)
{
$this->authorize("acceptDecline", $sharedSave);
$sharedSave->accepted = false;
$sharedSave->declined = true;
$sharedSave->save();
return \response(null, Response::HTTP_OK);
}

/**
* Remove the specified resource from storage.
*
* @param \App\Models\SharedSave $sharedSave
* @return \Illuminate\Http\Response
* @param SharedSave $sharedSave
* @return Response
*/
public function destroy(SharedSave $sharedSave)
{
//
$this->authorize("delete", $sharedSave);
$sharedSave->delete();
return \response(null, Response::HTTP_OK);
}
}
5 changes: 4 additions & 1 deletion app/Http/Resources/SaveResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public function toArray($request)
"tool_id"=>$this->tool_id,
"contributors"=>$this->contributors->map(function($c){
return $c->id;
})->toArray()
})->toArray(),
"invited"=>$this->invited->map(function($c){
return $c->id;
})->toArray(),
];
}
}
3 changes: 3 additions & 0 deletions app/Http/Resources/SharedSaveResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ class SharedSaveResource extends JsonResource
public function toArray($request)
{
return [
"id" => $this->id,
"user"=>$this->user_id,
"save"=>$this->save_id,
"permission"=>$this->permission,
"accepted" => $this->accepted,
"declined" => $this->declined,
"revoked" => $this->revoked
];
}
}
2 changes: 2 additions & 0 deletions app/Models/EmailVerification.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* @method static \Illuminate\Database\Eloquent\Builder|EmailVerification whereUserId($value)
* @mixin \Eloquent
* @property-read \App\Models\User $user
* @property string $token
* @method static \Illuminate\Database\Eloquent\Builder|EmailVerification whereToken($value)
*/
class EmailVerification extends Model
{
Expand Down
2 changes: 2 additions & 0 deletions app/Models/InvitationLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* @method static \Illuminate\Database\Eloquent\Builder|InvitationLink whereUpdatedAt($value)
* @mixin \Eloquent
* @property-read \App\Models\Save $safe
* @property string $token
* @method static \Illuminate\Database\Eloquent\Builder|InvitationLink whereToken($value)
*/
class InvitationLink extends Model
{
Expand Down
23 changes: 17 additions & 6 deletions app/Models/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Helper\PermissionHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
Expand Down Expand Up @@ -51,6 +52,8 @@
* @method static \Illuminate\Database\Query\Builder|Save withoutTrashed()
* @method static \Database\Factories\SaveFactory factory(...$parameters)
* @method static \Illuminate\Database\Eloquent\Builder|Save whereName($value)
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\SharedSave[] $sharedSaves
* @property-read int|null $shared_saves_count
*/
class Save extends Model
{
Expand Down Expand Up @@ -100,28 +103,36 @@ public function tool(): \Illuminate\Database\Eloquent\Relations\BelongsTo
return $this->belongsTo(Tool::class);
}

public function sharedSaves(): HasMany
{
return $this->hasMany(SharedSave::class);
}

public function invited(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(User::class, 'invite')->using(SharedSave::class)->as("invitation")
->withPivot(["permission", "accepted"])
return $this->belongsToMany(User::class, 'shared_save')->using(SharedSave::class)
->withPivot(["permission", "accepted", "declined", "revoked"])
->withPivotValue("accepted", false)
->withTimestamps();
}

public function invitations(): \Illuminate\Database\Eloquent\Relations\HasMany
public function invitations(): HasMany
{
return $this->hasMany(SharedSave::class)->where("accepted", '=', false);
return $this->hasMany(SharedSave::class)
->where("accepted", '=', false);
}

public function contributors(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(User::class, 'shared_save')->using(SharedSave::class)
->withPivot(["permission", "accepted"])
->withPivot(["permission", "accepted", "declined", "revoked"])
->withPivotValue("accepted", true)
->withPivotValue("declined", false)
->withPivotValue("revoked", false)
->withTimestamps();
}

public function invitationLinks(): \Illuminate\Database\Eloquent\Relations\HasMany
public function invitationLinks(): HasMany
{
return $this->hasMany(InvitationLink::class);
}
Expand Down
5 changes: 4 additions & 1 deletion app/Models/SharedSave.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* @method static \Illuminate\Database\Eloquent\Builder|SharedSave whereSaveId($value)
* @method static \Illuminate\Database\Eloquent\Builder|SharedSave whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|SharedSave whereUserId($value)
* @property int $declined
* @method static \Illuminate\Database\Eloquent\Builder|SharedSave whereDeclined($value)
*/
class SharedSave extends Pivot
{
Expand All @@ -40,6 +42,7 @@ class SharedSave extends Pivot
*/
protected $fillable = [
'permission',
'revoked'
];

/**
Expand Down Expand Up @@ -69,7 +72,7 @@ public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function safe(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Save::class);
return $this->belongsTo(Save::class,"save_id");
}

}
14 changes: 13 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
* @property-read int|null $clients_count
* @property-read \Illuminate\Database\Eloquent\Collection|\Laravel\Passport\Token[] $tokens
* @property-read int|null $tokens_count
* @method static \Illuminate\Database\Eloquent\Builder|User whereLastActivity($value)
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\SharedSave[] $sharedSaves
* @property-read int|null $shared_saves_count
*/
class User extends Authenticatable
{
Expand Down Expand Up @@ -118,19 +121,28 @@ public function invitedSaves(): BelongsToMany
return $this->belongsToMany(Save::class, 'shared_save')->using(SharedSave::class)
->withPivot(["permission"])
->withPivotValue("accepted", false)
->withPivotValue("declined", false)
->withTimestamps();
}

public function invitations(): HasMany
{
return $this->hasMany(SharedSave::class)->where('accepted', '=', false);
return $this->hasMany(SharedSave::class)
->where('accepted', '=', false)
->where('declined', '=', false);
}

public function sharedSaves(): HasMany
{
return $this->hasMany(SharedSave::class);
}

public function accessibleShares(): BelongsToMany
{
return $this->belongsToMany(Save::class, 'shared_save')->using(SharedSave::class)
->withPivot(["permission"])
->withPivotValue("accepted", true)
->withPivotValue("declined",false)
->withTimestamps();
}

Expand Down
Loading