Skip to content

Commit

Permalink
small changes to problem create-edit functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
papandrk committed Nov 11, 2024
1 parent e26e5d8 commit 5efaff8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
use App\Models\CrowdSourcingProject\Problem\CrowdSourcingProjectProblemTranslation;
use App\Repository\CrowdSourcingProject\Problem\CrowdSourcingProjectProblemRepository;
use App\Repository\LanguageRepository;
use App\Utils\FileUploader;
use App\ViewModels\CrowdSourcingProject\Problem\CreateEditProblem;
use App\ViewModels\CrowdSourcingProject\Problem\CrowdSourcingProjectProblemsLandingPage;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class CrowdSourcingProjectProblemManager {
protected CrowdSourcingProjectProblemRepository $crowdSourcingProjectProblemRepository;
Expand Down Expand Up @@ -74,4 +77,29 @@ public function getCreateEditProblemViewModel(?int $id = null): CreateEditProble
$projects
);
}

public function storeProblem(array $attributes): int {
if (isset($attributes['problem-image']) && $attributes['problem-image']->isValid()) {
$imgPath = FileUploader::uploadAndGetPath($attributes['problem-image'], 'problem_image');
}

$crowdSourcingProjectProblem = CrowdSourcingProjectProblem::create([
'project_id' => $attributes['problem-owner-project'],
'user_creator_id' => Auth::id(),
'slug' => Str::random(16), // temporary - will be changed after record creation
'status_id' => $attributes['problem-status'],
'img_url' => $imgPath ?? null,
'default_language_id' => $attributes['problem-default-language'], // bookmark2 - default or generally another translation language?
]);

$crowdSourcingProjectProblem->slug = Str::slug($attributes['problem-title'] . '-' . $crowdSourcingProjectProblem->id);
$crowdSourcingProjectProblem->save();

$crowdSourcingProjectProblemTranslation = $crowdSourcingProjectProblem->defaultTranslation()->create([ // bookmark2 - default or regular translation?
'title' => $attributes['problem-title'],
'description' => $attributes['problem-description'],
]);

return $crowdSourcingProjectProblem->id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
use App\BusinessLogicLayer\CrowdSourcingProject\Problem\CrowdSourcingProjectProblemManager;
use App\Http\Controllers\Controller;
use App\Models\CrowdSourcingProject\Problem\CrowdSourcingProjectProblem;
use App\Utils\FileUploader;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;

Expand Down Expand Up @@ -61,44 +59,27 @@ public function create(): View {
/**
* Store a newly created resource in storage.
*/
public function store(Request $request) {
$validated = $request->validate([ // bookmark2
public function store(Request $request): RedirectResponse {
$this->validate($request, [ // bookmark2
'problem-title' => ['required', 'string', 'max:100'],
'problem-description' => ['required', 'string', 'max:400'],
'problem-status' => ['required'], // bookmark2
'problem-default-language' => ['required'], // bookmark2
'problem-slug' => ['required', 'unique:crowd_sourcing_project_problems,slug'],
'problem-image' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
'problem-owner-project' => ['required'],
]);

try {
if (isset($request['problem-image']) && $request['problem-image']->isValid()) {
$imgPath = FileUploader::uploadAndGetPath($request['problem-image'], 'problem_image');
}

$crowdSourcingProjectProblem = CrowdSourcingProjectProblem::create([
'project_id' => $request->input('problem-owner-project'),
'user_creator_id' => Auth::user()->id,
'slug' => Str::slug($request->input('problem-title')),
'status_id' => $request->input('problem-status'),
'img_url' => $imgPath,
'default_language_id' => $request->input('problem-default-language'), // bookmark2 - default or generally another translation language?
]);

$crowdSourcingProjectProblemTranslation = $crowdSourcingProjectProblem->defaultTranslation()->create([ // bookmark2 - default or regular translation?
'title' => $request->input('problem-title'),
'description' => $request->input('problem-description'),
]);

session()->flash('flash_message_success', 'Problem Created Successfully.');

return redirect()->route('problems.index');
$createdProblemId = $this->crowdSourcingProjectProblemManager->storeProblem($request->all());
} catch (\Exception $e) {
session()->flash('flash_message_error', 'Error: ' . $e->getCode() . ' ' . $e->getMessage());

return back()->withInput();
}

session()->flash('flash_message_success', 'Problem Created Successfully.');

return redirect()->route('problems.edit', ['problem' => $createdProblemId]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@if($viewModel->project->external_url)
<div class="col-md-5 col-sm-12 mx-auto mt-5">
<div class="col-md-5 col-sm-12 mx-auto text-center mt-5">
<a href="{{$viewModel->project->external_url}}" target="_blank" class="btn call-to-action">
{{ __("questionnaire.visit_projects_site") }}
</a>
Expand Down

0 comments on commit 5efaff8

Please sign in to comment.