Skip to content

Stylemix Test Task, Laravel based API for grocery list

Notifications You must be signed in to change notification settings

b1mt3/laravel_task

Repository files navigation

laravel_task

Stylemix Test Task, Laravel based API for grocery list

Steps of the DEVELOPMENT process

  1. 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
  1. 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

  1. prepare the database and initiate the migration php artisan migrate
  2. create a GroceryList resource php artisan make:resource GroceryList
  3. 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,
  ];
}
  1. 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));
}
  1. add a new route in order to parse the address bar query in accordance with the above controller:
Route::get('/grocerylist/{id}', 'GroceryListController@show');
  1. 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).

Steps of the TESTING process

  1. create GroceryList table seeder and populate with:
public function run()
{
    factory(App\GroceryList::class, 10)->create();
}
  1. populate the DatabaseSeeder.php file with:
use Illuminate\Database\Eloquent\Model;

public function run()
{
    Model::unguard();

    $this->call(GroceryListTableSeeder::class);

    Model::reguard();
}
  1. 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,
    ];
});
  1. execute the php artisan migrate --seed to fill the tables with a sample data.

References

About

Stylemix Test Task, Laravel based API for grocery list

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published