-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to post comment to project messages (#1322)
- Loading branch information
Showing
28 changed files
with
1,541 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
app/Http/Controllers/Company/Company/Project/ProjectMessagesCommentController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Company\Company\Project; | ||
|
||
use App\Helpers\DateHelper; | ||
use App\Helpers\ImageHelper; | ||
use Illuminate\Http\Request; | ||
use App\Helpers\StringHelper; | ||
use App\Helpers\InstanceHelper; | ||
use Illuminate\Http\JsonResponse; | ||
use App\Http\Controllers\Controller; | ||
use App\Services\Company\Project\CreateProjectMessageComment; | ||
use App\Services\Company\Project\UpdateProjectMessageComment; | ||
use App\Services\Company\Project\DestroyProjectMessageComment; | ||
|
||
class ProjectMessagesCommentController extends Controller | ||
{ | ||
/** | ||
* Create the message's comment. | ||
* | ||
* @param Request $request | ||
* @param int $companyId | ||
* @param int $projectId | ||
* @param int $projectMessageId | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function store(Request $request, int $companyId, int $projectId, int $projectMessageId): JsonResponse | ||
{ | ||
$loggedEmployee = InstanceHelper::getLoggedEmployee(); | ||
$loggedCompany = InstanceHelper::getLoggedCompany(); | ||
|
||
$data = [ | ||
'company_id' => $loggedCompany->id, | ||
'author_id' => $loggedEmployee->id, | ||
'project_id' => $projectId, | ||
'project_message_id' => $projectMessageId, | ||
'content' => $request->input('comment'), | ||
]; | ||
|
||
$comment = (new CreateProjectMessageComment)->execute($data); | ||
|
||
return response()->json([ | ||
'data' => [ | ||
'id' => $comment->id, | ||
'content' => StringHelper::parse($comment->content), | ||
'content_raw' => $comment->content, | ||
'written_at' => DateHelper::formatShortDateWithTime($comment->created_at), | ||
'author' => [ | ||
'id' => $loggedEmployee->id, | ||
'name' => $loggedEmployee->name, | ||
'avatar' => ImageHelper::getAvatar($loggedEmployee, 32), | ||
'url' => route('employees.show', [ | ||
'company' => $loggedCompany, | ||
'employee' => $loggedEmployee, | ||
]), | ||
], | ||
'can_edit' => true, | ||
'can_delete' => true, | ||
], | ||
], 201); | ||
} | ||
|
||
/** | ||
* Edit a comment. | ||
* | ||
* @param Request $request | ||
* @param int $companyId | ||
* @param int $projectId | ||
* @param int $projectMessageId | ||
* @param int $commentId | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function update(Request $request, int $companyId, int $projectId, int $projectMessageId, int $commentId): JsonResponse | ||
{ | ||
$loggedEmployee = InstanceHelper::getLoggedEmployee(); | ||
$loggedCompany = InstanceHelper::getLoggedCompany(); | ||
|
||
$data = [ | ||
'company_id' => $loggedCompany->id, | ||
'author_id' => $loggedEmployee->id, | ||
'project_id' => $projectId, | ||
'project_message_id' => $projectMessageId, | ||
'comment_id' => $commentId, | ||
'content' => $request->input('commentEdit'), | ||
]; | ||
|
||
$comment = (new UpdateProjectMessageComment)->execute($data); | ||
|
||
return response()->json([ | ||
'data' => [ | ||
'id' => $comment->id, | ||
'content' => StringHelper::parse($comment->content), | ||
'content_raw' => $comment->content, | ||
'written_at' => DateHelper::formatShortDateWithTime($comment->created_at), | ||
'author' => [ | ||
'id' => $loggedEmployee->id, | ||
'name' => $loggedEmployee->name, | ||
'avatar' => ImageHelper::getAvatar($loggedEmployee, 32), | ||
'url' => route('employees.show', [ | ||
'company' => $loggedCompany, | ||
'employee' => $loggedEmployee, | ||
]), | ||
], | ||
'can_edit' => true, | ||
'can_delete' => true, | ||
], | ||
], 200); | ||
} | ||
|
||
/** | ||
* Destroy the message. | ||
* | ||
* @param Request $request | ||
* @param int $companyId | ||
* @param int $projectId | ||
* @param int $projectMessageId | ||
* @param int $commentId | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function destroy(Request $request, int $companyId, int $projectId, int $projectMessageId, int $commentId): JsonResponse | ||
{ | ||
$loggedEmployee = InstanceHelper::getLoggedEmployee(); | ||
$company = InstanceHelper::getLoggedCompany(); | ||
|
||
$data = [ | ||
'company_id' => $company->id, | ||
'author_id' => $loggedEmployee->id, | ||
'project_id' => $projectId, | ||
'project_message_id' => $projectMessageId, | ||
'comment_id' => $commentId, | ||
]; | ||
|
||
(new DestroyProjectMessageComment)->execute($data); | ||
|
||
return response()->json([ | ||
'data' => true, | ||
], 200); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace App\Models\Company; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\MorphTo; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
|
||
class Comment extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $table = 'comments'; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'company_id', | ||
'author_id', | ||
'author_name', | ||
'content', | ||
'commentable_id', | ||
'commentable_type', | ||
]; | ||
|
||
/** | ||
* Get the Company record associated with the comment object. | ||
* | ||
* @return BelongsTo | ||
*/ | ||
public function company() | ||
{ | ||
return $this->belongsTo(Company::class); | ||
} | ||
|
||
/** | ||
* Get the parent commentable model. | ||
* | ||
* @return MorphTo | ||
*/ | ||
public function commentable() | ||
{ | ||
return $this->morphTo(); | ||
} | ||
|
||
/** | ||
* Get the employee record associated with the comment object. | ||
* | ||
* @return BelongsTo | ||
*/ | ||
public function author() | ||
{ | ||
return $this->belongsTo(Employee::class, 'author_id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.