From 2edac1f5607839efc70be2d98237a6de4b28d6d8 Mon Sep 17 00:00:00 2001 From: horus Date: Fri, 6 Mar 2020 00:59:24 +0100 Subject: Background jobs should be okay. --- app/Libraries/Helper.php | 58 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'app/Libraries/Helper.php') 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 @@ get()->first(); - echo "
";
+
+		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;
+	}
 
 }
-- 
cgit v1.2.3