File size: 2,109 Bytes
5f32fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php

namespace App\Http\Controllers;

use App\Models\Favourite;
use App\Models\Prompt;
use Illuminate\Http\Request;

class PromptController extends Controller
{
    public function getAll()
    {
        $prompts = Prompt::where('user_id', auth()->user()->id)->get();
        $favourites = Favourite::where('type', 'prompt')
            ->where('user_id', auth()->user()->id)
            ->get();

        return view('panel.user.openai_chat.components.prompt_library_list', ['promptData' => $prompts, 'favData' => $favourites])->render();
    }

    public function addNew(Request $req)
    {
        $title = $req->title;
        $prompt = $req->prompt;

        $prompt_record = new Prompt([
            'user_id' => auth()->user()->id,
            'title'   => $title,
            'prompt'  => $prompt,
        ]);

        $prompt_record->save();

        $prompts = Prompt::where('user_id', auth()->user()->id)->get();
        $favourites = Favourite::where('type', 'prompt')
            ->where('user_id', auth()->user()->id)
            ->get();

        return view('panel.user.openai_chat.components.prompt_library_list', ['promptData' => $prompts, 'favData' => $favourites])->render();
    }

    public function updateFav(Request $req)
    {
        $id = $req->id;

        $favourites = Favourite::where('type', 'prompt')
            ->where('item_id', $id)
            ->where('user_id', auth()->user()->id)
            ->get();

        if ($favourites->count() != 0) {
            $favourites->each->delete();
        } else {
            $favourite = Favourite::create([
                'user_id' => auth()->user()->id,
                'type'    => 'chat',
                'item_id' => $id,
            ]);
        }

        $favourites = Favourite::where('type', 'prompt')
            ->where('user_id', auth()->user()->id)
            ->get();

        return response()->json($favourites);
    }

    public function deletePrompt(Request $req)
    {
        $id = $req->id;
        Prompt::where('id', $id)->delete();

        return response()->json(['status' => 'success']);
    }
}