Skip to content

Commit

Permalink
Merge pull request #4 from caubuonviaiV/user_optimization
Browse files Browse the repository at this point in the history
update
  • Loading branch information
dntrung044 authored Apr 1, 2024
2 parents 2562dc3 + 165c35a commit c6895f7
Show file tree
Hide file tree
Showing 936 changed files with 2,800 additions and 10,495 deletions.
14 changes: 0 additions & 14 deletions app/Http/Controllers/Admin/FeeShipController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,6 @@ public function create()
]);
}

public function add_address(Request $request)
{
$data = $request->all();
$output ='';
$data['action'] == "district" ;
$select = Ward::where('district_name', $data['district_name'])->get();
$output.='<option>---Chọn xã/phường---</option>';
foreach ($select as $key => $ward){
$output.='<option value="'.$ward->name.'">'.$ward->name.'</option>';
}

echo $output;
}

public function store(Request $request)
{
$this->validate($request, [
Expand Down
10 changes: 4 additions & 6 deletions app/Http/Controllers/User/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ public function changetPasswordHandle(User $id, $token, Request $request) {

// Login
public function login() {
if (Auth::check()) {
return redirect()->route('home');
}
return view('user.auth.login',[
'title' => 'Đăng nhập - Mì Quảng Bà Mua',
]);
Expand All @@ -181,8 +184,7 @@ public function loginHandle(Request $request) {
if (Auth::user()->level==1) {
return redirect('admin')->with('success', 'Đăng nhập thành công');
} else {
// return redirect()->back()->with('success', 'Đăng nhập với vài trò thành công');
return redirect()->intended('/');
return redirect()->intended('/')->with('success', 'Đăng nhập với vài trò thành công');
}
} elseif(Auth::attempt([
'email' => $request->input('email'),
Expand All @@ -207,10 +209,6 @@ public function loginAjaxHandle(Request $request) {
]);

if ($validation->fails()) {
// foreach($validation->messages()->getMessages() as $field_name => $messages)
// {
// $error_array[] = $messages;
// }
return response()->json(['error' => $validation->errors()->first()]);
}
else {
Expand Down
16 changes: 8 additions & 8 deletions app/Http/Controllers/User/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ public function popular_comment(Request $request)
->with('postCategory')
->firstOrFail();

$comments = PostComment::withCount('likes')
->with(['replies' => function ($query) {
$query->withCount('likes')->orderBy('likes_count', 'desc');
}])->where('post_id', $post_id)->where('active', 1)->get()
->orderByRaw('likes_count + (SELECT COALESCE(SUM(likes_count), 0) FROM replies WHERE comment_id = comments.id) DESC');
// $comments = PostComment::withCount('likes')
// ->with(['replies' => function ($query) {
// $query->withCount('likes')->orderBy('likes_count', 'desc');
// }])->where('post_id', $post_id)->where('active', 1)->get()
// ->orderByRaw('likes_count + (SELECT COALESCE(SUM(likes_count), 0) FROM replies WHERE comment_id = comments.id) DESC');

// $popular_comments = PostComment::withCount('likes')
// ->orderBy('number_like', 'desc')
// ->get();
$comments = PostComment::withCount('likes')
->orderBy('number_like', 'desc')
->get();
// $comments = PostCommentReply::whereIn('comment_id', $popular_comments->pluck('id'))
// ->withCount('likes')
// ->orderBy('number_like', 'desc')
Expand Down
34 changes: 33 additions & 1 deletion app/Http/Controllers/User/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,39 @@ public function add_to_cart(Request $request)
], 200);
}

public function destroy_cart(Request $request)
{
$productId = $request->id;
$carts = session()->get('carts');

if (isset($carts[$productId])) {
unset($carts[$productId]);
session()->put('carts', $carts);
session()->save();

$productsInCart = $this->cartService->getProduct();
$carts = Session::get('carts');
//price
$price = $quantity = $quantity_total = $subtotal = $total = $total_cart = 0;
foreach ($productsInCart as $key => $product) {
$price = $product->price_sale != 0 ? $product->price_sale : $product->price;
$quantity = $carts[$product->id];
$quantity_total += $quantity;
$subtotal = $price * $quantity;
$total_cart += $subtotal;
}
//render view
$cart_compoment = view('user.products.compoments.cart', compact('productsInCart', 'carts', 'price', 'quantity', 'subtotal','total_cart', 'quantity_total' ))->render();

return response()->json([
'cart_compoment' => $cart_compoment,
'code' => 200,
'success' => 'Đã xoá món ăn thành công!',
]);
}

return response()->json(['errors' => 'Không tìm thấy sản phẩm trong giỏ hàng!']);
}
public function showcartAjax()
{
$products = $this->cartService->getProduct();
Expand Down Expand Up @@ -281,7 +314,6 @@ public function check_coupon(Request $request)
}
}


public function payment_compoment()
{
$carts = Session::get('carts');
Expand Down
128 changes: 0 additions & 128 deletions app/Http/Controllers/User/CartController1.php

This file was deleted.

71 changes: 29 additions & 42 deletions app/Http/Controllers/User/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
use App\Models\User;
use App\Http\Services\User\UserService;
use App\Models\District;
use App\Models\Feeship;
use App\Models\ProductComment;
use App\Models\Transaction;
use App\Models\Ward;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;

class UserController extends Controller
{

protected $userSevice;

public function __construct(UserService $userSevice)
Expand All @@ -38,7 +36,7 @@ public function index(){
->count();

$totalDevliver = $GetTransaction
->where('status', Transaction::STATUS_DEVLIVERING)
->where('status', Transaction::STATUS_DELIVERING)
->select('id')->count();

$listComments = ProductComment::where('email', Auth::User()->email)->paginate(8);
Expand All @@ -65,18 +63,28 @@ public function changeInfor(User $user)
]);
}

public function updateInfor(Request $request, User $user)
{
$result = $this->userSevice->update($request, $user);
if ($result) {
alert('Cập nhật trạng thái thành công', 'success');
// return redirect()->intended('/');
return redirect()->back();
public function updateInfor(Request $request, User $user) {
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email,'.$user->id,
'phone' => 'required|string|max:12',
'street' => 'required|string|max:255',
'fee' => 'required|numeric',
]);

$userData = $request->only('name', 'email', 'phone', 'street', 'fee', 'district', 'ward');

try {
if ($user) {
$user->update($userData);
}
return redirect()->back()->with('success', 'Thông tin của bạn đã được cập nhật thành công.');
} catch (Exception $e) {
return redirect()->back()->with('danger', 'Đã có lỗi xảy ra. Vui lòng thử lại sau');
}
alert('Cập nhật lỗi', 'error');
return redirect()->back();
}


public function changePassword(User $user)
{
return view('User.user.editPassword', [
Expand All @@ -87,7 +95,6 @@ public function changePassword(User $user)

public function updatePassword(RequestPassword $requestPassword, User $user)
{
// dd($requestPassword->all());
if (Hash::check($requestPassword->password_old, Auth::user()->password)) {
$user = User::find( Auth::user()->id);
$user->password = bcrypt($requestPassword->password);
Expand All @@ -97,44 +104,24 @@ public function updatePassword(RequestPassword $requestPassword, User $user)

}
return redirect()->back()->with('danger', 'Mật khẩu không đúng');

}

public function load_address(Request $request)
{
$data = $request->all();
$output ='';
$data['action'] == "district" ;
$output ='';
$data['action'] == "district" ;

$select = Ward::where('district_name', $data['district_name'])->get();
$output.='<option>---Chọn xã/phường---</option>';
foreach ($select as $d){
$output.='<option value="'.$d->name.'">'.$d->name.'</option>';
}
return response()->json(['html' => $output]);
}

public function calculate_ship(Request $request)
{
$data = $request->all();
if($data['district'] && $data['ward'] ) {
$feeship = Feeship::where('district', $data['district'])->where('ward', $data['ward'])->firstOrFail();
if($feeship->feeship != '') {
return response()->json([
'status'=>200,
'fee' => $feeship->feeship
]);
} else {
return response()->json([
'status'=>400,
'nodata'=> '15000'
]);
}
$select = Ward::where('district_id', $data['district_id'])->get();
$output.='<option>---Chọn xã/phường---</option>';
foreach ($select as $d){
$output.='<option value="'.$d->id.'">'.$d->name.'</option>';
}
return response()->json(['html' => $output]);
}

public function redirectLogin()
{
return redirect()->route('/dang-nhap');
return redirect()->route('login');
}
}
2 changes: 0 additions & 2 deletions app/Http/Services/User/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

namespace App\Http\Services\User;

use App\Models\District;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;

class UserService
{
Expand Down
Loading

0 comments on commit c6895f7

Please sign in to comment.