Skip to content

Commit

Permalink
study sheet model, route, controller, test
Browse files Browse the repository at this point in the history
  • Loading branch information
christhofer committed Jul 5, 2022
1 parent 56bb94c commit b58fead
Show file tree
Hide file tree
Showing 10 changed files with 489 additions and 8 deletions.
114 changes: 114 additions & 0 deletions app/Http/Controllers/Api/Plugin/Study/StudySheetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace App\Http\Controllers\Api\Plugin\Study;

use App\Http\Controllers\Controller;
use App\Model\Master\User;
use App\Model\Plugin\Study\StudySheet;
use Illuminate\Http\Request;

class StudySheetController extends Controller
{
/**
* Display a listing of the resource.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
return StudySheet::eloquentFilter($request)
->fields($request->get('fields'))
->where('user_id', auth()->id())
->paginate();

// search
// filter
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// $request->file('video');
// $request->file('voice');
// $request->file('photo');

$validated = $request->all();
// $validated = $request->validated();

$sheet = new StudySheet();
$sheet->fill($validated);
$sheet->user_id = auth()->id();
$sheet->save();

return $sheet;
}

/**
* Display the specified resource.
*
* @param \App\Model\Plugin\Study\StudySheet $sheet
* @return \Illuminate\Http\Response
*/
public function show(StudySheet $sheet)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param \App\Model\Plugin\Study\StudySheet $sheet
* @return \Illuminate\Http\Response
*/
public function edit(StudySheet $sheet)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Model\Plugin\Study\StudySheet $sheet
* @return \Illuminate\Http\Response
*/
public function update(Request $request, StudySheet $sheet)
{
$validated = $request->all();
// $validated = $request->validated();
$sheet->update($validated);
return $sheet;
}

/**
* Remove the specified resource from storage.
*
* @param \App\Model\Plugin\Study\StudySheet $sheet
* @return \Illuminate\Http\Response
*/
public function destroy(StudySheet $sheet)
{
return $sheet->delete();

// delete photo from drive
// delete voice from drive
// delete video from drive
// delete from database
}
}
29 changes: 29 additions & 0 deletions app/Model/Plugin/Study/StudySheet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Model\Plugin\Study;

use App\Model\PointModel;

class StudySheet extends PointModel
{
protected $connection = 'tenant';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'started_at',
'ended_at',
'subject_id',
'institution',
'teacher',
'competency',
'learning_goals',
'activities',
'grade',
'behavior',
'remarks',
];
}
4 changes: 2 additions & 2 deletions app/Model/Plugin/Study/StudySubject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Model\Plugin\Study;

use Illuminate\Database\Eloquent\Model;
use App\Model\PointModel;

class StudySubject extends Model
class StudySubject extends PointModel
{
protected $connection = 'tenant';

Expand Down
71 changes: 71 additions & 0 deletions app/Services/Google/Drive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

class Drive
{
private \Google_Client $client;
private \Google\Service\Drive $service;
private \Masbug\Flysystem\GoogleDriveAdapter $adapter;
private \League\Flysystem\Filesystem $driver;
private \Illuminate\Filesystem\FilesystemAdapter $disk;

public function __construct()
{
$this->client = Google::client();

// https://github.com/masbug/flysystem-google-drive-ext#using-with-laravel-framework
$this->service = new \Google\Service\Drive($this->client);
$this->adapter = new \Masbug\Flysystem\GoogleDriveAdapter($this->service);
$this->driver = new \League\Flysystem\Filesystem($this->adapter);
$this->disk = new \Illuminate\Filesystem\FilesystemAdapter($this->driver, $this->adapter);
}

/**
* Upload file to user's Google Drive account, set permission, get file id and path.
*
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $file
* @param string $dir
* @return array
*/
public function store($file, $dir = '')
{
if (empty($dir)) {
$dir = config('services.google.drive.root');
}

// Upload file to google drive, get the filename and extension
$filename = $this->disk->putFile($dir, $file);

// get file metadata to get the file id
// file id is used to set permission and get preview link
$metadata = $this->adapter->getMetadata($filename);

$fileId = $metadata['id'];

// set file permission to "Anyone on the internet with this link can view"
// https://stackoverflow.com/a/57450358/3671954
$newPermission = new \Google\Service\Drive\Permission([
'type' => 'anyone',
'role' => 'reader',
'additionalRoles' => [],
'withLink' => true,
'value' => ''
]);
$this->service->permissions->create($fileId, $newPermission);

return [
'id' => $fileId,
'path' => implode('/', [$dir, $filename]),
];
}

/**
* Delete existing file from user's Google Drive account.
*
* @param string $fileId
* @return void
*/
public function destroy(string $path)
{
$this->disk->delete($path);
}
}
20 changes: 20 additions & 0 deletions app/Services/Google/Google.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

class Google
{
public static function client()
{
$oauthClientId = config('services.google.client_id');
$oauthClientSecret = config('services.google.client_secret');

// $client = new \Google\Client(); // newer version use this
$client = new \Google_Client();

$client->setClientId($oauthClientId);
$client->setClientSecret($oauthClientSecret);

// get user access / refresh token

return $client;
}
}
4 changes: 4 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,9 @@
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_REDIRECT'),
'project_id' => env('GOOGLE_PROJECT_ID'),
'drive' => [
'root' => env('GOOGLE_DRIVE_ROOT'),
],
],
];
25 changes: 25 additions & 0 deletions database/factories/Plugin/Study/StudySheetFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Model\Plugin\Study\StudySheet;
use App\Model\Plugin\Study\StudySubject;
use Faker\Generator as Faker;

$factory->define(StudySheet::class, function (Faker $faker) {
return [
'started_at' => now()->startOfHour(),
'ended_at' => function (array $attributes) {
return Carbon\Carbon::make($attributes['started_at'])->addHour();
},
'institution' => $faker->text(),
'teacher' => $faker->name(),
'competency' => $faker->text(),
'learning_goals' => $faker->text(),
'activities' => $faker->text(),
'grade' => $faker->numberBetween(0,100),
'behavior' => $faker->randomElement(['A', 'B', 'C']),
'remarks' => $faker->text(),
'subject_id' => factory(StudySubject::class),
];
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,29 @@ public function up()
{
Schema::create('study_sheets', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->datetime('date_start');
$table->string('photo', 1000); // TODO adjust length according to actual needs
$table->string('voice_note', 1000)->nullable(); // TODO adjust length according to actual needs
$table->string('video', 1000)->nullable(); // TODO adjust length according to actual needs
// $table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->unsignedInteger('user_id');
$table->datetime('started_at');
$table->datetime('ended_at');
$table->string('photo');
$table->string('voice_note')->nullable();
$table->string('video')->nullable();
$table->foreignId('subject_id')->constrained('study_subjects');
$table->string('institution')->nullable();
$table->string('teacher')->nullable();
$table->string('competency');
$table->string('learning_goals');
$table->string('activities')->nullable();
$table->unsignedTinyInteger('grade')->nullable();
$table->string('behavior');
$table->enum('behavior', ['A', 'B', 'C']);
$table->string('remarks')->nullable();
$table->boolean('is_draft');
$table->timestamps();

$table->foreign('user_id')
->references('id')
->on('users')
->cascadeOnDelete();
});
}

Expand Down
1 change: 1 addition & 0 deletions routes/api/plugin/study.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

Route::prefix('plugin/study')->namespace('Plugin\Study')->group(function () {
Route::resource('subject', 'StudySubjectController', ['as' => 'study']);
Route::resource('sheet', 'StudySheetController', ['as' => 'study']);
});
Loading

0 comments on commit b58fead

Please sign in to comment.