Skip to content

Commit 6713104

Browse files
committed
insertion of photos to database
1 parent 33b8074 commit 6713104

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

app/functions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@ function InsertINTODB($i, $w, $h,$a, $d){
1414
$p->save();
1515
}
1616

17+
function checkIFPhotoExists($photoid){
18+
$p = Entries::where('photoid', $photoid)->first();
19+
if($p !== null)
20+
return true;
21+
else
22+
return false;
23+
}
24+
1725
?>

resources/views/welcome.blade.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,27 @@
4646
4747
require(app_path(). '/functions.php');
4848
49-
$cache_key = 'photos-cache-key';
49+
$cache_key = 'photos-cache-key2';
5050
$faker = \Faker\Factory::create();
5151
5252
if(\Cache::has($cache_key))
5353
$photos = cache($cache_key);
5454
else{
5555
$photos = json_decode(file_get_contents("https://picsum.photos/v2/list"));
56-
cache([$cache_key => $photos], 60);
56+
$photos_with_desc = array_map(function($item) use($faker){
57+
$item->Description = $faker->realText;
58+
$item->Votes = 0;
59+
return $item;
60+
}, $photos);
61+
62+
foreach($photos_with_desc as $photo){
63+
$exists = checkIFPhotoExists($photo->id);
64+
if(!$exists){
65+
InsertINTODB($photo->id, $photo->width, $photo->height,$photo->author,$photo->Description);
66+
}
67+
}
68+
69+
cache([$cache_key => $photos_with_desc], 60);
5770
}
5871
5972
@endphp
@@ -65,17 +78,17 @@
6578
<div class="card mb-4 shadow-sm">
6679
<img src="https://picsum.photos/id/{{$photo->id}}/348/225"/>
6780
<div class="card-body">
68-
<p class="card-text">{{ $faker->realText }}</p>
81+
<p class="card-text">{{ $photo->Description }}</p>
6982
<div class="d-flex justify-content-between align-items-center">
7083
<div class="btn-group">
7184
<button type="button" class="btn btn-sm btn-outline-secondary">
72-
<span class="oi oi-arrow-thick-top"></span>
85+
<a href="/track.php?vote_method=up&photoID={{$photo->id}}"><span class="oi oi-arrow-thick-top"></span></a>
7386
</button>
7487
<button type="button" class="btn btn-sm btn-outline-secondary">
75-
<span class="oi oi-arrow-thick-bottom"></span>
88+
<a href="/track.php?vote_method=down&photoID={{$photo->id}}"><span class="oi oi-arrow-thick-bottom"></span></a>
7689
</button>
7790
</div>
78-
<small class="text-muted">{{ mt_rand(100,10000)}} votes</small>
91+
<small class="text-muted">{{ $photo->Votes}} votes</small>
7992
</div>
8093
</div>
8194
</div>

storage/db.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ CREATE TABLE IF NOT EXISTS `Pictures` (
66
`Votes` int NOT NULL default 0,
77
`Author` varchar(100) NOT NULL default 'None',
88
`Description` varchar(255) default 'None',
9+
`updated_at` DATETIME ON UPDATE CURRENT_TIMESTAMP,
10+
`created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
911
PRIMARY KEY (`picID`)
1012
);

0 commit comments

Comments
 (0)