X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FHttp%2FControllers%2FTechniqueController.php;h=59559a94a1883edb46c50c7f71e33cd33f87b072;hb=7c6716036321ba09846785720e81459aad55a323;hp=97a45f047a9bb6d5635480111afd9206690a4102;hpb=68aabaf6da8ed6e675bdea728702d5bd75066964;p=alttp.git diff --git a/app/Http/Controllers/TechniqueController.php b/app/Http/Controllers/TechniqueController.php index 97a45f0..59559a9 100644 --- a/app/Http/Controllers/TechniqueController.php +++ b/app/Http/Controllers/TechniqueController.php @@ -3,14 +3,139 @@ namespace App\Http\Controllers; use App\Models\Technique; +use App\Models\TechniqueMap; +use App\Models\TechniqueTranslation; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; class TechniqueController extends Controller { + public function byType(Request $request, $type) { + $techs = Technique::where('index', true)->where('type', '=', $type); + $this->applyFilter($request, $techs); + return $techs->get()->toJson(); + } + + public function byTypeAndName(Request $request, $type, $name) { + $tech = Technique::where('type', '=', $type)->where('name', '=', $name)->firstOrFail(); + $this->authorize('view', $tech); + $tech->load(['chapters', 'relations']); + return $tech->toJson(); + } + + public function forMap($map) { + $techs = TechniqueMap::with(['technique', 'technique.relations'])->where('map', '=', $map); + + return $techs->get()->toJson(); + } + + public function search(Request $request) { + $techs = Technique::where('index', '=', 1); + $this->applyFilter($request, $techs); + return $techs->get()->toJson(); + } + public function single(Request $request, Technique $tech) { $this->authorize('view', $tech); + $tech->load(['chapters', 'relations']); return $tech->toJson(); } + public function update(Request $request, Technique $content) { + $this->authorize('update', $content); + + $validatedData = $request->validate([ + 'attribution' => 'string', + 'description' => 'string', + 'language' => 'string|in:de,en', + 'parent_id' => 'integer|exists:App\\Models\\Technique,id', + 'short' => 'string', + 'title' => 'string', + ]); + + if ($validatedData['language'] == 'en') { + $this->applyLocalizedValues($validatedData, $content); + $content->save(); + } else { + $translation = $this->getTranslation($content, $validatedData['language']); + $this->applyLocalizedValues($validatedData, $translation); + $translation->save(); + } + + $result = isset($validatedData['parent_id']) ? Technique::findOrFail($validatedData['parent_id']) : $content->fresh(); + $result->load(['chapters', 'relations']); + return $result->toJson(); + } + + private function applyLocalizedValues($validatedData, $content) { + foreach (['attribution', 'description', 'short', 'title'] as $name) { + if (isset($validatedData[$name])) { + $content->{$name} = $validatedData[$name]; + } + } + } + + private function getTranslation(Technique $content, $language) { + foreach ($content->translations as $translation) { + if ($translation->locale == $language) { + return $translation; + } + } + $translation = new TechniqueTranslation(); + $translation->technique_id = $content->id; + $translation->locale = $language; + return $translation; + } + + protected function applyFilter(Request $request, Builder $query) { + $validatedData = $request->validate([ + 'phrase' => 'string|nullable', + 'ruleset' => 'array|nullable', + 'ruleset.competitive' => 'boolean', + 'ruleset.mg' => 'boolean', + 'ruleset.nl' => 'boolean', + 'ruleset.owg' => 'boolean', + 'type' => 'string|nullable', + ]); + + if (!empty($validatedData['type'])) { + $query->where('type', '=', $validatedData['type']); + } + + if (!empty($validatedData['phrase'])) { + $search = $validatedData['phrase']; + $query->where(function (Builder $query) use ($search) { + $query->where('title', 'LIKE', '%'.$search.'%') + ->orWhere('short', 'LIKE', '%'.$search.'%'); + }); + } + + if (isset($validatedData['ruleset'])) { + $com = isset($validatedData['ruleset']['competitive']) && $validatedData['ruleset']['competitive']; + $owg = isset($validatedData['ruleset']['owg']) && $validatedData['ruleset']['owg']; + $mg = isset($validatedData['ruleset']['mg']) && $validatedData['ruleset']['mg']; + $nl = isset($validatedData['ruleset']['nl']) && $validatedData['ruleset']['nl']; + $any = $com || $owg || $mg || $nl; + $all = $com && $owg && $mg && $nl; + if ($any && !$all) { + $query->where(function(Builder $query) use ($com, $owg, $mg, $nl) { + $query->whereNull('rulesets'); + if ($com) { + $query->orWhere('rulesets->competitive', '=', true); + } + if ($owg) { + $query->orWhere('rulesets->owg', '=', true); + } + if ($mg) { + $query->orWhere('rulesets->mg', '=', true); + } + if ($nl) { + $query->orWhere('rulesets->nl', '=', true); + } + }); + } + } + } + }