<file_sep><?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Post;
use App\Like;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Database\QueryException;

class PostController extends Controller
{

    public function gotoDashboard()
    {
        $posts = Post::orderBy('created_at', 'desc')->get();
        return view('dashboard', ['posts' => $posts]);
    }

    public function likePost(Request $request)
    {
        // TODO : find like, create new like, fill the fields, add number of likes

            $post = Post::find($request['postId']);
            $user = Auth::user();
            $user_id = $user->id;
            
            // ** find liked post
            $like = $user->likes()->where('post_id',$post->id)->first();
            if($like){
                // ? : if liked by the user already
                // todo : unlike it
                if($like->is_liked){
                    $like->delete();
                    return response()->json(['msg'=>'success unliked'], 200);
                }

            }
            else{
                // ? if post is not liked 
                $like = new Like();
                $like->post_id = $post->id;
                $like->user_id = $user->id;
                $like->is_liked = 1;
                $like->save();
                return response()->json(['msg' => 'success liked'], 200);
            }
        }
        
    

    public function createPost(Request $request)
    {
        $post = new Post();
        $post->body = $request['body'];
        // ? get the current user and save the relation post
        if ($request->user()->posts()->save($post)) {
            return redirect()->route('dashboard');
        }
    }

    public function deletePost($post_id)
    {
        if (Auth::user() != $post->user) { // ? protecting route from other users
            return redirect()->back();
        }
        $post = Post::where('id', $post_id)->first();
        $post->delete();
        return redirect('dashboard');
    }

    public function editPost(Request $request)
    {
        $post = Post::find($request['postId']);
        $post->body = $request['body'];
        $post->update();
        return response()->json(['message' => 'post edited'], 200);
    }
}
<file_sep><?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
 */

Route::get('/', function () {
    return view('welcome');
});

Route::get('/x', function () {
    return view('x');
});

Route::get('/first', function () {
    return view('first');
});

Route::get('/signup', function () {
    return view('signup');
});

Route::get('/dashboard', [
    'uses' => 'PostController@gotoDashboard',
    'as' => 'dashboard',
]);

Route::get('/profile', [
    'uses' => 'UserController@profile',
    'as' => 'profile',
]);

Route::get('/profilepic/{filename}', [
    'uses' => 'UserController@getProfilePic',
    'as' => 'profile.pic',
]);

Route::get('/logout', [
    'uses' => 'UserController@logout',
    'as' => 'logout',
]);

Route::post('/createUser', [
    'uses' => 'UserController@postSignup',
    'as' => 'create', // ? name of route
]);

Route::post('/editProfile', [
    'uses' => 'UserController@editProfile',
    'as' => 'profile.edit', // ? name of route
]);

Route::post('/saveProfilePic', [
    'uses' => 'UserController@saveProfilePic',
    'as' => 'profile.pic.save', // ? name of route
]);

Route::post('/loginUser', [
    'uses' => 'UserController@postLogin',
    'as' => 'login', // ? name of route
]);

Route::post('/createpost', [
    'uses' => 'PostController@createPost',
    'as' => 'post.create', // ? name of route
]);

Route::post('/editpost', [
    'uses' => "PostController@editPost",
    'as' => 'editpost',
]);

Route::get('/deletepost/{post_id}', [
    'uses' => 'PostController@deletePost',
    'as' => 'post.delete',
]);

Route::post('/like', [
    'uses' => "PostController@likePost",
    'as' => 'like',
]);
<file_sep><?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\User;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

// use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{

    public function profile()
    {
        return view('profile', ['user' => Auth::user()]);
    }

    public function getProfilePic($filename)
    {
        $file = Storage::disk('local')->get($filename);
        return new Response($file, 200);
    }

    public function saveProfilePic(Request $request)
    {
        $file = $request->file('profilepic');
        $filename = Auth::user()->username. '.jpg';

        if ($file) {
            Storage::disk('local')->put($filename, File::get($file));
            return redirect('profile');
        }

    }

    public function editProfile(Request $request)
    {
        $user = Auth::user();
        $user->username = $request['username'];
        $user->email = $request['email'];
        $user->update();

        return redirect('profile');
    }

    public function postSignup(Request $request)
    {

        $request->validate([
            'email' => 'required|email|unique:users', // ** users is the table name
            'password' => '<PASSWORD>',
            'username' => 'required|unique:users',
        ]);

        $email = $request['email'];
        $password = <PASSWORD>($request['password']);
        $username = $request['username'];

        $user = new User();
        $user->email = $email;
        $user->password = $<PASSWORD>;
        $user->username = $username;

        $user->save();
        Auth::login($user);
        return redirect()->route('dashboard');
    }

    public function postLogin(Request $request)
    {

        if (Auth::attempt(['email' => $request['email'], 'password' => $request['password']])) {
            return redirect()->route('dashboard');
        } else {
            return redirect()->back();
        }

    }

    public function logout()
    {
        Auth::logout();
        return redirect('/first');
    }
}
