File size: 1,247 Bytes
b2dcf0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php

namespace App\Domain\Files;

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use stdClass;

class FileRepository
{
    public function create(array $attributes): string
    {
        $now = Carbon::now();

        DB::table('stored_files')->insert(array_merge($attributes, [
            'created_at' => $now,
            'updated_at' => $now,
        ]));

        return $attributes['id'];
    }

    public function find(string $id, bool $includeDeleted = false): ?stdClass
    {
        $query = DB::table('stored_files')->where('id', $id);

        if (! $includeDeleted) {
            $query->whereNull('deleted_at');
        }

        return $query->first();
    }

    public function archive(string $id): void
    {
        DB::table('stored_files')->where('id', $id)->whereNull('deleted_at')->update([
            'archived_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ]);
    }

    public function softDelete(string $id): void
    {
        DB::table('stored_files')->where('id', $id)->whereNull('deleted_at')->update([
            'deleted_from_storage_at' => Carbon::now(),
            'deleted_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ]);
    }
}