-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,311 additions
and
54 deletions.
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
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,94 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Resources\StationMediaResource; | ||
use App\Models\Report; | ||
use App\Models\Station; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Validation\Rule; | ||
|
||
class StationReportController extends Controller | ||
{ | ||
public function index(Station $station, Request $request) | ||
{ | ||
$this->authorize('viewReport', $station); | ||
|
||
$report = $station->reports(); | ||
|
||
if ($request->show_all == false) { | ||
$report = $report->whereNull('resolved_at'); | ||
} | ||
|
||
$reports = $report->with('reporter')->latest()->get(); | ||
|
||
return view('reports.index', compact('reports', 'station')); | ||
} | ||
|
||
public function show(Station $station, $report) | ||
{ | ||
$this->authorize('viewReport', $station); | ||
|
||
$report = $station->reports()->findOrFail($report); | ||
|
||
return $report->load('reporter', 'media')->append('photo')->makeHidden('media'); | ||
} | ||
|
||
public function create(Station $station) | ||
{ | ||
// return Report::getConditions(); | ||
return view('reports.create', compact('station')); | ||
} | ||
|
||
public function store(Station $station, Request $request) | ||
{ | ||
$request->validate([ | ||
'condition' => [Rule::in(Report::getConditions()), 'required'], | ||
'user_latitude' => 'numeric|between:-90,90|nullable', | ||
'user_longitude' => 'numeric|between:-180,180|nullable', | ||
'photo' => 'file|image|nullable', | ||
]); | ||
|
||
/** @var Report */ | ||
$report = $station->reports()->create([ | ||
'condition' => $request->condition, | ||
'user_latitude' => $request->user_latitude, | ||
'user_longitude' => $request->user_longitude, | ||
'reporter_id' => $request->user()->id, | ||
]); | ||
|
||
if ($request->file('photo')) { | ||
$report->addMediaFromRequest('photo')->toMediaCollection(); | ||
} | ||
|
||
return $report->load('media')->append('photo')->makeHidden('media'); | ||
} | ||
|
||
public function resolve(Station $station, Request $request) | ||
{ | ||
$this->authorize('resolveReport', $station); | ||
|
||
$request->validate([ | ||
'resolve_all' => 'boolean', | ||
'report_id' => 'array|required_unless:resolve_all,true', | ||
'report_id.*' => 'integer', | ||
]); | ||
|
||
$report = $station->reports(); | ||
|
||
if ($request->resolve_all == true) { | ||
$report = $report->whereNull('resolved_at'); | ||
} else { | ||
$report = $report->whereIn('id', $request->report_id); | ||
} | ||
|
||
$report->update([ | ||
'resolved_at' => now(), | ||
'resolver_id' => $request->user()->id, | ||
]); | ||
|
||
return response([ | ||
'success' => true | ||
]); | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Http\Resources\StationMediaResource; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Spatie\MediaLibrary\HasMedia\HasMedia; | ||
use Spatie\MediaLibrary\HasMedia\HasMediaTrait; | ||
|
||
class Report extends Model implements HasMedia | ||
{ | ||
use HasFactory, HasMediaTrait; | ||
|
||
public static $conditions = [ | ||
'full' => 'Dropbox Penuh', | ||
'missing' => 'Dropbox Hilang/Tidak Pada Tempatnya', | ||
'damaged' => 'Dropbox Rusak' | ||
]; | ||
|
||
protected $guarded = ['id']; | ||
protected $dates = ['resolved_at']; | ||
protected $casts = [ | ||
'resolved_at' => 'datetime:Y-m-d\TH:i:sP', | ||
]; | ||
|
||
public static function getConditions() | ||
{ | ||
return array_keys(self::$conditions); | ||
} | ||
|
||
public function station() | ||
{ | ||
return $this->belongsTo('App\Models\Station'); | ||
} | ||
|
||
public function reporter() | ||
{ | ||
return $this->belongsTo('App\Models\User', 'reporter_id'); | ||
} | ||
|
||
public function resolver() | ||
{ | ||
return $this->belongsTo('App\Models\User', 'resolver_id'); | ||
} | ||
|
||
public function getPhotoAttribute() | ||
{ | ||
if ($media = $this->getFirstMedia()) { | ||
return new StationMediaResource($media); | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use App\Models\Report; | ||
use App\Models\Station; | ||
use App\Models\User; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class ReportFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var string | ||
*/ | ||
protected $model = Report::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array | ||
*/ | ||
public function definition() | ||
{ | ||
return [ | ||
'station_id' => Station::factory(), | ||
'reporter_id' => User::factory(), | ||
'user_latitude' => $this->faker->latitude, | ||
'user_longitude' => $this->faker->longitude, | ||
'condition' => $this->faker->randomElement(Report::getConditions()), | ||
'resolved_at' => null, | ||
'resolver_id' => null, | ||
]; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
database/migrations/2021_05_18_073320_create_reports_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,38 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateReportsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('reports', function (Blueprint $table) { | ||
$table->id(); | ||
$table->foreignId('station_id')->index(); | ||
$table->foreignId('reporter_id')->index(); | ||
$table->string('user_latitude')->nullable(); | ||
$table->string('user_longitude')->nullable(); | ||
$table->string('condition')->nullable(); | ||
$table->timestamp('resolved_at')->nullable(); | ||
$table->foreignId('resolver_id')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('reports'); | ||
} | ||
} |
Oops, something went wrong.