Skip to content

Commit

Permalink
create the modles and migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Amjad Ghzlan committed Jun 20, 2023
1 parent 04a43ad commit 224af67
Show file tree
Hide file tree
Showing 13 changed files with 599 additions and 3 deletions.
64 changes: 64 additions & 0 deletions Work Specifications
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Create a Laravel APIs application for a travel agency.

Glossary

Travel is the main unit of the project: it contains all the necessary information, like the number of days, the images, title, etc. An example is Japan: road to Wonder or Norway: the land of the ICE;
Tour is a specific dates-range of a travel with its own price and details. Japan: road to Wonder may have a tour from 10 to 27 May at €1899, another one from 10 to 15 September at €669 etc. At the end, you will book a tour, not a travel.

--------------------------------------
Goals

At the end, the project should have:

A private (admin) endpoint to create new users. If you want, this could also be an artisan command, as you like. It will mainly be used to generate users for this exercise;
A private (admin) endpoint to create new travels;
A private (admin) endpoint to create new tours for a travel;
A private (editor) endpoint to update a travel;
A public (no auth) endpoint to get a list of paginated travels. It must return only public travels;
A public (no auth) endpoint to get a list of paginated tours by the travel slug (e.g. all the tours of the travel foo-bar). Users can filter (search) the results by priceFrom, priceTo, dateFrom (from that startingDate) and dateTo (until that startingDate). User can sort the list by price asc and desc. They will always be sorted, after every additional user-provided filter, by startingDate asc.

-------------------------------------
Models

* Users

-ID
-Email
-Password
-Roles (M2M relationship)
-------------------------
* Roles

-ID
-Name

-----------------
* Travels

-ID
-Is Public (bool)
-Slug
-Name
-Description
-Number of days
-Number of nights (virtual, computed by numberOfDays - 1)
-------------------------
* Tours
-ID
-Travel ID (M2O relationship)
-Name
-Starting date
-Ending date
-Price (integer, see below)
---------------------------------------------------------

Notes

Feel free to use the native Laravel authentication.
We use UUIDs as primary keys instead of incremental IDs, but it's not required for you to use them, although highly appreciated;
Tours prices are integer multiplied by 100: for example, €999 euro will be 99900, but, when returned to Frontends, they will be formatted (99900 / 100);
Every admin user will also have the editor role;
Every creation endpoint, of course, should create one and only one resource. You can't, for example, send an array of resource to create;
Usage of php-cs-fixer and larastan are a (plus);
Creating docs is (big plus);
Feature tests are a (big big plus) .
14 changes: 14 additions & 0 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
use HasFactory , HasUuids;

protected $fillable= ['name'];
}
29 changes: 29 additions & 0 deletions app/Models/Tour.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Tour extends Model
{
use HasFactory , HasUuids;

protected $fillable = [
'travel_id',
'name',
'starting_date',
'ending_date',
'price',
];

public function priceInCents() : Attribute
{
return Attribute::make(
get:fn($value) => $value / 100 ,
set:fn($value) => $value * 100
);
}
}
50 changes: 50 additions & 0 deletions app/Models/Travel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Models;

use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Travel extends Model
{
use HasFactory , Sluggable , HasUuids;


protected $table= 'travels';


protected $fillable = [
'is_public',
'slug',
'name',
'description',
'number_of_days',
];

public function tours() :HasMany
{
return $this->hasMany(Tour::class);
}

public function sluggable(): array
{
return [
'slug' => [
'source' => 'name'
]
];
}


public function numberOfNights() :Attribute
{
return Attribute::make(
get: fn ($value , $attributes) => $attributes['number_of_days'] - 1
);

}
}
16 changes: 15 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use \Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
use HasApiTokens, HasFactory, Notifiable , HasUuids;

/**
* The attributes that are mass assignable.
Expand Down Expand Up @@ -42,4 +45,15 @@ class User extends Authenticatable
'email_verified_at' => 'datetime',
'password' => 'hashed',
];

/**
* The roles that belong to the User
*
*
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class, 'role_user_table', 'user_id', 'role_id');
}

}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"require": {
"php": "^8.1",
"cviebrock/eloquent-sluggable": "^10.0",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.2",
Expand Down
148 changes: 147 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 224af67

Please sign in to comment.