mirror of
https://github.com/brunofontes/shareit.git
synced 2024-11-24 04:14:57 +00:00
Bruno Fontes
e22f49bc6a
I made the code more passive, avoiding issued at taking, returning, storing alerts or removing alerts from an item. Now they all check if it is with you before returning/deleting alert etc. I am not sure if all cases are covered, but they are better than before. I had one only issued on this on that time, but I prefer to prioritize safety/security. I took the opportunitie to move some code from Controllers to the model itself, as they were changing with the DB.
100 lines
1.9 KiB
PHP
100 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use Auth;
|
|
use Lang;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Exception;
|
|
|
|
class Item extends Model
|
|
{
|
|
protected $fillable = ['product_id', 'name'];
|
|
|
|
public function product()
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
|
|
public function users()
|
|
{
|
|
return $this->belongsToMany(User::class);
|
|
}
|
|
|
|
public static function deleteAndDetach($item)
|
|
{
|
|
//Detach users from this item
|
|
foreach ($item->users as $user) {
|
|
User::findOrFail($user->id)->items()->detach($item->id);
|
|
}
|
|
|
|
//Delete item
|
|
$item->delete();
|
|
}
|
|
|
|
/**
|
|
* Return the items from logged in user
|
|
*
|
|
* @return \App\Item
|
|
*/
|
|
public static function fromAuthUser()
|
|
{
|
|
return (new static)->where('user_id', Auth::id());
|
|
}
|
|
|
|
/**
|
|
* Take a specified item
|
|
*
|
|
* @return void
|
|
*/
|
|
public function takeItem()
|
|
{
|
|
if (isset($this->used_by)) {
|
|
throw new Exception("Trying to take an Item that is in use", 1);
|
|
}
|
|
|
|
$this->used_by = Auth::id();
|
|
$this->waiting_user_id = null;
|
|
$this->save();
|
|
}
|
|
|
|
/**
|
|
* Return a specified item
|
|
*
|
|
* @return void
|
|
*/
|
|
public function returnItem()
|
|
{
|
|
if ($this->used_by != Auth::id()) {
|
|
throw new Exception("Trying to return an empty Item or from other user", 1);
|
|
}
|
|
|
|
$this->used_by = null;
|
|
$this->save();
|
|
}
|
|
|
|
/**
|
|
* Store a waiting user to the item
|
|
*
|
|
* @return void
|
|
*/
|
|
public function storeAlert()
|
|
{
|
|
$this->waiting_user_id = Auth::id();
|
|
$this->timestamps = false;
|
|
$this->save();
|
|
}
|
|
|
|
/**
|
|
* Remove a waiting user to the item
|
|
*
|
|
* @return void
|
|
*/
|
|
public function removeAlert()
|
|
{
|
|
$this->waiting_user_id = null;
|
|
$this->timestamps = false;
|
|
$this->save();
|
|
}
|
|
}
|