Skip to content

Commit 82c278f

Browse files
committed
added delete api route that is compliant with the dotnet cli
1 parent a7158e7 commit 82c278f

File tree

7 files changed

+157
-110
lines changed

7 files changed

+157
-110
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Chocolatier allows you to host your own NuGet repository on any platform that ca
1616

1717
A live example of Chocolatier can be found at [choco.sage.edu](https://choco.sage.edu). The example site is also utilizing my [NuGetReflector](https://github.com/MelonSmasher/NuGetReflector).
1818

19-
The current release of Chocolatier is based on Laravel 5.4.
19+
The current release of Chocolatier is based on Laravel 5.8.
2020

2121
## Installation:
2222

app/Choco/NuGet/NugetPackage.php

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,29 @@
55
use Illuminate\Database\Eloquent\Model;
66
use App\Choco\Atom\AtomElement;
77
use App\Model\User;
8+
use Illuminate\Support\Facades\Storage;
89

910
/**
1011
* @property string hash
1112
* @property string hash_algorithm
12-
* @property int size
13-
* @property int user_id
14-
* @property mixed version
15-
* @property mixed is_prerelease
16-
* @property mixed package_id
17-
* @property mixed authors
18-
* @property mixed updated_at
19-
* @property int download_count
13+
* @property int size
14+
* @property int user_id
15+
* @property mixed version
16+
* @property mixed is_prerelease
17+
* @property mixed package_id
18+
* @property mixed authors
19+
* @property mixed updated_at
20+
* @property int download_count
2021
* @property string tags
21-
* @property bool is_absolute_latest_version
22-
* @property bool is_listed
23-
* @property int version_download_count
24-
* @property bool is_latest_version
25-
* @property mixed icon_url
26-
* @property mixed owners
22+
* @property bool is_absolute_latest_version
23+
* @property bool is_listed
24+
* @property int version_download_count
25+
* @property bool is_latest_version
26+
* @property mixed icon_url
27+
* @property mixed owners
2728
*/
28-
class NugetPackage extends Model {
29+
class NugetPackage extends Model
30+
{
2931
protected $fillable = [
3032
'package_id', /* string */
3133
'version', /* string */
@@ -69,52 +71,98 @@ public function user()
6971
return $this->belongsTo(User::class);
7072
}
7173

74+
/**
75+
* @return mixed
76+
*/
7277
public function versions()
7378
{
7479
return static::where('package_id', $this->package_id)
7580
->orderBy('created_at', 'desc')
7681
->get();
7782
}
83+
84+
/**
85+
* @return string
86+
*/
7887
public function getNupkgPath()
7988
{
8089
return storage_path() . "/app/packages/{$this->package_id}.{$this->version}.nupkg";
8190
}
8291

92+
/**
93+
* @return string
94+
*/
95+
public function getNupkgName()
96+
{
97+
return "{$this->package_id}.{$this->version}.nupkg";
98+
}
99+
100+
/**
101+
* @return bool|null
102+
* @throws \Exception
103+
*/
104+
public function delete()
105+
{
106+
Storage::delete("packages/{$this->getNupkgName()}");
107+
return parent::delete();
108+
}
109+
110+
/**
111+
* @return string
112+
*/
83113
public function getGalleryUrl()
84114
{
85115
return route('packages.show', [$this->package_id]);
86116
}
87117

118+
/**
119+
* @return string
120+
*/
88121
public function getApiQuery()
89122
{
90123
return "Packages(Id='{$this->package_id}',Version='{$this->version}')";
91124
}
92125

126+
/**
127+
* @return string
128+
*/
93129
public function getApiUrl()
94130
{
95131
return route('api.package', ['id' => $this->package_id, 'version' => $this->version]);
96132
}
97133

134+
/**
135+
* @return string
136+
*/
98137
public function getDownloadUrl()
99138
{
100139
return route('api.download', ['id' => $this->package_id, 'version' => $this->version]);
101140
}
102141

142+
/**
143+
* @return array
144+
*/
103145
public function getOwners()
104146
{
105147
return array_map('trim', explode(',', $this->owners));
106148
}
107149

150+
/**
151+
* @return array
152+
*/
108153
public function getAuthors()
109154
{
110155
return array_map('trim', explode(',', $this->authors));
111156
}
112157

158+
/**
159+
* @return mixed|string
160+
*/
113161
public function getIconUrl()
114162
{
115163
return !empty($this->icon_url)
116-
&& (strpos($this->icon_url, 'http://') === 0
117-
|| strpos($this->icon_url, 'https://') === 0) ? $this->icon_url : '/images/packageDefaultIcon.png';
164+
&& (strpos($this->icon_url, 'http://') === 0
165+
|| strpos($this->icon_url, 'https://') === 0) ? $this->icon_url : '/images/packageDefaultIcon.png';
118166
}
119167

120168
/**

app/Http/Controllers/ApiController.php

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44

55
use DOMDocument;
66

7-
use function GuzzleHttp\Psr7\str;
7+
88
use Illuminate\Support\Facades\Input;
9-
use Illuminate\Support\Facades\Log;
109
use Illuminate\Support\Facades\Response;
1110
use Illuminate\Support\Facades\Storage;
1211
use App\Choco\Atom\AtomElement;
13-
use App\Http\Requests;
1412
use App\Http\Requests\NugetRequest;
1513
use App\Nuget\NupkgFile;
1614
use App\Repositories\NugetQueryBuilder;
1715
use App\Choco\NuGet\NugetPackage;
1816

19-
class ApiController extends Controller {
17+
class ApiController extends Controller
18+
{
2019
private $queryBuilder;
2120

2221
/**
@@ -61,8 +60,7 @@ public function upload(NugetRequest $request)
6160
$user = $request->getUser();
6261
$file = $request->getUploadedFile('package');
6362

64-
if($file === false)
65-
{
63+
if ($file === false) {
6664
\Log::error('package not uploaded on second check');
6765
return Response('package not uploaded on second check', 500);
6866
}
@@ -74,6 +72,35 @@ public function upload(NugetRequest $request)
7472
return Response::make('OK');
7573
}
7674

75+
/**
76+
* @param NugetRequest $request
77+
* @param $id
78+
* @param $version
79+
* @return mixed
80+
*/
81+
public function delete(NugetRequest $request, $id, $version)
82+
{
83+
$user = $request->getUser();
84+
if ($user) {
85+
86+
$package = NugetPackage::where('package_id', $id)->where('version', $version)->firstOrFail();
87+
88+
$is_latest_version = $package->is_latest_version;
89+
$is_absolute_latest_version = $package->is_absolute_latest_version;
90+
91+
$package->delete();
92+
$nextVersion = NugetPackage::orderby('created_at', 'desc')->first();
93+
94+
if ($nextVersion) {
95+
if (!$nextVersion->is_latest_version) $nextVersion->is_latest_version = $is_latest_version;
96+
if (!$nextVersion->is_absolute_latest_version) $nextVersion->is_absolute_latest_version = $is_absolute_latest_version;
97+
if($nextVersion->isDirty()) $nextVersion->save();
98+
}
99+
return Response::make('No Content', 204);
100+
}
101+
return Response::make('Unauthorized', 403);
102+
}
103+
77104
/**
78105
* Download a package.
79106
*
@@ -93,16 +120,14 @@ public function download($id, $version = null)
93120
->where('version', $version)
94121
->first();
95122
}
96-
if ($package === null)
97-
{
123+
if ($package === null) {
98124
return Response::make('not found', 404);
99125
}
100126
$package->version_download_count++;
101127
$package->save();
102128

103129
foreach (NugetPackage::where('package_id', $id)
104-
->get() as $vPackage)
105-
{
130+
->get() as $vPackage) {
106131
$vPackage->download_count++;
107132
$vPackage->save();
108133
}
@@ -118,8 +143,7 @@ public function download($id, $version = null)
118143
*/
119144
public function search($action)
120145
{
121-
if ($action == 'count' || $action == '$count')
122-
{
146+
if ($action == 'count' || $action == '$count') {
123147
$count = $this->processSearchQuery()
124148
->count();
125149

@@ -168,8 +192,7 @@ public function package($id, $version)
168192
->where('version', $version)
169193
->first();
170194

171-
if ($package == null)
172-
{
195+
if ($package == null) {
173196
return $this->generateResourceNotFoundError('Packages');
174197
}
175198

@@ -211,25 +234,21 @@ public function updates()
211234
//$version_constraints= explode('|', Input::get('versionConstraints'));//@todo ??
212235
//$target_frameworks= explode('|', Input::get('targetFrameworks'));//@todo ??
213236

214-
if (count($package_ids) != count($package_versions))
215-
{
237+
if (count($package_ids) != count($package_versions)) {
216238
return $this->generateError('Invalid version count', 'eu-US', 301);
217239
}
218240

219241
// Query database.
220242
$packages = [];
221-
foreach ($package_ids as $index => $id)
222-
{
243+
foreach ($package_ids as $index => $id) {
223244
$version = $package_versions[$index];
224245
$builder = NugetPackage::where('package_id', $id);
225-
if (!$include_prerelease)
226-
{
246+
if (!$include_prerelease) {
227247
$builder = $builder->where('is_prerelease', false);
228248
}
229249
$latest = $builder->orderBy('created_at', 'desc')
230250
->first();
231-
if ($latest != null && $latest->version != $version)
232-
{
251+
if ($latest != null && $latest->version != $version) {
233252
array_push($packages, $latest);
234253
}
235254
}
@@ -257,29 +276,23 @@ private function generateResourceNotFoundError($segmentName)
257276

258277
/**
259278
* @param NugetPackage $package
260-
* @param array $properties
261-
* @param AtomElement $atomElement
279+
* @param array $properties
280+
* @param AtomElement $atomElement
262281
*/
263282
private function addPackagePropertiesToAtomElement($package, $properties, $atomElement)
264283
{
265-
foreach ($properties as $property)
266-
{
267-
if (!$this->queryBuilder->isProperty($property))
268-
{
284+
foreach ($properties as $property) {
285+
if (!$this->queryBuilder->isProperty($property)) {
269286
continue;
270287
}
271288

272289
$mapping = $this->queryBuilder->getMapping($property);
273290

274-
if (array_has($mapping, 'function'))
275-
{
291+
if (array_has($mapping, 'function')) {
276292
$func = $mapping['function'];
277293
$value = $package->$func();
278-
}
279-
else
280-
{
281-
if (array_has($mapping, 'field'))
282-
{
294+
} else {
295+
if (array_has($mapping, 'field')) {
283296
$field = $mapping['field'];
284297
$value = $package->$field;
285298
}
@@ -297,14 +310,13 @@ private function addPackagePropertiesToAtomElement($package, $properties, $atomE
297310
* @param $id
298311
* @param $title
299312
* @param $updated
300-
* @param mixed $count
313+
* @param mixed $count
301314
* @return mixed
302315
*/
303316
private function displayPackages($packages, $id, $title, $updated, $count = false)
304317
{
305318
$properties = Input::has('$select')
306-
? array_filter(explode(',', Input::get('$select')), function ($name)
307-
{
319+
? array_filter(explode(',', Input::get('$select')), function ($name) {
308320
return $this->queryBuilder->isProperty($name);
309321
})
310322
: $this->queryBuilder->getAllProperties();
@@ -314,8 +326,7 @@ private function displayPackages($packages, $id, $title, $updated, $count = fals
314326
->setCount($count);
315327

316328
/** @var NugetPackage $package */
317-
foreach ($packages as $package)
318-
{
329+
foreach ($packages as $package) {
319330
$atomElement = $package->getAtomElement();
320331
$this->addPackagePropertiesToAtomElement($package, $properties, $atomElement);
321332
$atom->appendChild($atomElement);
@@ -340,10 +351,8 @@ private function processSearchQuery()
340351
// Query database.
341352
$eloquent = $this->queryBuilder->query(Input::get('$filter'), Input::get('$orderby'));
342353

343-
if (!empty($search_term))
344-
{
345-
$eloquent = $eloquent->where(function ($query) use ($search_term)
346-
{
354+
if (!empty($search_term)) {
355+
$eloquent = $eloquent->where(function ($query) use ($search_term) {
347356
$query->where('package_id', 'LIKE', "%$search_term%");
348357
$query->orWhere('title', 'LIKE', "%$search_term%");
349358
$query->orWhere('description', 'LIKE', "%$search_term%");
@@ -352,8 +361,7 @@ private function processSearchQuery()
352361
$query->orWhere('authors', 'LIKE', "%$search_term%");
353362
});
354363
}
355-
if (!$include_prerelease)
356-
{
364+
if (!$include_prerelease) {
357365
$eloquent->where('is_prerelease', false);
358366
}
359367

0 commit comments

Comments
 (0)