Skip to content

Commit

Permalink
like/unlike
Browse files Browse the repository at this point in the history
  • Loading branch information
linrium committed Dec 21, 2016
1 parent 4cb257e commit fa8c429
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 63 deletions.
35 changes: 14 additions & 21 deletions app/Http/Controllers/LikeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,24 @@

use Illuminate\Http\Request;

use App\Like;
use App\Episode;
use Auth;

class LikeController extends Controller
{
public function likeMovie($id)
{
// check if product exists or is valid or whatever
$this->handleLike('App\Movie', $id);
public function like($id) {
$like = new Like();
$like->user_id = Auth::id();
$like->episode_id = $id;
$like->save();

return redirect()->back();
}

public function unlike($id) {
Like::destroy($id);

public function handleLike($type, $id)
{
$existing_like = Like::withTrashed()->whereLikeableType($type)->whereLikeableId($id)->whereUserId(Auth::id())->first();

if (is_null($existing_like)) {
Like::create([
'user_id' => Auth::id(),
'likeable_id' => $id,
'likeable_type' => $type,
]);
} else {
if (is_null($existing_like->deleted_at)) {
$existing_like->delete();
} else {
$existing_like->restore();
}
}
return redirect()->back();
}
}
25 changes: 21 additions & 4 deletions app/Http/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Movie;
use App\Episode;
use App\Genres;
use App\Like;
use Auth;

class PageController extends Controller
Expand All @@ -23,27 +24,43 @@ public function mapArray($option, $data) {
return $str;
}

public function getUserLiked($arrLike, &$likeId) {
foreach($arrLike as $like) {
if($like['user_id'] == Auth::id()) {
$likeId = $like['id'];
return true;
}
}
return false;
}

public function index($id, $episodeId) {
$movie = Movie::find($id)->toArray();
$movies = Movie::select('id', 'name', 'thumb', 'views', 'likes')->orderByRaw("RAND()")->limit(10)->orderBy('id', 'DESC')->get()->toArray();

$episodes = Episode::where('movie_id', $id)->select('id', 'name', 'views', 'likes')->limit(10)->get()->toArray();
$episode = Episode::find($episodeId)->toArray();

// // get data
// get data
$genres = Movie::find($id)->genre()->get()->toArray();
$producers = Movie::find($id)->producer()->get()->toArray();
$keywords = Movie::find($id)->keyword()->get()->toArray();
$links = Episode::find($episodeId)->link()->get()->toArray();

// // convert data to array;
// convert data to array;
$arrGenres = $this->mapArray('webmovie.dev/genre/anime', $genres);
$arrProducers = $this->mapArray('webmovie.dev/producer/anime', $producers);
$arrTags = $this->mapArray('webmovie.dev/keyword/anime', $keywords);

// like
$like = Like::where('episode_id','=',$episodeId)->get()->toArray();
$totalLiked = count($like);
$likeId = 0;
$isLiked = $this->getUserLiked($like, $likeId);

return view('home.page', compact('links', 'movie', 'movies', 'episodes', 'episode', 'arrGenres', 'arrProducers', 'arrTags','id', 'episodeId'));
return view('home.page', compact('likeId','isLiked','totalLiked','links', 'movie', 'movies', 'episodes', 'episode', 'arrGenres', 'arrProducers', 'arrTags','id', 'episodeId'));
// echo '<pre>';
// print_r($episode['views']);
// print_r($likeId);
}

}
12 changes: 1 addition & 11 deletions app/Like.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,14 @@
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Like extends Model
{
use SoftDeletes;

protected $table = 'likeables';

protected $fillable = [
'user_id',
'likeable_id',
'likeable_type',
'episode_id',
];

/**
* Get all of the products that are assigned this like.
*/
public function movie() {
return $this->morphedByMany('App\Movie', 'likeable');
}
}
9 changes: 0 additions & 9 deletions app/Movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,4 @@ public function fansub() {
public function keyword() {
return $this->belongsToMany('App\Keyword', 'movie_keywords')->withTimestamps();
}

public function likes(){
return $this->morphToMany('App\User', 'likeable')->whereDeletedAt(null);
}

public function getIsLikedAttribute(){
$like = $this->likes()->whereUserId(Auth::id())->first();
return (!is_null($like)) ? true : false;
}
}
4 changes: 0 additions & 4 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,4 @@ class User extends Authenticatable
protected $hidden = [
'password', 'remember_token',
];

public function likedMovie() {
return $this->morphedByMany('App\Movie', 'likeable')->whereDeletedAt(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ public function up()
Schema::create('likeables', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('likeable_id');
$table->string('likeable_type');
$table->softDeletes();
$table->integer('episode_id');
$table->timestamps();
});
}
Expand Down
27 changes: 19 additions & 8 deletions resources/views/home/page.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,25 @@
<div class="main__watch-info">
<h1>{!! $movie['name'] !!}</h1>
<div class="row">
<div class="col-md-8" style="line-height: 50px;"><span id="likes"><i class="fa fa-heart"></i> <span id="count-likes" style="display: inline">{{ $episode['likes'] }}</span>
<?php print_r($movie) ?>
@if ($movie->isLiked)
<a href="{{ route('movie.like', $movie->id) }}">Unlike</a>
@else
<a href="{{ route('movie.like', $movie->id) }}">Like</a>
@endif
</span></div>
<div class="col-md-8" style="line-height: 50px;">
<span id="likes">
<i class="fa fa-heart"></i>
{{ $totalLiked }}
@if ($isLiked)
<form action="{{ route('episode.unlike', $likeId) }}" method="post" style="display: inline">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="id" value="{{ $likeId }}">
<button style="background: none; border: none; padding: 0; cursor: pointer; margin-top: -5px" type="submit" class="btn btn-error" >Unlike</button>
</form>
@else
<form action="{{ route('episode.like', $episode['id']) }}" method="post" style="display: inline">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button style="background: none; border: none; padding: 0; cursor: pointer; margin-top: -5px" type="submit" class="btn btn-error" >Like</button>
</form>
@endif
</span>
</div>
<div class="col-md-4"><span id="views" class="hvr-underline-from-left"> <span id="count-views" style="display: inline">{{ Counter::showAndCount('episodes', $episode['id'] ) }}</span> views</span></div>
</div>
<span class="span-inline"><i class="fa fa-list"></i> Genre: {!! $arrGenres !!}</span>
Expand Down
10 changes: 7 additions & 3 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
});

// like
Route::get('anime/like/{id}', [
'as' => 'movie.like',
'uses' => 'LikeController@likeMovie'
Route::post('anime/like/{id}', [
'as' => 'episode.like',
'uses' => 'LikeController@like'
]);
Route::delete('anime/unlike/{id}', [
'as' => 'episode.unlike',
'uses' => 'LikeController@unlike'
]);

// search
Expand Down

0 comments on commit fa8c429

Please sign in to comment.