Skip to content

Commit

Permalink
add-file-upload-s3
Browse files Browse the repository at this point in the history
  • Loading branch information
RikuHirose committed Oct 27, 2019
1 parent f957a92 commit 2dbd363
Show file tree
Hide file tree
Showing 15 changed files with 3,387 additions and 28 deletions.
48 changes: 47 additions & 1 deletion app/Http/Controllers/Api/RoomController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use App\Http\Controllers\Controller;
use App\Services\ReserveTimeOptionService;
use Illuminate\Http\Request;

use Carbon\Carbon;
use App\Models\Room;
use App\Models\RoomFile;
use App\Models\File;

/**
* House Room
Expand All @@ -18,6 +20,8 @@ class RoomController extends Controller
public function __construct()
{
$this->middleware('auth:api');
$this->file = new File();
$this->roomFile = new RoomFile();
}

public function index()
Expand All @@ -34,4 +38,46 @@ public function show(Request $request, Room $room)
return response()->json(['room' => $room]);
}

public function create(Request $request)
{
$reserveTimeOptionService = new ReserveTimeOptionService();
$now = Carbon::now();
$timeOptions = $reserveTimeOptionService->makeTimeOptions($now);

return response()->json([
'timeOptions' => $timeOptions,
'baseTime' => $now
]);
}

public function store(Request $request)
{
$validator = \Validator::make($request->all(), [
'room_id' => 'required',
'bg_image' => 'required|mimes:jpg,png,webp'
]);

if ($validator->fails()) {
return redirect('/rooms');
}

\DB::transaction(function () use ($request) {

if ($request->hasFile('bg_image')) {
$file = $request->file('bg_image');
$fileUrl = $this->file->upload($file);

$fileModel = $this->file->create([
'url' => $fileUrl
]);

$this->roomFile->create([
'room_id' => $request->get('room_id'),
'file_id' => $fileModel->id
]);
}
});

return response()->json([]);
}
}
9 changes: 9 additions & 0 deletions app/Models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storage;

class File extends Model
{
Expand All @@ -17,4 +18,12 @@ class File extends Model
];

// Relations

public function upload($image)
{
$path = Storage::disk('s3')->putFile('reserbath', $image, 'public');
$url = Storage::disk('s3')->url($path);

return $url;
}
}
2 changes: 1 addition & 1 deletion app/Services/ReserveTimeOptionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Services;

use App\Reservation;
use App\Models\Reservation;
use Carbon\Carbon;

/**
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"laravel/slack-notification-channel": "^2.0",
"laravel/socialite": "^4.1",
"laravel/tinker": "^1.0",
"laravelcollective/html": "^5.8"
"laravelcollective/html": "^5.8",
"league/flysystem-aws-s3-v3": "^1.0"
},
"require-dev": {
"beyondcode/laravel-dump-server": "^1.0",
Expand Down
Loading

0 comments on commit 2dbd363

Please sign in to comment.