Stylemix Test Task, Laravel based API for grocery list
- create 3 models: ProductGroup, Product (both migration- and factory-ready) and GroceryList (migration-, factory- and resource controller-ready)
php artisan make:model ProductGroup -mf
php artisan make:model Product -mf
php artisan make:model Grocerylist -crmf
- populate the migration files for the corresponding files:
public function up()
{
Schema::create('product_groups', function (Blueprint $table) {
$table->increments('id');
$table->string('group_name');
$table->timestamps();
});
}
database\migrations\product_group
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('product_name');
$table->integer('group_id')->unsigned();
$table->timestamps();
$table->foreign('group_id')->references('id')->on('product_groups');
});
}
database\migrations\product
public function up()
{
Schema::create('grocery_lists', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->integer('price');
$table->integer('quantity');
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products');
});
}
database\migrations\grocery_list
- prepare the database and initiate the migration
php artisan migrate
- create a GroceryList resource
php artisan make:resource GroceryList
- populate toArray() method of the resource:
public function toArray($request)
{
return [
'id' => $this->id,
'product' => $this->product_id,
'quantity' => $this->quantity,
'price' => $this->price,
'created_at' => (string)$this->created_at,
'updated_at' => (string)$this->updated_at,
];
}
- populate show() method of the GroceryListController to display a JSON:API formatted response:
public function show($id)
{
GroceryListResource::withoutWrapping();
return new GroceryListResource(GroceryList::find($id));
}
- add a new route in order to parse the address bar query in accordance with the above controller:
Route::get('/grocerylist/{id}', 'GroceryListController@show');
- populate the database with test information and execute
php artisan serve
to check the resulting response
Projecting on an abstract MVC-pattern, steps 1-3 reflect the preparation of the Model (M); steps 4 and 5 - the View (V); and the rest of the steps - the Controller (C).
- create GroceryList table seeder and populate with:
public function run()
{
factory(App\GroceryList::class, 10)->create();
}
- populate the DatabaseSeeder.php file with:
use Illuminate\Database\Eloquent\Model;
public function run()
{
Model::unguard();
$this->call(GroceryListTableSeeder::class);
Model::reguard();
}
- instruct factories for GroceryList, Product and ProductGroup on what to insert into the table:
$factory->define(App\GroceryList::class, function (Faker $faker) {
return [
'product_id' => function () {
return factory(App\Product::class)->create()->id;
},
'price' => $faker->randomNumber,
'quantity' => $faker->numberBetween($min = 1, $max = 1000),
];
});
$factory->define(App\Product::class, function (Faker $faker) {
return [
'product_name' => $faker->word,
'group_id'=> function () {
return factory(App\ProductGroup::class)->create()->id;
}
];
});
$factory->define(App\ProductGroup::class, function (Faker $faker) {
return [
'group_name' => $faker->word,
];
});
- execute the
php artisan migrate --seed
to fill the tables with a sample data.