Skip to content

Commit 693cb6b

Browse files
author
Renato Marinho
committed
Add LabelService
1 parent 78fa1cf commit 693cb6b

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

app/Http/Controllers/Web/LabelController.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,7 @@ public function index($model, $slug_label)
2525

2626
public function store(LabelRequest $request)
2727
{
28-
$data = [
29-
'labelable_id' => $request->labelable_id,
30-
'labelable_type' => $request->labelable_type,
31-
'title' => $request->title,
32-
];
33-
34-
try {
35-
$label = Label::create($data);
36-
} catch (\Exception $e) {
37-
$label = Label::where('title', $request->title)->first();
38-
}
39-
40-
$relation = \Config::get('database.relation');
41-
42-
$result = $relation[$request->labelable_type]::where('id', $request->labelable_id)->first();
43-
44-
if (!$result->labels()->where('label_id', $label->id)->first()) {
45-
$result->labels()->attach([$label->id]);
46-
}
28+
resolve('LabelService')->create($request);
4729

4830
return back()->with('success', trans('gitscrum.label-added-successfully'));
4931
}

app/Services/LabelService.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Laravel GitScrum <https://github.com/GitScrum-Community/laravel-gitscrum>
4+
*
5+
* The MIT License (MIT)
6+
* Copyright (c) 2017 Renato Marinho <renato.marinho@s2move.com>
7+
*/
8+
9+
namespace GitScrum\Services;
10+
11+
use Config;
12+
use GitScrum\Http\Requests\LabelRequest;
13+
use GitScrum\Models\Label;
14+
15+
class LabelService extends Service
16+
{
17+
public function create(LabelRequest $request)
18+
{
19+
$data = [
20+
'labelable_id' => $request->labelable_id,
21+
'labelable_type' => $request->labelable_type,
22+
'title' => $request->title,
23+
];
24+
25+
try {
26+
$label = Label::create($data);
27+
} catch (\Exception $e) {
28+
$label = Label::where('title', $request->title)->first();
29+
}
30+
31+
$relation = Config::get('database.relation');
32+
33+
$result = $relation[$request->labelable_type]::where('id', $request->labelable_id)->first();
34+
35+
if (!$result->labels()->where('label_id', $label->id)->first()) {
36+
$result->labels()->attach([$label->id]);
37+
}
38+
39+
return $label;
40+
}
41+
}

config/app.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@
141141
'CommentService',
142142
'ConfigStatusService',
143143
'FavoriteService',
144-
'IssueService'
144+
'IssueService',
145+
'LabelService'
145146
],
146147

147148
/*

0 commit comments

Comments
 (0)