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

Chapter6 #23

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added vuebnb/.DS_Store
Binary file not shown.
63 changes: 63 additions & 0 deletions vuebnb/app/Http/Controllers/ListingController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Http\Controllers;

use App\Listing;
use Illuminate\Http\Request;

class ListingController extends Controller
{
private function get_listing($listing)
{
$model = $listing->toArray();
for($i = 1; $i < 4; $i++) {
$model['image_' . $i] = asset(
'images/' . $listing->id . '/Image_' . $i . '.jpg'
);
}
return collect(['listing' => $model]);
}

private function add_meta_data($collection, $request)
{
return $collection->merge([
'path' => $request->getPathInfo()
]);
}

private function get_listing_summaries() {
$collection = Listing::all([
'id', 'address', 'title', 'price_per_night'
]);
$collection->transform(function($listing) {
$listing->thumb = asset(
'images/' . $listing->id . '/Image_1_thumb.jpg'
);
return $listing;
});
return collect(['listings' => $collection->toArray()]);
}


public function get_listing_api(Listing $listing) {
$data = $this->get_listing($listing);
return response()->json($data);
}

public function get_listing_web(Listing $listing, Request $request) {
$data = $this->get_listing($listing);
$data = $this->add_meta_data($data, $request);
return view('app', ['data' => $data]);
}

public function get_home_api() {
$data = $this->get_listing_summaries();
return response()->json($data);
}

public function get_home_web(Request $request) {
$data = $this->get_listing_summaries();
$data = $this->add_meta_data($data, $request);
return view('app', ['data' => $data]);
}
}
17 changes: 17 additions & 0 deletions vuebnb/app/Listing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Listing extends Model
{
protected $casts = [
'amenity_wifi' => 'boolean',
'amenity_pets_allowed' => 'boolean',
'amenity_tv' => 'boolean',
'amenity_kitchen' => 'boolean',
'amenity_breakfast' => 'boolean',
'amenity_laptop' => 'boolean',
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateListingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('listings', function (Blueprint $table) {
//
$table->primary('id');
$table->unsignedInteger('id');
$table->string('title');
$table->string('address');
$table->longText('about');

//amenities
$table->boolean('amenity_wifi')->default(false);
$table->boolean('amenity_pets_allowed')->default(false);
$table->boolean('amenity_tv')->default(false);
$table->boolean('amenity_kitchen')->default(false);
$table->boolean('amenity_breakfast')->default(false);
$table->boolean('amenity_laptop')->default(false);

//prices
$table->string('price_per_night')->nullable();
$table->string('price_extra_people')->nullable();
$table->string('price_weekly_discount')->nullable();
$table->string('price_monthly_discount')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('listings', function (Blueprint $table) {
//
});
}
}
2 changes: 1 addition & 1 deletion vuebnb/database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(ListingsTableSeeder::class);
}
}
21 changes: 21 additions & 0 deletions vuebnb/database/seeds/ListingsTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\File;

class ListingsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
$path = base_path() . '/database/data.json';
$file = File::get($path);
$data = json_decode($file, true);
DB::table('listings')->insert($data);
}
}
Loading