1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Http\Controllers\Store;
7
8use App\Models\Store\Product;
9use Auth;
10use Illuminate\Database\QueryException;
11
12class NotificationRequestsController extends Controller
13{
14 public function __construct()
15 {
16 $this->middleware('auth');
17 if (!$this->isAllowRestrictedUsers()) {
18 $this->middleware('check-user-restricted');
19 }
20
21 parent::__construct();
22 }
23
24 public function store($productId)
25 {
26 $product = Product::findOrFail($productId);
27
28 if ($product->inStock()) {
29 return error_popup(osu_trans('store.product.notification_in_stock'));
30 }
31
32 try {
33 $product
34 ->notificationRequests()
35 ->create(['user_id' => Auth::user()->user_id]);
36 } catch (QueryException $e) {
37 if (!is_sql_unique_exception($e)) {
38 throw $e;
39 }
40 }
41
42 return ext_view('layout.ujs-reload', [], 'js');
43 }
44
45 public function destroy($productId)
46 {
47 Product::findOrFail($productId)
48 ->notificationRequests()
49 ->where('user_id', '=', Auth::user()->user_id)
50 ->delete();
51
52 return ext_view('layout.ujs-reload', [], 'js');
53 }
54}