-
Notifications
You must be signed in to change notification settings - Fork 140
Create comand #1799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
GreedVal
wants to merge
29
commits into
Hexlet:main
Choose a base branch
from
GreedVal:dump-database
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Create comand #1799
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
0d45ae9
Create comand
GreedVal 377cb7a
Merge branch 'main' into dump-database
GreedVal 20bf344
Merge branch 'main' into dump-database
GreedVal e74bd64
Create class services
GreedVal d2127a0
fix spase
GreedVal 92417f8
Fix select
GreedVal c438721
Merge branch 'main' into dump-database
GreedVal 2edff8e
Make test
GreedVal 031f705
Fix test create factory
GreedVal 98ed224
Updated dependencies
GreedVal 2894ccf
Fix factory
GreedVal f7fd033
fix export services
GreedVal 713fb1e
Fix test
GreedVal 1b03f18
Fix test
GreedVal 1d1e580
Merge branch 'main' into dump-database
GreedVal 8d79468
Merge branch 'main' into dump-database
GreedVal 5c4cbb4
fix compos
GreedVal 176abf4
fix
GreedVal 6fb40f7
Make export in admin panel
GreedVal 86a8a7b
Merge branch 'main' into dump-database
GreedVal ffbe573
Fix name in test csv file
GreedVal 3ce546f
fix
GreedVal a00a2b2
fix
GreedVal 2d42092
path formation fix
GreedVal bafb7a7
Creating a CSV file using a library
GreedVal 10da35d
Routes used resource have been corrected
GreedVal f9f7710
The controller and service have been replaced.
GreedVal 5931845
Fix test
GreedVal c3ca73f
Fix test
GreedVal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,33 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\Controllers\Admin; | ||
|
|
||
| use App\Services\AnalyticsExporter; | ||
| use Illuminate\Http\Request; | ||
| use Illuminate\Support\Facades\Storage; | ||
| use Illuminate\View\View; | ||
| use Symfony\Component\HttpFoundation\BinaryFileResponse; | ||
|
|
||
| class ExportController extends AdminController | ||
| { | ||
| public function __construct( | ||
| private readonly AnalyticsExporter $exporter | ||
| ) { | ||
| } | ||
|
|
||
| public function index(Request $request): View | ||
| { | ||
| return view('admin.export'); | ||
| } | ||
|
|
||
| public function store(Request $request): BinaryFileResponse | ||
| { | ||
| $request->validate([ | ||
| 'type' => 'required|string', | ||
| ]); | ||
|
|
||
| $filePath = $this->exporter->export($request->type); | ||
|
|
||
| return response()->download($filePath)->deleteFileAfterSend(); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,105 @@ | ||
| <?php | ||
|
|
||
| namespace App\Services; | ||
|
|
||
| use App\Models\Chapter; | ||
| use App\Models\Comment; | ||
| use App\Models\Exercise; | ||
| use App\Models\Solution; | ||
| use App\Models\User; | ||
| use Illuminate\Database\Eloquent\Collection; | ||
| use Illuminate\Support\Facades\Storage; | ||
| use League\Csv\Writer; | ||
| use Spatie\Activitylog\Models\Activity; | ||
|
|
||
| class AnalyticsExporter | ||
| { | ||
| public function export(string $type): string | ||
| { | ||
| return match ($type) { | ||
| 'users' => $this->exportUsers("export/users.csv"), | ||
| 'chapters' => $this->exportChapters("export/chapters.csv"), | ||
| 'exercises' => $this->exportExercises("export/exercises.csv"), | ||
| 'solutions' => $this->exportSolutions("export/solutions.csv"), | ||
| 'comments' => $this->exportComments("export/comments.csv"), | ||
| 'activity' => $this->exportActivityLog("export/activity.csv"), | ||
| default => throw new \InvalidArgumentException("Unknown export type: {$type}"), | ||
| }; | ||
| } | ||
|
|
||
| private function exportUsers(string $path): string | ||
| { | ||
| $data = User::select(['id', 'name', 'email', 'email_verified_at', 'github_name', 'points', 'created_at', 'is_admin']) | ||
| ->get(); | ||
|
|
||
| return $this->writeCsv($data, $path); | ||
| } | ||
|
|
||
| private function exportChapters(string $path): string | ||
| { | ||
| $data = Chapter::select(['id', 'path', 'parent_id', 'created_at']) | ||
| ->get(); | ||
|
|
||
| return $this->writeCsv($data, $path); | ||
| } | ||
|
|
||
| private function exportExercises(string $path): string | ||
| { | ||
| $data = Exercise::select(['id', 'chapter_id', 'path', 'created_at']) | ||
| ->get(); | ||
|
|
||
| return $this->writeCsv($data, $path); | ||
| } | ||
|
|
||
| private function exportSolutions(string $path): string | ||
| { | ||
| $data = Solution::select(['id', 'exercise_id', 'user_id', 'created_at']) | ||
| ->get(); | ||
|
|
||
| return $this->writeCsv($data, $path); | ||
| } | ||
|
|
||
| private function exportComments(string $path): string | ||
| { | ||
| $data = Comment::select(['id', 'user_id', 'commentable_type', 'commentable_id', 'parent_id', 'created_at']) | ||
| ->get(); | ||
|
|
||
| return $this->writeCsv($data, $path); | ||
| } | ||
|
|
||
| private function exportActivityLog(string $path): string | ||
| { | ||
| $data = Activity::select(['id', 'log_name', 'subject_id', 'subject_type', 'causer_id', 'event', 'created_at']) | ||
| ->get(); | ||
|
|
||
| return $this->writeCsv($data, $path); | ||
| } | ||
|
|
||
| private function writeCsv(Collection $collection, string $path): string | ||
| { | ||
| $disk = Storage::disk(); | ||
| $directory = dirname($path); | ||
|
|
||
| if (!$disk->exists($directory)) { | ||
| $disk->makeDirectory($directory); | ||
| } | ||
|
|
||
| $csv = Writer::createFromPath( | ||
| $disk->path($path), | ||
| 'w+' | ||
| ); | ||
|
|
||
| $csv->setDelimiter(','); | ||
| $csv->setNewline("\n"); | ||
|
|
||
| if ($collection->isNotEmpty()) { | ||
| $csv->insertOne(array_keys($collection->first()->getAttributes())); | ||
|
|
||
| foreach ($collection as $item) { | ||
| $csv->insertOne(array_values($item->getAttributes())); | ||
| } | ||
| } | ||
|
|
||
| return $disk->path($path); | ||
| } | ||
| } | ||
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,21 @@ | ||
| <?php | ||
|
|
||
| namespace Database\Factories; | ||
|
|
||
| use App\Models\Chapter; | ||
| use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
|
||
| class ChapterFactory extends Factory | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Хм, а у нас ведь нет особо необходимости в генерации глав и упражнений. Они ведь есть в коде. Они не подходят? |
||
| { | ||
| protected $model = Chapter::class; | ||
|
|
||
| public function definition(): array | ||
| { | ||
| return [ | ||
| 'path' => $this->faker->word, | ||
| 'parent_id' => null, | ||
| 'created_at' => now(), | ||
| 'updated_at' => now(), | ||
| ]; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
тут все еще самописная работа с csv. Давай возьмем готовое решение, вот к примеру. https://github.com/thephpleague/csv