1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6declare(strict_types=1);
7
8namespace App\Http\Controllers;
9
10use App\Models\Team;
11use App\Transformers\UserCompactTransformer;
12use Symfony\Component\HttpFoundation\Response;
13
14class TeamsController extends Controller
15{
16 public function __construct()
17 {
18 parent::__construct();
19 $this->middleware('auth', ['only' => ['part']]);
20 }
21
22 public function destroy(string $id): Response
23 {
24 $team = Team::findOrFail($id);
25 priv_check('TeamUpdate', $team)->ensureCan();
26
27 $team->delete();
28 \Session::flash('popup', osu_trans('teams.destroy.ok'));
29
30 return ujs_redirect(route('home'));
31 }
32
33 public function edit(string $id): Response
34 {
35 $team = Team::findOrFail($id);
36 priv_check('TeamUpdate', $team)->ensureCan();
37
38 return ext_view('teams.edit', compact('team'));
39 }
40
41 public function part(string $id): Response
42 {
43 $team = Team::findOrFail($id);
44 priv_check('TeamPart', $team)->ensureCan();
45
46 $team->members()->findOrFail(\Auth::user()->getKey())->delete();
47 \Session::flash('popup', osu_trans('teams.part.ok'));
48
49 return ujs_redirect(route('teams.show', ['team' => $team]));
50 }
51
52 public function show(string $id): Response
53 {
54 $team = Team
55 ::with(array_map(
56 fn (string $preload): string => "members.user.{$preload}",
57 UserCompactTransformer::CARD_INCLUDES_PRELOAD,
58 ))->findOrFail($id);
59
60 return ext_view('teams.show', compact('team'));
61 }
62
63 public function update(string $id): Response
64 {
65 $team = Team::findOrFail($id);
66 priv_check('TeamUpdate', $team)->ensureCan();
67 $params = get_params(\Request::all(), 'team', [
68 'default_ruleset_id:int',
69 'description',
70 'header:file',
71 'header_remove:bool',
72 'is_open:bool',
73 'logo:file',
74 'logo_remove:bool',
75 'url',
76 ]);
77
78 $team->fill($params)->saveOrExplode();
79
80 \Session::flash('popup', osu_trans('teams.edit.saved'));
81
82 return response(null, 201);
83 }
84}