Skip to content

Commit d159cea

Browse files
author
Volodymyr Ponomarenko
committed
Init
1 parent bf607d1 commit d159cea

File tree

11 files changed

+300
-21
lines changed

11 files changed

+300
-21
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
start:
2+
docker run -p 80:8181 peace-for-hair
3+
4+
build:
5+
docker build -t peace-for-hair .
6+
7+
stop:
8+
docker stop php-laravel-docker

app/Clients.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Clients extends Model
9+
{
10+
use HasFactory;
11+
/*
12+
$table->string('first_name');
13+
$table->string('second_name');
14+
$table->string('third_name');
15+
$table->enum('sex', ['male', 'female']);
16+
*/
17+
protected $fillable = [
18+
'first_name',
19+
'second_name',
20+
'third_name',
21+
'sex',
22+
'has_discount',
23+
];
24+
}

app/Haircuts.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Haircuts extends Model
9+
{
10+
use HasFactory;
11+
12+
/*
13+
$table->string('name');
14+
$table->float('cost');
15+
$table->enum('sex', ['male', 'female']);
16+
*/
17+
protected $fillable = [
18+
'name',
19+
'cost',
20+
'sex',
21+
];
22+
}

app/Http/Controllers/HomeController.php

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,60 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Clients;
6+
use App\Haircuts;
7+
use App\Works;
58
use Illuminate\Http\Request;
69

710
class HomeController extends Controller
811
{
9-
/**
10-
* Create a new controller instance.
11-
*
12-
* @return void
13-
*/
14-
public function __construct()
15-
{
16-
$this->middleware('auth');
17-
}
18-
1912
/**
2013
* Show the application dashboard.
2114
*
2215
* @return \Illuminate\Contracts\Support\Renderable
2316
*/
2417
public function index()
2518
{
26-
return view('home');
19+
return view('welcome', [
20+
'haircuts' => Haircuts::all()->toArray() ?? [],
21+
'works' => Works::with('client')->with('haircut')->get()->toArray() ?? [],
22+
]);
23+
}
24+
25+
public function create()
26+
{
27+
$client = Clients::where([
28+
['first_name', request()->get('first_name')],
29+
['second_name', request()->get('second_name')],
30+
['third_name', request()->get('third_name')],
31+
])->first();
32+
33+
if (!$client) {
34+
$client = Clients::create([
35+
'first_name' => request()->get('first_name'),
36+
'second_name' => request()->get('second_name'),
37+
'third_name' => request()->get('third_name'),
38+
]);
39+
40+
$client->save();
41+
}
42+
43+
$work = Works::create([
44+
'haircut_id' => request()->get('haircut_id'),
45+
'client_id' => $client->id,
46+
]);
47+
48+
$work->save();
49+
50+
51+
$count = Works::where('client_id', $client->id)->get()->count();
52+
53+
if ($count > 4) {
54+
$client->has_discount = true;
55+
56+
$client->save();
57+
}
58+
59+
return redirect()->route('home');
2760
}
2861
}

app/Works.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Works extends Model
9+
{
10+
use HasFactory;
11+
12+
/**
13+
* $table->foreignId('haircut_id')->references('id')->on('haircuts');
14+
* $table->foreignId('client_id')->references('id')->on('clients');
15+
*
16+
* @var array
17+
*/
18+
protected $fillable = [
19+
'haircut_id',
20+
'client_id',
21+
];
22+
23+
public function client()
24+
{
25+
return $this->belongsTo(Clients::class);
26+
}
27+
28+
public function haircut()
29+
{
30+
return $this->belongsTo(Haircuts::class);
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('clients', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('first_name');
17+
$table->string('second_name');
18+
$table->string('third_name');
19+
$table->enum('sex', ['male', 'female']);
20+
$table->boolean('has_discount')->default(false);
21+
$table->timestamps();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*/
28+
public function down(): void
29+
{
30+
Schema::dropIfExists('clients');
31+
}
32+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('haircuts', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('name');
17+
$table->float('cost');
18+
$table->enum('sex', ['male', 'female']);
19+
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*/
27+
public function down(): void
28+
{
29+
Schema::dropIfExists('haircuts');
30+
}
31+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('works', function (Blueprint $table) {
15+
$table->id();
16+
$table->foreignId('haircut_id')->references('id')->on('haircuts');
17+
$table->foreignId('client_id')->references('id')->on('clients');
18+
$table->timestamps();
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*/
25+
public function down(): void
26+
{
27+
Schema::dropIfExists('works');
28+
}
29+
};

database/seeds/DatabaseSeeder.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,29 @@ class DatabaseSeeder extends Seeder
1212
public function run()
1313
{
1414
// $this->call(UsersTableSeeder::class);
15+
16+
\App\Haircuts::create([
17+
'cost' => 100,
18+
'name' => 'Haircut 1 male',
19+
'sex' => 'male',
20+
]);
21+
22+
\App\Haircuts::create([
23+
'cost' => 200,
24+
'name' => 'Haircut 2 female',
25+
'sex' => 'female',
26+
]);
27+
28+
\App\Haircuts::create([
29+
'cost' => 100,
30+
'name' => 'Haircut 3 male',
31+
'sex' => 'male',
32+
]);
33+
34+
\App\Haircuts::create([
35+
'cost' => 250,
36+
'name' => 'Haircut 4 female',
37+
'sex' => 'female',
38+
]);
1539
}
1640
}

resources/views/welcome.blade.php

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,54 @@
11
<!DOCTYPE html>
22
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
3-
<head>
4-
<title>Volodymyr Ponomarenko</title>
5-
</head>
6-
<body>
7-
<h1>Hello from Volodymyr Ponomarenko KN-N223 (Lab 1)</h1>
8-
</body>
3+
<head>
4+
<title>haircut</title>
5+
</head>
6+
<body>
7+
<div>
8+
<h1>Peace of hair</h1>
9+
</div>
10+
<div>
11+
<form method="POST" action="/works">
12+
@csrf <!-- {{ csrf_field() }} -->
13+
@foreach($haircuts as $haircut)
14+
<div style="border: 1px black solid; margin-top: 10px">
15+
<div>
16+
Haircut number: <span>{{$haircut['id']}}</span>
17+
</div>
18+
<div>
19+
Haircut name: <span>{{$haircut['name']}}</span>
20+
</div>
21+
<div>
22+
Haircut sex: <span>{{$haircut['sex']}}</span>
23+
</div>
24+
</div>
25+
@endforeach
26+
<div style="margin-top: 20px">
27+
<div><span>First name</span><input name="first_name" placeholder="Enter first name"></div>
28+
<div><span>Second name</span><input name="second_name" placeholder="Enter second name"></div>
29+
<div><span>Third name</span><input name="third_name" placeholder="Enter third name"></div>
30+
<div><span>Number of hair cut(see on the top of page)</span><input name="haircut_id" placeholder="Enter haircut number"></div>
31+
<button>Save</button>
32+
</div>
33+
</form>
34+
</div>
35+
<div>
36+
@foreach ($works as $work)
37+
<div style="border: 1px black solid; margin-top: 10px">
38+
<div>
39+
Haircut name: <span>{{$work['haircut']['name']}}</span>
40+
</div>
41+
<div>
42+
Haircut cost: <span>{{$work['haircut']['cost']}}</span>
43+
</div>
44+
<div>
45+
Haircut client full name: <span>{{$work['client']['first_name'] . $work['client']['second_name'] . $work['client']['third_name']}}</span>
46+
</div>
47+
<div>
48+
Client has discount: <span>{{$work['client']['has_discount'] }}</span>
49+
</div>
50+
</div>
51+
@endforeach
52+
</div>
53+
</body>
954
</html>

0 commit comments

Comments
 (0)