M
M
Maxim2021-11-15 11:58:04
PHP
Maxim, 2021-11-15 11:58:04

Am I using the controller correctly or should this code be in the models?

This code is in the controller, is this correct, or do I need to create a method in the models and pass values ​​there?

public  function  buyProduct($id){
        $product = Product::where('id',$id)->get();
        $userId = Auth::user()->id;
        $sellerUserId = $product->first()->seller_user_id;
        $sellerUserIdBalance = User::where('id',$sellerUserId)->get()->first()->balance;
        $productOwnerId = $product->first()->owner_user_id;
        $productPrice = $product->first()->price;
        $userBalance = User::where('id',$userId)->get()->first()->balance;

        if($userId == $productOwnerId){
            $response = [
                'response' => 'You cant buy your product'
            ];
            return response($response,201);

        }
        elseif ($product->first()->status_id == SOLD){
            $response = [
                'response' => 'This product is sold'
            ];
            return response($response,201);
        }
        elseif ($productPrice >= $userBalance ){
            $response = [
                'response' => 'Not enough money'
            ];
            return response($response,201);
        }
        else{
                Product::where('id', $id)->update(array('owner_user_id' => $userId));
                Product::where('id', $id)->update(array('buyer_user_id' => $userId));
                Product::where('id', $id)->update(array('status_id' => SOLD));
                User::where('id', $userId)->update(array('balance' => $userBalance-$productPrice));
                User::where('id', $sellerUserId)->update(array('balance' => $sellerUserIdBalance+$productPrice));
            }
            return  Product::where('id',$id)->get();
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2021-11-22
@delphinpro

public function buyProduct($id){
$product = Product::where('id',$id)->get();

Well, here, as it were, you can immediately receive a finished model object in the parameter.
But even if you get by id, isn't it better to use one of the find..() group methods?
Conclusion: Lark's knowledge is near-zero.
Move on
$userId = Auth::user()->id;
$sellerUserId = $product->first()->seller_user_id;
$sellerUserIdBalance = User::where('id',$sellerUserId)->get()->first()->balance;

That is, we first get the user , take his ID, then look for this user ID in the database (and we already got it earlier) and get it again to pull the balance from there.
Conclusion: Lark's knowledge is near-zero.
User::where('id', $sellerUserId)->update(array('balance' => $sellerUserIdBalance+$productPrice));

And here we once again turn to the user table. What for? We have already received a user a couple of times ...
-------------------
But in general, on the issue, it seems to me the norms.
It is in the controller that we determine whether this user can make a purchase.
Well, or as an option, you can throw it in the middleware. Provided that these checks can be performed in subroutes. If the check is only in this route, then the rules.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question