-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputerScienceResourceService.php
More file actions
162 lines (139 loc) · 5.87 KB
/
Copy pathComputerScienceResourceService.php
File metadata and controls
162 lines (139 loc) · 5.87 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace App\Services;
use App\Exceptions\Resources\ResourceAlreadyCreatedException;
use App\Exceptions\Resources\ResourceInvalidTabException;
use App\Models\ComputerScienceResource;
use App\Models\ResourceEdits;
use App\Models\ResourceReview;
use App\Services\SortingManagers\ResourceSortingManager;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Inertia\Inertia;
use Throwable;
class ComputerScienceResourceService
{
public function __construct(
protected CommentService $commentService,
protected UpvoteService $upvoteService,
protected ResourceReviewService $reviewService,
protected ResourceSortingManager $resourceSortingManager,
) {}
/**
* Create a new ComputerScienceResource
*
* @throws Throwable
*/
public function createResource(array $validatedData): ComputerScienceResource
{
if ($conflictingResource = $this->existingConflictingResource($validatedData)) {
throw new ResourceAlreadyCreatedException($conflictingResource);
}
DB::beginTransaction();
try {
// Store the image onto storage
$path = null;
if (array_key_exists('image_file', $validatedData) && $imageFile = $validatedData['image_file']) {
// TODO: FIGURE OUT WHAT TO DO IN CASE OF EXCEPTION IN CODE FROM LATER STEPS
$path = $imageFile->store('resource', 'public');
}
$resource = ComputerScienceResource::create([
'user_id' => Auth::id(),
'name' => $validatedData['name'],
'image_path' => $path,
'description' => $validatedData['description'],
'page_url' => $validatedData['page_url'],
'platforms' => $validatedData['platforms'],
'difficulty' => $validatedData['difficulty'],
'pricing' => $validatedData['pricing'],
]);
// Add topics as tags
$resource->topic_tags = $validatedData['topic_tags'];
// Add programming languages as tags (if provided)
if (isset($validatedData['programming_language_tags'])) {
$resource->programming_language_tags = $validatedData['programming_language_tags'];
}
// Add general tags (if provided)
if (isset($validatedData['general_tags'])) {
$resource->general_tags = $validatedData['general_tags'];
}
DB::commit();
$this->upvoteService->upvote('resource', $resource->id);
Log::info('Resource created', [
'resource_id' => $resource->id,
'user_id' => Auth::id(),
'name' => $resource->name,
'slug' => $resource->slug,
'platforms' => $resource->platforms,
]);
return $resource;
} catch (Throwable $e) {
DB::rollBack();
Log::critical('Failed to create resource', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
'user_id' => Auth::id(),
'data' => $validatedData,
]);
throw $e;
}
}
/**
* Get all data for the resource show page, including tab logic.
*
* @throws Throwable
*/
public function getShowResourceData(Request $request, string $slug, string $tab = 'reviews')
{
$computerScienceResource = ComputerScienceResource::where('slug', $slug)->firstOrFail();
$computerScienceResource->load('reviewSummary');
$computerScienceResource->load('user');
$validTabs = ['reviews', 'discussion', 'edits'];
if (! in_array($tab, $validTabs)) {
throw new ResourceInvalidTabException('Invalid tab: '.$tab);
}
$data = [
'tab' => $tab,
'resource' => $computerScienceResource,
];
$sortBy = $request->query('sort_by', 'top');
if ($tab === 'reviews') {
$userReview = null;
if ($userId = Auth::id()) {
$userReview = ResourceReview::whereBelongsTo($computerScienceResource)
->firstWhere('user_id', $userId);
}
$data['userReview'] = $userReview;
$data['reviews'] = Inertia::defer(
function () use ($computerScienceResource, $sortBy, $request) {
$query = ResourceReview::whereBelongsTo($computerScienceResource);
$query = $this->resourceSortingManager->applySort($query, $sortBy, ResourceReview::class);
return $query->with('user')->paginate(10)->appends($request->query());
}
);
} elseif ($tab === 'edits') {
$data['resourceEdits'] = Inertia::defer(
function () use ($computerScienceResource, $sortBy, $request) {
$query = ResourceEdits::whereBelongsTo($computerScienceResource);
$query = $this->resourceSortingManager->applySort($query, $sortBy, ResourceEdits::class);
return $query->with('user')->paginate(10)->appends($request->query());
}
);
} elseif ($tab === 'discussion') {
$data['discussion'] = Inertia::defer(
fn () => $this->commentService->getPaginatedComments('resource', $computerScienceResource->id, 0, 150, $sortBy)
);
}
return $data;
}
/**
* In case a user does a double submit, we have a check for that
* Checks against StoreResourceRequest
*/
private function existingConflictingResource(array $data): ?ComputerScienceResource
{
return ComputerScienceResource::where('page_url', $data['page_url'])->first();
}
}