Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mvc refactor #2

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions .tags1
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,3 @@
!_TAG_PROGRAM_NAME Exuberant Ctags //
!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/
!_TAG_PROGRAM_VERSION 5.9~svn20110310 //
UserProfileController /home/andres/Documents/Universidad/ProgWeb/proy_puericultura/app/Http/Controllers/UserProfileController.php /^class UserProfileController extends Controller$/;" class line:14
show /home/andres/Documents/Universidad/ProgWeb/proy_puericultura/app/Http/Controllers/UserProfileController.php /^ public function show($username)$/;" function line:18
user /home/andres/Documents/Universidad/ProgWeb/proy_puericultura/app/Http/Controllers/UserProfileController.php /^ $user = User::where('username', $username)->first();$/;" variable line:20
me /home/andres/Documents/Universidad/ProgWeb/proy_puericultura/app/Http/Controllers/UserProfileController.php /^ public function me(){$/;" function line:25
user /home/andres/Documents/Universidad/ProgWeb/proy_puericultura/app/Http/Controllers/UserProfileController.php /^ $user = User::find(Auth::user()->user_id);$/;" variable line:27
showEditForm /home/andres/Documents/Universidad/ProgWeb/proy_puericultura/app/Http/Controllers/UserProfileController.php /^ public function showEditForm(){$/;" function line:35
user /home/andres/Documents/Universidad/ProgWeb/proy_puericultura/app/Http/Controllers/UserProfileController.php /^ $user = User::find(Auth::user()->user_id);$/;" variable line:37
saveEdit /home/andres/Documents/Universidad/ProgWeb/proy_puericultura/app/Http/Controllers/UserProfileController.php /^ public function saveEdit()$/;" function line:45
user /home/andres/Documents/Universidad/ProgWeb/proy_puericultura/app/Http/Controllers/UserProfileController.php /^ $user = User::find(Auth::user()->user_id);$/;" variable line:47
24 changes: 24 additions & 0 deletions app/AccessPoints/Bans.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace App\AccessPoints;

use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Models\Bans as Bans;
use Auth;

class Bans extends Authenticatable{

public static function banearPersona(){
$user_id = request()->input('user_id');
$description = request()->input('description');
$typeBan = TB::where('description',"=", $description)->first();
$meses = request()->input('meses');
$fecha = C::now()->addMonths($meses);
$porque = request()->input('porque');
B::create([
'user_id' => $user_id,
'typeBan_id' => $typeBan,
'ban_date_end' => $fecha,
'description' => $porque
]);
}
}
53 changes: 53 additions & 0 deletions app/AccessPoints/Blog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\AccessPoints;

use App\Models\Post as PostM;
use App\Models\User as UserM;
use DB;

class Blog {
public static function mainList(){
# Gets the 4 more recent posts and save them in an array
$recentPosts = PostM::orderBy('created_at', 'desc')->get()->take(4)->toArray();

# Gets the 3 most liked posts
$mostLikedPostsQuery = DB::select('
SELECT posts.post_id, COUNT( posts.post_id ) AS PostIdCounter2
FROM posts
JOIN likes ON posts.post_id = likes.post_id
GROUP BY posts.post_id
ORDER BY PostIdCounter2 DESC
LIMIT 0 , 3
');


$currentPost;
$mostLikedPosts = array();

# Iterates for each post inside the mostLiked array
# Retrieve their model from the DB
# Push them into another array for later usage

foreach($mostLikedPostsQuery As $post){
$currentPost = DB::table('posts')->where('post_id', $post->post_id)->get();
$post2 = $currentPost[0];
array_push($mostLikedPosts, $post2);
}

return [
'recentPosts' => $recentPosts,
'mostLikedPosts' => $mostLikedPosts
];

}

public static function getPost($slug){
return $post = PostM::where('slug', '=', $slug)->firstOrFail();
}

public static function index(){
return PostM::paginate(4);
}

}
101 changes: 101 additions & 0 deletions app/AccessPoints/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace App\AccessPoints;

use App\Models\Post as PostM;
use App\Models\Like as LikeM;
use Auth;
use DB;

class Post {

public static function newPost(){
# Retrieves the data sent by the user
$title = request()->input('title');
$body = request()->input('body');
$tags = request()->input('tags');
$user_id = request()->input('user_id');
$slug = str_slug($title);

# Stores the model
PostM::create([
'title' => $title,
'body' => $body,
'tags' => $tags,
'slug' => $slug,
'user_id' => $user_id
]);

if(!is_null(PostM::where('slug', '=', $slug)->first())){
return true;
}

return false;

}

public static function hasHeAlreadyLikeThisPost(){
# Checks for the AJAX call
if(request()->ajax()){
# Finds the post where the call is coming from
$post = PostM::where('slug', '=', request()->input('slug'))->first();
$post_id = $post->post_id;

# Check if a Like already exists in that post with that user and return the "display" order
if(LikeM::where(['user_id' => request()->input('user_id'), 'post_id' => $post_id])->count() > 0){
return response()->json(['display' => true, 'user_id' => request()->input('user_id'), 'post_id' => $post_id]);
}else{
return response()->json(['display' => false]);
}
}
abort(404);
}

public static function addLike(){
# Cheks for the AJAX call
if(request()->ajax()){

# Retrieves the data from the AJAX call
$slug = request()->input('slug');
$user_id = request()->input('user_id');

# Gets the Post where the call is coming from
$post = PostM::where('slug', '=', $slug)->first();

# Stores the like
$like = LikeM::create(array(
'user_id' => $user_id,
'post_id' => $post->post_id
));

if($like){
return response()->json(['display' => true]);
}else{
return response()->json(['display' => false]);
}
}
}

public static function dislike(){

# Checks for the AJAX call
if(request()->ajax()){

# Retrieve the data from the AJAX call
$slug = request()->input('slug');
$user_id = request()->input('user_id');

# Finds the post where the call is made from
$post = PostM::where('slug', '=', $slug)->first();
$post_id = $post->post_id;

# Finds the Like model and delete it
$like = DB::table('likes')->where([
['user_id', '=', $user_id],
['post_id', '=', $post_id]
])->delete();
return response()->json(['display' => false]);
}
}

}
98 changes: 98 additions & 0 deletions app/AccessPoints/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
namespace App\AccessPoints;

use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Models\User as UserM;
use Auth;
use Mail;
use DB;

class User extends Authenticatable{

public static function login(){

$credentials = [
'email' => request()->input('email'),
'password' => request()->input('password'),
'confirmed' => 1
];
return Auth::attempt($credentials, true);
}

public static function logout(){
if(Auth::check()){
Auth::logout();
}
}

public static function register(){

$name = request()->input('name');
$email = request()->input('email');
$pwd = bcrypt(request()->input('password'));
$confirmation_code = str_random(55);

UserM::create([
'name' => $name,
'email' => $email,
'password' => $pwd,
'confirmation_code' => $confirmation_code
]);

self::sendConfirmationEmail($confirmation_code);

return $dataForTheView = array(
'name' => $name,
'email' => $email
);
}

private static function sendConfirmationEmail($confirmation_code){
Mail::send('email.verify', ['confirmation_code' => $confirmation_code], function($message) {
$message->from('[email protected]', 'GPNDS')->to(request()->input('email'), explode(' ',trim(request()->input('name')))[0])->subject('Verifica tu correo.');
});
}

public static function confirm($confirmationCode){
if(!$confirmationCode){
dd('No se encontró ningún código de verificación en la URL');
}
# Search and retrieve the user with the UNIQUE confirmation code
$user = UserM::where('confirmation_code', $confirmationCode)->first();
if(!$user){
return false;
}
$user->confirmed = 1;
$user->confirmation_code = null;
$user_id = $user->user_id;
$user->save();

# Pass the user_id field to the session to be used later
session()->flash('user_id', $user_id);
return true;
}

public static function setUsername(){
# Gets the user_id from the session
# Requests the username from the input
# Updates the user with the given username
$user_id = session()->get('user_id');
$username = request()->input('username');
session()->flash('user_id', $user_id);
DB::table('users')->where('user_id', $user_id)->update(['username' => $username]);
$user = DB::table('users')->where('user_id', $user_id)->get();
if(!is_null($user)){
return true;
}
return false;
}

public static function wasSuccessfullyRegistered(){
$user_id = session()->get('user_id');
if($user_id != null){
session()->forget('user_id');
return true;
}
return false;
}
}
Loading