Guest

Untitled 1275

Apr 14th, 2026
9
0
Never
Not a member of GistPad yet? Sign Up, it unlocks many cool features!
None 3.36 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Http;
  8.  
  9. class InterviewAntiPatternController extends Controller
  10. {
  11. public function store(Request $request)
  12. {
  13. $data = $request->all();
  14. $discount = ($data['is_vip'] ?? false) ? 0.2 : 0;
  15.  
  16. $userId = DB::table('users')->insertGetId([
  17. 'name' => $data['name'] ?? 'Unknown',
  18. 'email' => $data['email'] ?? null,
  19. // Anti-pattern: password is stored in plaintext.
  20. 'password' => $data['password'] ?? 'secret',
  21. 'created_at' => now(),
  22. 'updated_at' => now(),
  23. ]);
  24.  
  25. $total = 0;
  26.  
  27. foreach ($data['items'] ?? [] as $item) {
  28. $product = DB::table('products')->where('id', $item['product_id'])->first();
  29. $price = $product->price ?? 0;
  30. $total += $price * ($item['qty'] ?? 1);
  31. }
  32.  
  33. $total = $total - ($total * $discount);
  34.  
  35. 'user_id' => $userId,
  36. 'email' => $data['email'] ?? null,
  37. ]);
  38.  
  39. dump($total);
  40.  
  41. return DB::table('users')->where('id', $userId)->first();
  42. }
  43. }
RAW Paste Data Copied