Skip to content

Commit 96771c8

Browse files
committed
fixed insertion logic
1 parent 6713104 commit 96771c8

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

resources/views/welcome.blade.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,27 @@
5252
if(\Cache::has($cache_key))
5353
$photos = cache($cache_key);
5454
else{
55-
$photos = json_decode(file_get_contents("https://picsum.photos/v2/list"));
56-
$photos_with_desc = array_map(function($item) use($faker){
57-
$item->Description = $faker->realText;
58-
$item->Votes = 0;
59-
return $item;
60-
}, $photos);
6155
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-
}
56+
$photos = \App\Entries::all();
57+
if(count($photos)<=0){
58+
$photos_from_api = json_decode(file_get_contents("https://picsum.photos/v2/list"));
59+
60+
$photos_with_desc = array_map(function($item) use($faker){
61+
$item->Description = $faker->realText;
62+
$item->Votes = 0;
63+
$item->photoid = $item->id;
64+
return $item;
65+
}, $photos_from_api);
6866
69-
cache([$cache_key => $photos_with_desc], 60);
67+
foreach($photos_with_desc as $photo){
68+
$exists = checkIFPhotoExists($photo->id);
69+
if(!$exists){
70+
InsertINTODB($photo->id, $photo->width, $photo->height,$photo->author,$photo->Description);
71+
}
72+
}
73+
$photos = $photos_with_desc;
74+
cache([$cache_key => $photos], 60);
75+
}
7076
}
7177
7278
@endphp
@@ -76,16 +82,16 @@
7682
@foreach($photos as $photo)
7783
<div class="col-md-4">
7884
<div class="card mb-4 shadow-sm">
79-
<img src="https://picsum.photos/id/{{$photo->id}}/348/225"/>
85+
<img src="https://picsum.photos/id/{{$photo->photoid}}/348/225"/>
8086
<div class="card-body">
8187
<p class="card-text">{{ $photo->Description }}</p>
8288
<div class="d-flex justify-content-between align-items-center">
8389
<div class="btn-group">
8490
<button type="button" class="btn btn-sm btn-outline-secondary">
85-
<a href="/track.php?vote_method=up&photoID={{$photo->id}}"><span class="oi oi-arrow-thick-top"></span></a>
91+
<a href="/track.php?vote_method=up&photoID={{$photo->photoid}}"><span class="oi oi-arrow-thick-top"></span></a>
8692
</button>
8793
<button type="button" class="btn btn-sm btn-outline-secondary">
88-
<a href="/track.php?vote_method=down&photoID={{$photo->id}}"><span class="oi oi-arrow-thick-bottom"></span></a>
94+
<a href="/track.php?vote_method=down&photoID={{$photo->photoid}}"><span class="oi oi-arrow-thick-bottom"></span></a>
8995
</button>
9096
</div>
9197
<small class="text-muted">{{ $photo->Votes}} votes</small>

0 commit comments

Comments
 (0)