Skip to content

Commit

Permalink
corrections for a high rating
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidolko committed May 23, 2024
1 parent 75032b7 commit 7f291d1
Show file tree
Hide file tree
Showing 39 changed files with 14,424 additions and 361 deletions.
Binary file added RentalVOD_archive.zip
Binary file not shown.
30 changes: 30 additions & 0 deletions app/Console/Commands/UpdateMoviePrices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdateMoviePrices extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:update-movie-prices';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle()
{
//
}
}
44 changes: 21 additions & 23 deletions app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Http\Requests\AddMovieRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Http\Requests\AddCategoryRequest;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Movie;
use App\Models\Loan;
Expand Down Expand Up @@ -36,11 +37,10 @@ public function movies()
$movies = Movie::paginate(10);


foreach ($movies as $movie) {
$dynamicPrice = $this->movieService->calculateDynamicPrice($movie);
$movie->price_day = $dynamicPrice;
$movie->save();
}
// foreach ($movies as $movie) {
// $movie->price_day;
// $movie->save();
// }

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

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

$category = Category::find($request->category_id);
if (!$category) {
$category = new Category();
$category->id = $request->category_id;
$category->save();
}

$movie->update(array_merge($data, ['category_id' => $category->id]));
$movie->update($data);

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

public function addMovie(AddMovieRequest $request)
{
$data = $request->all();
$data = $request->validated();

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

$category = Category::find($request->category_id);
if (!$category) {
$category = new Category();
$category->id = $request->category_id;
$category->save();
}

Movie::create(array_merge($data, ['category_id' => $category->id]));
Movie::create($data);

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

return redirect()->route('admin.movies')->with('success', 'Kategoria została dodana pomyślnie.');
}
public function setSuperPromoPrice(Request $request, $id)
{
$movie = Movie::findOrFail($id);

$movie->old_price = $movie->price_day;
$movie->price_day = $request->super_promo_price;
$movie->super_promo_price = $request->super_promo_price;
$movie->last_promo_update = now();
$movie->save();

return redirect()->route('admin.movies')->with('success', 'Super promocja została ustawiona.');
}
}
25 changes: 24 additions & 1 deletion app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\ReferralCode;
use App\Models\LoyaltyPoint;
use App\Http\Requests\AuthenticateRequest;
use App\Http\Requests\RegisterRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class AuthController extends Controller
{
Expand Down Expand Up @@ -58,7 +61,27 @@ public function register(RegisterRequest $request)
'address' => $validatedData['address'],
'role_id' => 2 // Przypisuje nowym użytkownikom rolę klienta, ponieważ rola nr.1 to admin.
]);


ReferralCode::create([
'user_id' => $user->id,
'code' => substr(md5($user->id . microtime()), 0, 8)
]);

LoyaltyPoint::create([
'user_id' => $user->id,
'points' => 0
]);

if ($request->filled('referral_code')) {
$referrer = ReferralCode::where('code', $request->input('referral_code'))->first();
if ($referrer) {
$referrer->user->loyaltyPoints()->increment('points', 20);

// Dodanie komunikatu o przyroście punktów do sesji
Session::flash('points_message', 'Dzieki użyciu kodu osoba której jest kod polecający dostała 20 punktów!');
}
}

Auth::login($user);
return redirect()->route('home');
}
Expand Down
21 changes: 17 additions & 4 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,32 @@ public function __construct(MovieService $movieService)

public function index()
{
$this->movieService->updatePrices(); // Wywołanie metody aktualizacji cen

$movies = Movie::where('available', 'dostępny')
->inRandomOrder()
->limit(6)
->with('category')
->get();

foreach ($movies as $movie) {
$promoPrice = $this->movieService->calculateDynamicPrice($movie);
$movie->old_price = $promoPrice;
$movie->save();
// $movie->current_price = $this->movieService->getPriceWithSuperPromotion($movie);
$promoPrice = $this->movieService->calculatePromoPrice($movie->price_day);
}

$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'))
->join('loan_movie', 'loan_movie.movie_id', '=', 'movies.id')
->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')
->orderByDesc('loans_count')
->limit(10)
->get();

foreach ($topMovies as $movie) {
// $movie->current_price = $this->movieService->getPriceWithSuperPromotion($movie);
$promoPrice = $this->movieService->calculatePromoPrice($movie->price_day);
}

return view('home', compact('movies'));
return view('home', compact('movies', 'topMovies', 'promoPrice'));
}

public function regulamin()
Expand Down
32 changes: 21 additions & 11 deletions app/Http/Controllers/MoviesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Auth;
use File;
use Illuminate\Http\Request;
use Redirect;
use Response;

class MoviesController extends Controller
Expand All @@ -18,9 +19,11 @@ public function __construct(MovieService $movieService)
{
$this->movieService = $movieService;
}

public function index(Request $request)
{
{
// $this->movieService->updatePrices(); // Wywołanie metody aktualizacji cen

$query = Movie::with('category');

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

if ($request->has('price_min') && $request->has('price_max')) {
$query->whereBetween('price_day', [$request->price_min, $request->price_max]);
}

switch ($request->sort_by) {
case 'release1':
$query->orderBy('release_year', 'asc');
Expand All @@ -57,25 +64,28 @@ public function index(Request $request)
$movies = $query->paginate(6);

foreach ($movies as $movie) {
$promoPrice = $this->movieService->calculateDynamicPrice($movie);
$movie->old_price = $promoPrice;
$movie->save();
// $movie->current_price = $this->movieService->getPriceWithSuperPromotion($movie);
$promoPrice = $this->movieService->calculatePromoPrice($movie->price_day);
}

return view('movies.index', compact('movies'));
return view('movies.index', compact('movies', 'promoPrice'));
}

public function show($id)
{
// $this->movieService->updatePrices();

$movie = Movie::with(['category', 'opinions.user'])->where('id', $id)->firstOrFail();
$promoPrice = $this->movieService->calculateDynamicPrice($movie);
// $movie->current_price = $this->movieService->getPriceWithSuperPromotion($movie);

$movie->old_price = $promoPrice;
$movie->save();
$promoPrice = $this->movieService->calculatePromoPrice($movie->price_day);

return view('movies.show', compact('movie', 'promoPrice'));
return view('movies.show', [
'movie' => $movie,
'promoPrice' => $promoPrice,
]);
}

public function search(Request $request)
{
$query = $request->input('query');
Expand Down
41 changes: 30 additions & 11 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Http\Requests\ChangePasswordRequest;
use App\Http\Requests\UpdateAvatarRequest;
use App\Http\Requests\UpdateCartRequest;
use DB;
use Illuminate\Http\Request;

class UsersController extends Controller
Expand All @@ -21,8 +22,23 @@ public function showProfile()
{
$user_id = Auth::id();
$loans = Loan::with('movies')->where('user_id', $user_id)->paginate(3);
return view('user.profile', compact('loans'));
}
$referralCode = Auth::user()->referralCode->code ?? 'Brak';

$expensesData = Loan::where('user_id', $user_id)
->where('start', '>=', now()->subMonth())
->selectRaw('DATE(start) as date, SUM(price) as amount')
->groupBy('date')
->orderBy('date')
->get()
->map(function ($item) {
return [
'date' => $item->date,
'amount' => $item->amount
];
});

return view('user.profile', compact('loans', 'referralCode', 'expensesData'));
}

public function showMovie($movie_id)
{
Expand Down Expand Up @@ -140,21 +156,21 @@ public function checkout(Request $request)
if (empty($cart)) {
return redirect()->route('cart.show')->with('error', 'Koszyk jest pusty.');
}

$user = Auth::user();
$loyaltyPoints = $user->loyaltyPoints->points ?? 0;

foreach ($cart as $id => $details) {
if (empty($details['start']) || empty($details['end'])) {
return back()->with('error', 'Niepoprawne daty wypożyczenia dla jednego z filmów.');
}

$startDate = new DateTime($details['start']);
$endDate = new DateTime($details['end']);
if ($endDate < $startDate) {
return back()->with('error', 'Data zakończenia nie może być wcześniejsza niż data rozpoczęcia.');
}

$loan = new Loan();
$loan->user_id = $user->id;
$loan->start = $startDate->format('Y-m-d');
Expand All @@ -168,24 +184,27 @@ public function checkout(Request $request)
$user->loyaltyPoints->points = $loyaltyPoints;
$user->loyaltyPoints->save();
}

$loan->price = $moviePrice * $diff;
$loan->status = StatusLoan::WYNAJEM;
$loan->save();

$loan->movies()->attach($id);

if ($moviePrice > 0) {
$pointsEarned = 10;
$userLoyaltyPoints = $user->loyaltyPoints()->firstOrCreate(['user_id' => $user->id]);
$userLoyaltyPoints->points += $pointsEarned;
$userLoyaltyPoints->save();

// Dodanie komunikatu o przyroście punktów do sesji
session()->flash('points_message', "Zdobyłeś $pointsEarned punktów lojalnościowych!");
}
}

session()->forget('cart');
return redirect()->route('user.profile')->with('success', 'Zakup udany. Produkty zostały dodane do Twojej historii wypożyczeń.');
}
}

public function updateCart(UpdateCartRequest $request, $movie_id)
{
Expand Down
18 changes: 18 additions & 0 deletions app/Http/Middleware/EnsureUserHasLoyaltyPoints.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class EnsureUserHasLoyaltyPoints
{
public function handle($request, Closure $next)
{
if (Auth::check() && !Auth::user()->loyaltyPoints) {
Auth::user()->loyaltyPoints()->create(['points' => 0]);
}

return $next($request);
}
}
Loading

0 comments on commit 7f291d1

Please sign in to comment.