Skip to content

Commit 66f15a8

Browse files
author
Shawn McCool
committed
add forum crud services
1 parent 8af589d commit 66f15a8

16 files changed

+286
-86
lines changed

app/Lio/Accounts/UserCreator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* This class can call the following methods on the observer object:
55
*
6-
* userValidationError($validator->getErrors())
7-
* userSuccessfullyCreated($user)
6+
* userValidationError($errors)
7+
* userCreated($user)
88
*/
99
class UserCreator
1010
{
@@ -15,7 +15,7 @@ public function __construct(UserRepository $users)
1515
$this->users = $users;
1616
}
1717

18-
public function create($observer, $data, $validator = null)
18+
public function create(UserCreatorObserver $observer, $data, $validator = null)
1919
{
2020
// check the passed in validator
2121
if ($validator && ! $validator->isValid()) {
@@ -33,6 +33,6 @@ private function createValidUserRecord($observer, $data)
3333
return $observer->userValidationError($user->getErrors());
3434
}
3535

36-
return $observer->userSuccessfullyCreated($user);
36+
return $observer->userCreated($user);
3737
}
3838
}

app/Lio/Accounts/UserCreatorObserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
interface UserCreatorObserver
44
{
55
public function userValidationError($errors);
6-
public function userSuccessfullyCreated($user);
6+
public function userCreated($user);
77
}

app/Lio/Forum/ForumReplyCreator.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php namespace Lio\Forum;
2+
3+
use Lio\Comments\CommentRepository;
4+
5+
/**
6+
* This class can call the following methods on the observer object:
7+
*
8+
* forumReplyValidationError($errors)
9+
* forumReplyCreated($reply)
10+
*/
11+
class ForumReplyCreator
12+
{
13+
protected $comments;
14+
15+
public function __construct(CommentRepository $comments)
16+
{
17+
$this->comments = $comments;
18+
}
19+
20+
public function create(ForumReplyCreatorObserver $observer, $data, $validator = null)
21+
{
22+
// check the passed in validator
23+
if ($validator && ! $validator->isValid()) {
24+
return $observer->forumReplyValidationError($validator->getErrors());
25+
}
26+
return $this->createValidRecord($observer, $data);
27+
}
28+
29+
private function createValidRecord($observer, $data)
30+
{
31+
$reply = $this->comments->getNew($data);
32+
33+
// check the model validation
34+
if ( ! $this->comments->save($reply)) {
35+
return $observer->forumReplyValidationError($reply->getErrors());
36+
}
37+
38+
$this->attachReply($reply, $data['thread']);
39+
40+
return $observer->forumReplyCreated($reply);
41+
}
42+
43+
private function attachReply($reply, $thread)
44+
{
45+
$thread->children()->save($reply);
46+
}
47+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php namespace Lio\Forum;
2+
3+
interface ForumReplyCreatorObserver
4+
{
5+
public function forumReplyValidationError($errors);
6+
public function forumReplyCreated($reply);
7+
}

app/Lio/Forum/ForumReplyForm.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php namespace Lio\Forum;
2+
3+
use Lio\Core\FormBase;
4+
5+
class ForumReplyForm extends FormBase
6+
{
7+
protected $validationRules = [
8+
'body' => 'required',
9+
];
10+
}

app/Lio/Forum/ForumReplyUpdater.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php namespace Lio\Forum;
2+
3+
use Lio\Comments\CommentRepository;
4+
5+
/**
6+
* This class can call the following methods on the observer object:
7+
*
8+
* forumReplyValidationError($errors)
9+
* forumReplyUpdated($reply)
10+
*/
11+
class ForumReplyUpdater
12+
{
13+
protected $comments;
14+
15+
public function __construct(CommentRepository $comments)
16+
{
17+
$this->comments = $comments;
18+
}
19+
20+
public function update($reply, ForumReplyUpdaterObserver $observer, $data, $validator = null)
21+
{
22+
// check the passed in validator
23+
if ($validator && ! $validator->isValid()) {
24+
return $observer->forumReplyValidationError($validator->getErrors());
25+
}
26+
return $this->updateRecord($reply, $observer, $data);
27+
}
28+
29+
private function updateRecord($reply, $observer, $data)
30+
{
31+
$reply->fill($data);
32+
33+
// check the model validation
34+
if ( ! $this->comments->save($reply)) {
35+
return $observer->forumReplyValidationError($reply->getErrors());
36+
}
37+
38+
return $observer->forumReplyUpdated($reply);
39+
}
40+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php namespace Lio\Forum;
2+
3+
interface ForumReplyUpdaterObserver
4+
{
5+
public function forumReplyValidationError($errors);
6+
public function forumReplyUpdated($reply);
7+
}

app/Lio/Forum/ForumThreadCreator.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php namespace Lio\Forum;
2+
3+
use Lio\Comments\CommentRepository;
4+
5+
/**
6+
* This class can call the following methods on the observer object:
7+
*
8+
* forumThreadValidationError($errors)
9+
* forumThreadCreated($thread)
10+
*/
11+
class ForumThreadCreator
12+
{
13+
protected $comments;
14+
15+
public function __construct(CommentRepository $comments)
16+
{
17+
$this->comments = $comments;
18+
}
19+
20+
public function create(ForumThreadCreatorObserver $observer, $data, $validator = null)
21+
{
22+
// check the passed in validator
23+
if ($validator && ! $validator->isValid()) {
24+
return $observer->forumThreadValidationError($validator->getErrors());
25+
}
26+
return $this->createValidRecord($observer, $data);
27+
}
28+
29+
private function createValidRecord($observer, $data)
30+
{
31+
$thread = $this->comments->getNew($data);
32+
33+
// check the model validation
34+
if ( ! $this->comments->save($thread)) {
35+
return $observer->forumThreadValidationError($thread->getErrors());
36+
}
37+
38+
if (isset($data['tags'])) {
39+
$this->attachTags($thread, $data['tags']);
40+
}
41+
42+
return $observer->forumThreadCreated($thread);
43+
}
44+
45+
private function attachTags($thread, $tags)
46+
{
47+
$thread->tags()->sync($tags->lists('id'));
48+
}
49+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php namespace Lio\Forum;
2+
3+
interface ForumThreadCreatorObserver
4+
{
5+
public function forumThreadValidationError($errors);
6+
public function forumThreadCreated($thread);
7+
}

app/Lio/Comments/ForumCreateForm.php renamed to app/Lio/Forum/ForumThreadForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<?php namespace Lio\Comments;
1+
<?php namespace Lio\Forum;
22

33
use Lio\Core\FormBase;
44
use App, Validator;
55

6-
class ForumCreateForm extends FormBase
6+
class ForumThreadForm extends FormBase
77
{
88
protected $validationRules = [
99
'title' => 'required|min:10',

0 commit comments

Comments
 (0)