Skip to content

Commit 7f291d1

Browse files
committed
corrections for a high rating
1 parent 75032b7 commit 7f291d1

39 files changed

+14424
-361
lines changed

RentalVOD_archive.zip

21.8 MB
Binary file not shown.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class UpdateMoviePrices extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'app:update-movie-prices';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Command description';
22+
23+
/**
24+
* Execute the console command.
25+
*/
26+
public function handle()
27+
{
28+
//
29+
}
30+
}

app/Http/Controllers/AdminController.php

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Http\Requests\AddMovieRequest;
77
use App\Http\Requests\UpdateUserRequest;
88
use App\Http\Requests\AddCategoryRequest;
9+
use Illuminate\Http\Request;
910
use App\Models\User;
1011
use App\Models\Movie;
1112
use App\Models\Loan;
@@ -36,11 +37,10 @@ public function movies()
3637
$movies = Movie::paginate(10);
3738

3839

39-
foreach ($movies as $movie) {
40-
$dynamicPrice = $this->movieService->calculateDynamicPrice($movie);
41-
$movie->price_day = $dynamicPrice;
42-
$movie->save();
43-
}
40+
// foreach ($movies as $movie) {
41+
// $movie->price_day;
42+
// $movie->save();
43+
// }
4444

4545
return view('admin.editMovies', compact('movies'));
4646
}
@@ -55,7 +55,7 @@ public function showUsers()
5555
public function updateMovie(UpdateMovieRequest $request, $id)
5656
{
5757
$movie = Movie::findOrFail($id);
58-
$data = $request->all();
58+
$data = $request->validated();
5959

6060
if ($request->hasFile('img_path')) {
6161
Storage::delete($movie->img_path);
@@ -64,21 +64,14 @@ public function updateMovie(UpdateMovieRequest $request, $id)
6464
$data['img_path'] = 'img/' . basename($imagePath);
6565
}
6666

67-
$category = Category::find($request->category_id);
68-
if (!$category) {
69-
$category = new Category();
70-
$category->id = $request->category_id;
71-
$category->save();
72-
}
73-
74-
$movie->update(array_merge($data, ['category_id' => $category->id]));
67+
$movie->update($data);
7568

7669
return redirect()->route('admin.movies')->with('success', 'Film został zaktualizowany.');
7770
}
7871

7972
public function addMovie(AddMovieRequest $request)
8073
{
81-
$data = $request->all();
74+
$data = $request->validated();
8275

8376
if ($request->hasFile('img_path')) {
8477
$imagePath = $request->file('img_path')->store('public/img');
@@ -88,14 +81,7 @@ public function addMovie(AddMovieRequest $request)
8881
$data['img_path'] = 'default.jpg';
8982
}
9083

91-
$category = Category::find($request->category_id);
92-
if (!$category) {
93-
$category = new Category();
94-
$category->id = $request->category_id;
95-
$category->save();
96-
}
97-
98-
Movie::create(array_merge($data, ['category_id' => $category->id]));
84+
Movie::create($data);
9985

10086
return redirect()->route('admin.movies')->with('success', 'Film został dodany pomyślnie.');
10187
}
@@ -148,4 +134,16 @@ public function addCategory(AddCategoryRequest $request) {
148134

149135
return redirect()->route('admin.movies')->with('success', 'Kategoria została dodana pomyślnie.');
150136
}
137+
public function setSuperPromoPrice(Request $request, $id)
138+
{
139+
$movie = Movie::findOrFail($id);
140+
141+
$movie->old_price = $movie->price_day;
142+
$movie->price_day = $request->super_promo_price;
143+
$movie->super_promo_price = $request->super_promo_price;
144+
$movie->last_promo_update = now();
145+
$movie->save();
146+
147+
return redirect()->route('admin.movies')->with('success', 'Super promocja została ustawiona.');
148+
}
151149
}

app/Http/Controllers/AuthController.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
use Illuminate\Support\Facades\Auth;
66
use App\Models\User;
7+
use App\Models\ReferralCode;
8+
use App\Models\LoyaltyPoint;
79
use App\Http\Requests\AuthenticateRequest;
810
use App\Http\Requests\RegisterRequest;
911
use Illuminate\Http\Request;
12+
use Illuminate\Support\Facades\Session;
1013

1114
class AuthController extends Controller
1215
{
@@ -58,7 +61,27 @@ public function register(RegisterRequest $request)
5861
'address' => $validatedData['address'],
5962
'role_id' => 2 // Przypisuje nowym użytkownikom rolę klienta, ponieważ rola nr.1 to admin.
6063
]);
61-
64+
65+
ReferralCode::create([
66+
'user_id' => $user->id,
67+
'code' => substr(md5($user->id . microtime()), 0, 8)
68+
]);
69+
70+
LoyaltyPoint::create([
71+
'user_id' => $user->id,
72+
'points' => 0
73+
]);
74+
75+
if ($request->filled('referral_code')) {
76+
$referrer = ReferralCode::where('code', $request->input('referral_code'))->first();
77+
if ($referrer) {
78+
$referrer->user->loyaltyPoints()->increment('points', 20);
79+
80+
// Dodanie komunikatu o przyroście punktów do sesji
81+
Session::flash('points_message', 'Dzieki użyciu kodu osoba której jest kod polecający dostała 20 punktów!');
82+
}
83+
}
84+
6285
Auth::login($user);
6386
return redirect()->route('home');
6487
}

app/Http/Controllers/HomeController.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,32 @@ public function __construct(MovieService $movieService)
1616

1717
public function index()
1818
{
19+
$this->movieService->updatePrices(); // Wywołanie metody aktualizacji cen
20+
1921
$movies = Movie::where('available', 'dostępny')
2022
->inRandomOrder()
2123
->limit(6)
2224
->with('category')
2325
->get();
2426

2527
foreach ($movies as $movie) {
26-
$promoPrice = $this->movieService->calculateDynamicPrice($movie);
27-
$movie->old_price = $promoPrice;
28-
$movie->save();
28+
// $movie->current_price = $this->movieService->getPriceWithSuperPromotion($movie);
29+
$promoPrice = $this->movieService->calculatePromoPrice($movie->price_day);
30+
}
31+
32+
$topMovies = Movie::select('movies.id', 'movies.title', 'movies.img_path', 'movies.available', 'movies.description', 'movies.category_id', 'movies.director', 'movies.release_year', 'movies.duration', 'movies.rate', 'movies.video_path', 'movies.price_day', \DB::raw('count(loan_movie.id) as loans_count'))
33+
->join('loan_movie', 'loan_movie.movie_id', '=', 'movies.id')
34+
->groupBy('movies.id', 'movies.title', 'movies.img_path', 'movies.available', 'movies.description', 'movies.category_id', 'movies.director', 'movies.release_year', 'movies.duration', 'movies.rate', 'movies.video_path', 'movies.price_day')
35+
->orderByDesc('loans_count')
36+
->limit(10)
37+
->get();
38+
39+
foreach ($topMovies as $movie) {
40+
// $movie->current_price = $this->movieService->getPriceWithSuperPromotion($movie);
41+
$promoPrice = $this->movieService->calculatePromoPrice($movie->price_day);
2942
}
3043

31-
return view('home', compact('movies'));
44+
return view('home', compact('movies', 'topMovies', 'promoPrice'));
3245
}
3346

3447
public function regulamin()

app/Http/Controllers/MoviesController.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Auth;
99
use File;
1010
use Illuminate\Http\Request;
11+
use Redirect;
1112
use Response;
1213

1314
class MoviesController extends Controller
@@ -18,9 +19,11 @@ public function __construct(MovieService $movieService)
1819
{
1920
$this->movieService = $movieService;
2021
}
21-
22+
2223
public function index(Request $request)
23-
{
24+
{
25+
// $this->movieService->updatePrices(); // Wywołanie metody aktualizacji cen
26+
2427
$query = Movie::with('category');
2528

2629
if ($request->has('species') && $request->species != '') {
@@ -33,6 +36,10 @@ public function index(Request $request)
3336
$query->where('category_id', $request->category);
3437
}
3538

39+
if ($request->has('price_min') && $request->has('price_max')) {
40+
$query->whereBetween('price_day', [$request->price_min, $request->price_max]);
41+
}
42+
3643
switch ($request->sort_by) {
3744
case 'release1':
3845
$query->orderBy('release_year', 'asc');
@@ -57,25 +64,28 @@ public function index(Request $request)
5764
$movies = $query->paginate(6);
5865

5966
foreach ($movies as $movie) {
60-
$promoPrice = $this->movieService->calculateDynamicPrice($movie);
61-
$movie->old_price = $promoPrice;
62-
$movie->save();
67+
// $movie->current_price = $this->movieService->getPriceWithSuperPromotion($movie);
68+
$promoPrice = $this->movieService->calculatePromoPrice($movie->price_day);
6369
}
6470

65-
return view('movies.index', compact('movies'));
71+
return view('movies.index', compact('movies', 'promoPrice'));
6672
}
6773

6874
public function show($id)
6975
{
76+
// $this->movieService->updatePrices();
77+
7078
$movie = Movie::with(['category', 'opinions.user'])->where('id', $id)->firstOrFail();
71-
$promoPrice = $this->movieService->calculateDynamicPrice($movie);
79+
// $movie->current_price = $this->movieService->getPriceWithSuperPromotion($movie);
7280

73-
$movie->old_price = $promoPrice;
74-
$movie->save();
81+
$promoPrice = $this->movieService->calculatePromoPrice($movie->price_day);
7582

76-
return view('movies.show', compact('movie', 'promoPrice'));
83+
return view('movies.show', [
84+
'movie' => $movie,
85+
'promoPrice' => $promoPrice,
86+
]);
7787
}
78-
88+
7989
public function search(Request $request)
8090
{
8191
$query = $request->input('query');

app/Http/Controllers/UsersController.php

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use App\Http\Requests\ChangePasswordRequest;
1414
use App\Http\Requests\UpdateAvatarRequest;
1515
use App\Http\Requests\UpdateCartRequest;
16+
use DB;
1617
use Illuminate\Http\Request;
1718

1819
class UsersController extends Controller
@@ -21,8 +22,23 @@ public function showProfile()
2122
{
2223
$user_id = Auth::id();
2324
$loans = Loan::with('movies')->where('user_id', $user_id)->paginate(3);
24-
return view('user.profile', compact('loans'));
25-
}
25+
$referralCode = Auth::user()->referralCode->code ?? 'Brak';
26+
27+
$expensesData = Loan::where('user_id', $user_id)
28+
->where('start', '>=', now()->subMonth())
29+
->selectRaw('DATE(start) as date, SUM(price) as amount')
30+
->groupBy('date')
31+
->orderBy('date')
32+
->get()
33+
->map(function ($item) {
34+
return [
35+
'date' => $item->date,
36+
'amount' => $item->amount
37+
];
38+
});
39+
40+
return view('user.profile', compact('loans', 'referralCode', 'expensesData'));
41+
}
2642

2743
public function showMovie($movie_id)
2844
{
@@ -140,21 +156,21 @@ public function checkout(Request $request)
140156
if (empty($cart)) {
141157
return redirect()->route('cart.show')->with('error', 'Koszyk jest pusty.');
142158
}
143-
159+
144160
$user = Auth::user();
145161
$loyaltyPoints = $user->loyaltyPoints->points ?? 0;
146-
162+
147163
foreach ($cart as $id => $details) {
148164
if (empty($details['start']) || empty($details['end'])) {
149165
return back()->with('error', 'Niepoprawne daty wypożyczenia dla jednego z filmów.');
150166
}
151-
167+
152168
$startDate = new DateTime($details['start']);
153169
$endDate = new DateTime($details['end']);
154170
if ($endDate < $startDate) {
155171
return back()->with('error', 'Data zakończenia nie może być wcześniejsza niż data rozpoczęcia.');
156172
}
157-
173+
158174
$loan = new Loan();
159175
$loan->user_id = $user->id;
160176
$loan->start = $startDate->format('Y-m-d');
@@ -168,24 +184,27 @@ public function checkout(Request $request)
168184
$user->loyaltyPoints->points = $loyaltyPoints;
169185
$user->loyaltyPoints->save();
170186
}
171-
187+
172188
$loan->price = $moviePrice * $diff;
173189
$loan->status = StatusLoan::WYNAJEM;
174190
$loan->save();
175-
191+
176192
$loan->movies()->attach($id);
177-
193+
178194
if ($moviePrice > 0) {
179195
$pointsEarned = 10;
180196
$userLoyaltyPoints = $user->loyaltyPoints()->firstOrCreate(['user_id' => $user->id]);
181197
$userLoyaltyPoints->points += $pointsEarned;
182198
$userLoyaltyPoints->save();
199+
200+
// Dodanie komunikatu o przyroście punktów do sesji
201+
session()->flash('points_message', "Zdobyłeś $pointsEarned punktów lojalnościowych!");
183202
}
184203
}
185-
204+
186205
session()->forget('cart');
187206
return redirect()->route('user.profile')->with('success', 'Zakup udany. Produkty zostały dodane do Twojej historii wypożyczeń.');
188-
}
207+
}
189208

190209
public function updateCart(UpdateCartRequest $request, $movie_id)
191210
{
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Support\Facades\Auth;
7+
8+
class EnsureUserHasLoyaltyPoints
9+
{
10+
public function handle($request, Closure $next)
11+
{
12+
if (Auth::check() && !Auth::user()->loyaltyPoints) {
13+
Auth::user()->loyaltyPoints()->create(['points' => 0]);
14+
}
15+
16+
return $next($request);
17+
}
18+
}

0 commit comments

Comments
 (0)