Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
69.23% |
9 / 13 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| AIPromptRepository | |
69.23% |
9 / 13 |
|
83.33% |
5 / 6 |
8.43 | |
0.00% |
0 / 1 |
| getAll | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| findById | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLatestByProductAndName | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| update | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| delete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Repositories; |
| 4 | |
| 5 | use App\Http\Models\AIPrompts; |
| 6 | use App\Http\Repositories\interfaces\IAIPromptRepository; |
| 7 | use Illuminate\Support\Collection; |
| 8 | |
| 9 | /** |
| 10 | * Repository for AI prompt data access operations. |
| 11 | * |
| 12 | * This repository handles all database queries related to AI prompts, |
| 13 | * keeping data access logic separate from business logic. |
| 14 | */ |
| 15 | class AIPromptRepository implements IAIPromptRepository |
| 16 | { |
| 17 | /** |
| 18 | * Get all AI prompts ordered by newest first, with optional product filtering. |
| 19 | * |
| 20 | * @param string|null $product Optional product to filter by |
| 21 | * @return Collection<int, AIPrompts> Collection of AI prompts |
| 22 | */ |
| 23 | public function getAll(?string $product = null): Collection |
| 24 | { |
| 25 | $query = AIPrompts::query(); |
| 26 | |
| 27 | if ($product) { |
| 28 | $query->where('product', $product); |
| 29 | } |
| 30 | |
| 31 | return $query->orderBy('created_at', 'desc')->get(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Find an AI prompt by its ID. |
| 36 | * |
| 37 | * @param string $id The AI prompt ID |
| 38 | * @return AIPrompts|null The AI prompt or null if not found |
| 39 | */ |
| 40 | public function findById(string $id): ?AIPrompts |
| 41 | { |
| 42 | return AIPrompts::find($id); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get the latest version of an AI prompt by product and name. |
| 47 | * |
| 48 | * @param string $product The product identifier |
| 49 | * @param string $name The prompt name/action |
| 50 | * @return AIPrompts|null The latest version or null if not found |
| 51 | */ |
| 52 | public function getLatestByProductAndName(string $product, string $name): ?AIPrompts |
| 53 | { |
| 54 | return AIPrompts::where('product', $product) |
| 55 | ->where('name', $name) |
| 56 | ->orderBy('version', 'desc') |
| 57 | ->first(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Create a new AI prompt. |
| 62 | * |
| 63 | * @param array $data The AI prompt data |
| 64 | * @return AIPrompts The created AI prompt |
| 65 | */ |
| 66 | public function create(array $data): AIPrompts |
| 67 | { |
| 68 | return AIPrompts::create($data); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Update an existing AI prompt. |
| 73 | * |
| 74 | * @param AIPrompts $aiPrompt The AI prompt to update |
| 75 | * @param array $data The update data |
| 76 | * @return AIPrompts The updated AI prompt |
| 77 | */ |
| 78 | public function update(AIPrompts $aiPrompt, array $data): AIPrompts |
| 79 | { |
| 80 | $aiPrompt->update($data); |
| 81 | |
| 82 | return $aiPrompt->fresh(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Delete an AI prompt. |
| 87 | * |
| 88 | * @param AIPrompts $aiPrompt The AI prompt to delete |
| 89 | * @return bool True if deleted successfully |
| 90 | */ |
| 91 | public function delete(AIPrompts $aiPrompt): bool |
| 92 | { |
| 93 | return $aiPrompt->delete(); |
| 94 | } |
| 95 | } |