summaryrefslogtreecommitdiff
path: root/app/Libraries/Helper.php
diff options
context:
space:
mode:
Diffstat (limited to 'app/Libraries/Helper.php')
-rw-r--r--app/Libraries/Helper.php58
1 files changed, 57 insertions, 1 deletions
diff --git a/app/Libraries/Helper.php b/app/Libraries/Helper.php
index 43b6ff3..88650b4 100644
--- a/app/Libraries/Helper.php
+++ b/app/Libraries/Helper.php
@@ -1,5 +1,6 @@
<?php
namespace App\Libraries;
+use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
@@ -49,7 +50,17 @@ class Helper {
public function setCalendar($username) {
$user = MALUser::where('username', $username)->get()->first();
- echo "<pre>";
+
+ if ( is_null($user) ) {
+ echo "User (" . $username . ") does not exists. Skipping\n";
+ return;
+ }
+
+ if ( is_null($user->IsWatching) ) {
+ echo "User (" . $username . ") does not watch any anime. Skipping\n";
+ return;
+ }
+
foreach( $user->IsWatching as $anime ) {
$stats = $anime->getStats()->get()->first();
$airing = $anime->getAiring()->get()->first();
@@ -84,5 +95,50 @@ class Helper {
}
}
+ public function setIsWatching($username) {
+ $user = MALUser::where('username', $username)->get()->first();
+
+ if ( is_null($user) ) {
+ echo "User (" . $username . ") does not exists. Skipping\n";
+ return;
+ }
+
+ $actually_watching = array();
+
+ foreach( $user->getIsWatching() as $anime_details ) {
+ $check = DB::table('is_watching')
+ ->where('mal_id', $anime_details["mal_id"])
+ ->where('user_id', $user->id)
+ ->exists();
+ if ( $check ) {
+ $actually_watching[] = $anime_details["mal_id"];
+ continue;
+ }
+ $anime = Anime::where('mal_id', $anime_details["mal_id"])->get()->first();
+ $user->IsWatching()->save( $anime, $anime_details );
+
+ $actually_watching[] = $anime_details["mal_id"];
+ }
+
+ /**
+ * Check for anime which aren't watched anymore and delete them.
+ */
+ $not_watched = DB::table('is_watching')
+ ->whereNotIn('mal_id', $actually_watching)
+ ->delete();
+ }
+
+ public function createUser( $username ) {
+ $user = MALUser::where('username', $username)->get()->first();
+
+ if ( is_null($user) ) {
+ $user = new MALUser();
+ $user->set( $username );
+
+ $user->save();
+ }
+
+ return $user;
+ }
}