Skip to content

Commit

Permalink
some validation
Browse files Browse the repository at this point in the history
  • Loading branch information
MariiaKovalenkoo committed Apr 7, 2024
1 parent ed78801 commit b13d198
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 87 deletions.
16 changes: 10 additions & 6 deletions app/controller/artists/createArtist.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@
$bio = $props['bio'];

$inputService->checkRequiredFields([$name, $bio]);
$inputService->validateArtistBio($bio);

$songs = [$_POST['song1'] ?? null, $_POST['song2'] ?? null, $_POST['song3'] ?? null];
$albums = [$_POST['album1'] ?? null, $_POST['album2'] ?? null, $_POST['album3'] ?? null];
$songs = [$props['song1'], $props['song2'], $props['song3']];
$albums = [$props['album1'], $props['album2'], $props['album3']];

$headerImgPath = $inputService->checkAndUploadImage('header_img', 'jazz/artists');
$artistImg1Path = $inputService->checkAndUploadImage('artist_img1', 'jazz/artists');
$artistImg2Path = $inputService->checkAndUploadImage('artist_img2', 'jazz/artists');
$performanceImgPath = $inputService->checkAndUploadImage('performance_img', 'jazz/performances');
$inputService->validateAlbums($albums);
$inputService->validateSongs($songs);

$headerImgPath = $inputService->validateAndUploadImage('header_img', 'jazz/artists');
$artistImg1Path = $inputService->validateAndUploadImage('artist_img1', 'jazz/artists');
$artistImg2Path = $inputService->validateAndUploadImage('artist_img2', 'jazz/artists');
$performanceImgPath = $inputService->validateAndUploadImage('performance_img', 'jazz/performances');

$jazzService->createArtist($name, $bio, $headerImgPath, $artistImg1Path, $artistImg2Path, $performanceImgPath, $songs, $albums);

Expand Down
18 changes: 11 additions & 7 deletions app/controller/artists/editArtist.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
require_once __DIR__ . '/../../service/ValidateInputService.php';

$jazzService = new JazzService();

Route::serve('/artists/editArtist', function (array $props) use ($jazzService){
if(!isset($props['id'])) {
Route::redirect('/artists/manageArtists');
Expand All @@ -33,14 +33,18 @@
$bio = $props['bio'];

$validateInputService->checkRequiredFields([$name, $bio]);
$validateInputService->validateArtistBio($bio);

$songs = [$props['song1'], $props['song2'], $props['song3']];
$albums = [$props['album1'], $props['album2'], $props['album3']];

$songs = [$_POST['song1'] ?? null, $_POST['song2'] ?? null, $_POST['song3'] ?? null];
$albums = [$_POST['album1'] ?? null, $_POST['album2'] ?? null, $_POST['album3'] ?? null];
$validateInputService->validateAlbums($albums);
$validateInputService->validateSongs($songs);

$headerImgPath = $validateInputService->handleImageUpload('header_img', 'jazz/artists');
$artistImg1Path = $validateInputService->handleImageUpload('artist_img1', 'jazz/artists');
$artistImg2Path = $validateInputService->handleImageUpload('artist_img2', 'jazz/artists');
$performanceImgPath = $validateInputService->handleImageUpload('performance_img', 'jazz/performances');
$headerImgPath = $validateInputService->updateImage('header_img', 'jazz/artists');
$artistImg1Path = $validateInputService->updateImage('artist_img1', 'jazz/artists');
$artistImg2Path = $validateInputService->updateImage('artist_img2', 'jazz/artists');
$performanceImgPath = $validateInputService->updateImage('performance_img', 'jazz/performances');

$jazzService->updateArtist($id, $name, $bio, $songs, $albums, $headerImgPath, $artistImg1Path, $artistImg2Path, $performanceImgPath);

Expand Down
2 changes: 1 addition & 1 deletion app/controller/jazzdays/createDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
$note = $props['note'];

$validateInputService->checkRequiredFields([$date, $venueId]);
$imgPath = $validateInputService->checkAndUploadImage('image', 'jazz');
$imgPath = $validateInputService->validateAndUploadImage('image', 'jazz');

$jazzService->createJazzDay($date, $venueId, $note, $imgPath);

Expand Down
2 changes: 1 addition & 1 deletion app/controller/jazzdays/editDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
$note = $props['note'];

$validateInputService->checkRequiredFields([$date, $venueId]);
$imgPath = $validateInputService->handleImageUpload('image', 'jazz');
$imgPath = $validateInputService->updateImage('image', 'jazz');

$jazzService->updateJazzDay($dayId, $date, $venueId, $note, $imgPath);

Expand Down
4 changes: 3 additions & 1 deletion app/pages/admin/jazz/manage/artists.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
<link href="/css/jazzStyles.css" rel="stylesheet">
</head>

<body class="bg-gray-100 flex justify-center items-center h-screen font-montserrat">
<body class="bg-gray-100 font-montserrat">
@include('main.navbar')
<div class="flex justify-center items-center h-screen">
<div class="flex w-full justify-between">

@include('admin.panel')
Expand Down
6 changes: 5 additions & 1 deletion app/pages/admin/jazz/manage/days.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
<link href="/css/jazzStyles.css" rel="stylesheet">
</head>

<body class="bg-gray-100 flex justify-center items-center h-screen font-montserrat">

<body class="bg-gray-100 font-montserrat">
@include('main.navbar')
<div class="flex justify-center items-center h-screen">

<div class="flex w-full justify-between">

@include('admin.panel')
Expand Down
6 changes: 5 additions & 1 deletion app/pages/admin/jazz/manage/passes.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
<link href="/css/jazzStyles.css" rel="stylesheet">
</head>

<body class="bg-gray-100 flex justify-center items-center h-screen font-montserrat">

<body class="bg-gray-100 font-montserrat">
@include('main.navbar')
<div class="flex justify-center items-center h-screen">

<div class="flex w-full justify-between">

@include('admin.panel')
Expand Down
6 changes: 5 additions & 1 deletion app/pages/admin/jazz/manage/performances.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
<link href="/css/jazzStyles.css" rel="stylesheet">
</head>

<body class="bg-gray-100 flex justify-center items-center h-screen font-montserrat">

<body class="bg-gray-100 font-montserrat">
@include('main.navbar')
<div class="flex justify-center items-center h-screen">

<div class="flex w-full justify-between">
<div class="flex flex-wrap">

Expand Down
6 changes: 5 additions & 1 deletion app/pages/admin/jazz/manage/venues.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
<link href="/css/jazzStyles.css" rel="stylesheet">
</head>

<body class="bg-gray-100 flex justify-center items-center h-screen font-montserrat">

<body class="bg-gray-100 font-montserrat">
@include('main.navbar')
<div class="flex justify-center items-center h-screen">

<div class="flex w-full justify-between">

@include('admin.panel')
Expand Down
8 changes: 4 additions & 4 deletions app/pages/jazz/artist/main.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
</div>

<div class="p-4 mx-2 my-4 overflow-hidden text-center">
<div class="bg-pink-500 p-4 inline-block max-h-[60vh] overflow-hidden rounded-lg">
<img src="{{ $artist->ArtistImg2 }}" alt="{{ $artist->Name }}" class="max-h-[50vh] w-auto rounded-lg shadow-lg"/>
<div class="bg-pink-500 p-4 inline-block max-h-[70vh] overflow-hidden rounded-lg">
<img src="{{ $artist->ArtistImg2 }}" alt="{{ $artist->Name }}" class="max-h-[55vh] w-auto rounded-lg shadow-lg"/>
</div>
</div>

<div class="p-4 mx-2 my-4 overflow-hidden text-center">
<div class="bg-pink-500 p-4 inline-block max-h-[60vh] overflow-hidden rounded-lg">
<img src="{{ $artist->ArtistImg1 }}" alt="{{ $artist->Name }}" class="max-h-[50vh] w-auto rounded-lg shadow-lg "/>
<div class="bg-pink-500 p-4 inline-block max-h-[70vh] overflow-hidden rounded-lg">
<img src="{{ $artist->ArtistImg1 }}" alt="{{ $artist->Name }}" class="max-h-[55vh] w-auto rounded-lg shadow-lg "/>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/pages/jazz/artist/performances.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@if ($performance->Price != '0.00')
<div class="mt-4 flex justify-center w-full">
@if ($performance->AvailableTickets > 0)
<a href="/agenda/purchase?name={{$artist->Name}}" class="p-2 sm:p-3 rounded-md font-semibold uppercase cursor-pointer text-sm sm:text-base w-48 bg-pink-500 text-black">
<a href="/agenda/purchase?name={{$artist->Name}}" class="p-2 sm:p-3 rounded-md font-semibold uppercase cursor-pointer text-sm sm:text-base w-48 bg-pink-500 text-white">
Buy Tickets!</a>
@else
<button class="p-2 sm:p-3 rounded-md font-semibold uppercase cursor-pointer text-sm sm:text-base w-48 bg-red-500 text-white" disabled>
Expand Down
2 changes: 1 addition & 1 deletion app/pages/jazz/artist/songs.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="p-4">
<div class="text-center mb-4 md:mb-0">
<div class="p-8 bg-pink-200 text-center overflow-hidden cursor-pointer block">
<div class="p-8 bg-pink-200 rounded-lg text-center overflow-hidden cursor-pointer block">
<iframe style="border-radius: 12px"
src="https://open.spotify.com/embed/track/{{ $song }}?utm_source=generator"
width="100%" height="352" allowfullscreen=""
Expand Down
1 change: 1 addition & 0 deletions app/public/css/jazzStyles.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@
}



Binary file added app/public/img/jazz/artists/661294a308cd1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/public/img/jazz/artists/6612969b16bd5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 5 additions & 30 deletions app/repository/JazzRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getArtistById(int $id): Artist

public function getAllArtists(): array
{
$query = $this->connection->prepare('SELECT * FROM Artist');
$query = $this->connection->prepare('SELECT * FROM Artist ORDER BY Name ASC');
$query->execute();

$artistData = $query->fetchAll(\PDO::FETCH_ASSOC);
Expand Down Expand Up @@ -67,13 +67,6 @@ private function createArtistFromData(array $artistData): Artist
}

// create edit delete artists
public function updateArtist1(int $artistId, string $name, string $bio, array $songs, array $albums, string $headerImg, string $artistImg1, string $artistImg2, string $performanceImg): bool
{
$query = $this->connection->prepare('UPDATE Artist SET Name = ?, Bio = ?, HeaderImg = ?, ArtistImg1 = ?, ArtistImg2 = ?, PerformanceImg = ?, Song1 = ?, Song2 = ?, Song3 = ?, Album1 = ?, Album2 = ?, Album3 = ? WHERE ArtistID = ?');

return $query->execute([$name, $bio, $headerImg, $artistImg1, $artistImg2, $performanceImg, $songs[0], $songs[1], $songs[2], $albums[0], $albums[1], $albums[2], $artistId]);
}

public function updateArtist(int $artistId, string $name, string $bio, array $songs, array $albums, ?string $headerImg = null, ?string $artistImg1 = null, ?string $artistImg2 = null, ?string $performanceImg = null): bool
{
$sql = 'UPDATE Artist SET Name = ?, Bio = ?';
Expand Down Expand Up @@ -223,23 +216,6 @@ public function getAllJazzDays(): array
return $jazzDays;
}

public function getAllJazzDaysByVenueId($id): array
{
$query = $this->connection->prepare('SELECT d.*, v.VenueID, v.Name as VenueName, v.Address, v.ContactDetails FROM JazzDay d
JOIN Venue v ON d.VenueID = v.VenueID
WHERE d.VenueID = ?');
$query->execute([$id]);

$jazzDaysData = $query->fetchAll(\PDO::FETCH_ASSOC);
$jazzDays = [];

foreach ($jazzDaysData as $jazzDayData) {
$jazzDays[] = $this->createJazzDayFromData($jazzDayData);
}

return $jazzDays;
}

private function createJazzDayFromData(array $jazzDayData): JazzDay
{
$venue = $this->createVenueFromData($jazzDayData);
Expand Down Expand Up @@ -300,11 +276,10 @@ public function deleteJazzDay(int $id): bool
// retrieve performances
public function getPerformancesByArtist(Artist $artist): array
{

$query = $this->connection->prepare('SELECT p.*, j.*, v.VenueID, v.Name as VenueName, v.Address, v.ContactDetails FROM Performance p
LEFT JOIN JazzDay j ON p.DayID = j.DayID
LEFT JOIN Venue v ON j.VenueID = v.VenueID
WHERE p.ArtistID = ?');
WHERE p.ArtistID = ? ORDER BY p.StartDateTime');
$query->execute([$artist->ArtistID]);

$performanceData = $query->fetchAll(\PDO::FETCH_ASSOC);
Expand All @@ -319,7 +294,7 @@ public function getPerformancesByArtist(Artist $artist): array

public function getPerformancesByJazzDay(JazzDay $day): array
{
$query = $this->connection->prepare('SELECT p.*, a.* FROM Performance p LEFT JOIN Artist a ON p.ArtistID = a.ArtistID WHERE p.DayID = ?');
$query = $this->connection->prepare('SELECT p.*, a.* FROM Performance p LEFT JOIN Artist a ON p.ArtistID = a.ArtistID WHERE p.DayID = ? ORDER BY p.StartDateTime');
$query->execute([$day->DayID]);
$performanceData = $query->fetchAll(\PDO::FETCH_ASSOC);

Expand All @@ -336,7 +311,7 @@ public function getAllPerformances(): array
$query = $this->connection->prepare('SELECT p.*, a.*, j.*, v.VenueID, v.Name as VenueName, v.Address, v.ContactDetails FROM Performance p
LEFT JOIN Artist a ON p.ArtistID = a.ArtistID
LEFT JOIN JazzDay j ON p.DayID = j.DayID
LEFT JOIN Venue v ON j.VenueID = v.VenueID');
LEFT JOIN Venue v ON j.VenueID = v.VenueID ORDER BY p.StartDateTime');
$query->execute();

$performanceData = $query->fetchAll(\PDO::FETCH_ASSOC);
Expand Down Expand Up @@ -434,7 +409,7 @@ public function getJazzPassById(int $id): JazzPass

public function getAllJazzPasses(): array
{
$query = $this->connection->prepare('SELECT * FROM JazzPass');
$query = $this->connection->prepare('SELECT * FROM JazzPass order by StartDateTime ASC');
$query->execute();

$passesData = $query->fetchAll(\PDO::FETCH_ASSOC);
Expand Down
30 changes: 3 additions & 27 deletions app/service/JazzService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public function getAllArtists(): array
return $this->repository->getAllArtists();
}

public function updateArtist(int $artistId, string $name, string $bio, ?array $songs, ?array $albums, ?string $headerImg, ?string $artistImg1, ?string $artistImg2, ?string $performanceImg): void
public function updateArtist(int $artistId, string $name, string $bio, array $songs, array $albums, ?string $headerImg, ?string $artistImg1, ?string $artistImg2, ?string $performanceImg): void
{
$artist = $this->repository->getArtistById($artistId);

$this->setDefaultSongsAndAlbumsIfEmpty($songs, $albums);
$this->repository->updateArtist($artistId, $name, $bio, $songs, $albums, $headerImg, $artistImg1, $artistImg2, $performanceImg);

// delete old images if new were uploaded and if old images aren't placeholders
// if the user updated image , delete the old one
// if the image is a placeholder don't delete it
if ($headerImg !== null && $artist->HeaderImg !== '/img/jazz/artists/artistPlaceholder.jpg') {
$this->imageService->deleteImage($artist->HeaderImg);
}
Expand All @@ -61,8 +61,6 @@ public function updateArtist(int $artistId, string $name, string $bio, ?array $s

public function createArtist(string $name, string $bio, string $headerImg, string $artistImg1, string $artistImg2, string $performanceImg, array $songs, array $albums): bool
{
$this->setDefaultSongsAndAlbumsIfEmpty($songs, $albums);

return $this->repository->createArtist($name, $bio, $headerImg, $artistImg1, $artistImg2, $performanceImg, $songs, $albums);
}

Expand All @@ -88,28 +86,6 @@ public function deleteArtist(int $id): void
}
}

private function setDefaultSongsAndAlbumsIfEmpty(array &$songs, array &$albums): void
{
if ($albums[0] == null) {
$albums[0] = '7oBC2PuPSvXkLEZdoCxsv5';
}
if ($albums[1] == null) {
$albums[1] = '18g4jSwIbYcbJI5U7PIzMz';
}
if ($albums[2] == null) {
$albums[2] = '0B7DKUR00yRXncWrlQwIR6';
}
if ($songs[0] == null) {
$songs[0] = '6XQHlsNu6so4PdglFkJQRJ';
}
if ($songs[1] == null) {
$songs[1] = '2VvDKx7lzdarObpQFn1iAh';
}
if ($songs[2] == null) {
$songs[2] = '1otrWVcbCxemNnn7eiKW1P';
}
}

// venues
public function getVenueById(int $id): Venue
{
Expand Down
44 changes: 40 additions & 4 deletions app/service/ValidateInputService.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function validateDate($startDate, $endDate): void
}

}

public function validateEmptyNumbers($numbers): void
{
foreach($numbers as $number) {
Expand All @@ -74,7 +74,7 @@ public function validateTicketFields($availableTickets, $totalTickets): void
}
}

public function checkAndUploadImage($image, $dir) {
public function validateAndUploadImage($image, $dir) {
if (isset($_FILES[$image]) && $_FILES[$image]['error'] === UPLOAD_ERR_OK) {
try {
return $this->imageService->uploadImage($_FILES[$image], $dir);
Expand All @@ -83,13 +83,14 @@ public function checkAndUploadImage($image, $dir) {
exit();
}
} else {
$error = 'Please upload an image.';
$imgName = str_replace(['_', ' '], '', $image);
$error = "Please upload an image {$imgName}.";
echo "<div class='error bg-red-100 border-l-4 border-red-500 text-red-700 p-4 m-4' id='error' role='alert'>$error</div>";
exit();
}
}

function handleImageUpload($image, $dir)
function updateImage($image, $dir)
{
if (isset($_FILES[$image]) && $_FILES[$image]['error'] === UPLOAD_ERR_OK) {
try {
Expand All @@ -102,4 +103,39 @@ function handleImageUpload($image, $dir)
return null;
}
}

public function validateArtistBio($bio): void
{
if (strlen($bio) < 800) {
$error = 'Bio should not be less then 800 characters (around 150 words).';
echo "<div class='error bg-red-100 border-l-4 border-red-500 text-red-700 p-4 m-4' id='error' role='alert'>$error</div>";
exit();
}
}

public function validateAlbums(array &$albums): void
{
if (strlen($albums[0]) < 22) {
$albums[0] = '7oBC2PuPSvXkLEZdoCxsv5';
}
if (strlen($albums[1]) < 22) {
$albums[1] = '18g4jSwIbYcbJI5U7PIzMz';
}
if (strlen($albums[2]) < 22) {
$albums[2] = '0B7DKUR00yRXncWrlQwIR6';
}
}

public function validateSongs(array &$songs): void
{
if (strlen($songs[0]) < 22) {
$songs[0] = '6XQHlsNu6so4PdglFkJQRJ';
}
if (strlen($songs[1]) < 22) {
$songs[1] = '2VvDKx7lzdarObpQFn1iAh';
}
if (strlen($songs[2]) < 22) {
$songs[2] = '1otrWVcbCxemNnn7eiKW1P';
}
}
}

0 comments on commit b13d198

Please sign in to comment.