source: app/Http/Controllers/OnboardingController.php

Last change on this file was dfae77e, checked in by Igor Danilovski <igor_danilovski@…>, 2 years ago
  • Initial commit;
  • Property mode set to 100644
File size: 4.5 KB
Line 
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Enum\UserType;
6use App\Models\Artist;
7use App\Models\ArtistType;
8use App\Models\Genre;
9use App\Models\Organizer;
10use App\Providers\RouteServiceProvider;
11use Illuminate\Contracts\Foundation\Application;
12use Illuminate\Contracts\View\Factory;
13use Illuminate\Contracts\View\View;
14use Illuminate\Http\RedirectResponse;
15use Illuminate\Http\Request;
16use Illuminate\Routing\Redirector;
17use Illuminate\Support\Facades\Auth;
18use Exception;
19
20class OnboardingController extends Controller
21{
22 public function __construct()
23 {
24 $this->middleware(['auth', 'verified']);
25 }
26
27 /**
28 * @return Application|Factory|View|RedirectResponse|Redirector|\never
29 */
30 public function index()
31 {
32 if (!empty(Auth::user()->type)) {
33 switch (Auth::user()->type) {
34 case UserType::ARTIST->value:
35 $artist = Artist::where('user_id', Auth::id())->firstOrFail();
36
37 if ($artist->isOnboardingCompleted()) {
38 return redirect(RouteServiceProvider::ARTIST_HOME);
39 }
40 return view('onboarding.artist.index')
41 ->with('artist', $artist)
42 ->with('types', ArtistType::all())
43 ->with('genres', Genre::all());
44
45 case UserType::ORGANIZER->value:
46 $organizer = Organizer::where('user_id', Auth::id())->firstOrFail();
47
48 if ($organizer->isOnboardingCompleted()) {
49 return redirect(RouteServiceProvider::ARTIST_HOME);
50 }
51 return view('onboarding.organizer.index')
52 ->with('organizer', $organizer);
53 }
54 }
55
56 return abort(404);
57 }
58
59 /**
60 * @param Request $request
61 * @return Application|RedirectResponse|Redirector|\never
62 */
63 public function create(Request $request)
64 {
65 if (!empty(Auth::user()->type)) {
66 switch (Auth::user()->type) {
67 case UserType::ARTIST->value:
68 $artist = Artist::where('user_id', Auth::id())->firstOrFail();
69
70 if ($request->has('birth_date')) {
71 $artist->birth_date = $request->input('birth_date');
72 }
73 if ($request->has('city')) {
74 $artist->city = $request->input('city');
75 }
76 if ($request->has('country')) {
77 $artist->country = $request->input('country');
78 }
79 if ($request->has('short_description')) {
80 $artist->short_description = $request->input('short_description');
81 }
82 if ($request->has('artist_type')) {
83 $type = ArtistType::where('name', $request->input('artist_type'))->firstOrFail();
84 $artist->artist_type()->associate($type);
85 }
86 if ($request->has('genres')) {
87 foreach ($request->input('genres') as $genre) {
88 Genre::findOrFail($genre);
89 }
90 $artist->genres()->attach($request->input('genres.*'));
91 }
92
93 try {
94 $artist->save();
95 } catch (Exception $e) {
96 return redirect()->back()
97 ->with('error', $e->getMessage());
98 }
99
100 $artist->user()->associate(Auth::user());
101
102 return redirect(RouteServiceProvider::ARTIST_HOME);
103 case UserType::ORGANIZER->value:
104 $organizer = Organizer::where('user_id', Auth::id())->firstOrFail();
105
106 if ($request->has('city')) {
107 $organizer->city = $request->input('city');
108 }
109 if ($request->has('country')) {
110 $organizer->country = $request->input('country');
111 }
112
113 $organizer->user()->associate(Auth::user());
114
115 try {
116 $organizer->save();
117 } catch (Exception $e) {
118 return redirect()->back()
119 ->with('error', $e->getMessage());
120 }
121
122 return redirect(RouteServiceProvider::ORGANIZER_HOME);
123 }
124 }
125
126 return abort(404);
127 }
128}
Note: See TracBrowser for help on using the repository browser.