-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #440 from range-of-motion/404-create-prototype-for…
…-spa Create prototype for SPA
- Loading branch information
Showing
26 changed files
with
679 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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,61 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\ApiKey; | ||
use App\Models\LoginAttempt; | ||
use App\Models\User; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Str; | ||
|
||
class LogInController extends Controller | ||
{ | ||
public function __invoke(Request $request): JsonResponse | ||
{ | ||
$request->validate([ | ||
'email' => ['required'], | ||
'password' => ['required'], | ||
]); | ||
|
||
if ( | ||
Auth::attempt([ | ||
'email' => $request->input('email'), | ||
'password' => $request->input('password'), | ||
]) | ||
) { | ||
$userId = Auth::user()->id; | ||
|
||
LoginAttempt::create([ | ||
'user_id' => $userId, | ||
'ip' => $request->ip(), | ||
'failed' => false, | ||
]); | ||
|
||
$apiKey = ApiKey::create([ | ||
'user_id' => $userId, | ||
'token' => Str::random(32), | ||
]); | ||
|
||
return response() | ||
->json([ | ||
'token' => $apiKey->token, | ||
]); | ||
} else { | ||
$userByEmail = User::query() | ||
->where('email', $request->input('email')) | ||
->first(); | ||
|
||
LoginAttempt::create([ | ||
'user_id' => $userByEmail ? $userByEmail->id : null, | ||
'ip' => $request->ip(), | ||
'failed' => true, | ||
]); | ||
|
||
return response() | ||
->json(['error' => 'UNABLE_TO_LOG_IN']); | ||
} | ||
} | ||
} |
This file contains 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,67 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Resources\TransactionResource; | ||
use App\Models\Earning; | ||
use App\Models\Spending; | ||
use Illuminate\Http\Request; | ||
|
||
class TransactionController extends Controller | ||
{ | ||
public function index(Request $request) | ||
{ | ||
/** @var ApiKey $apiKey */ | ||
$apiKey = $request->get('apiKey'); | ||
|
||
$transactions = collect(); | ||
|
||
foreach (Earning::query()->where('space_id', $apiKey->user->spaces()->first()->id)->get() as $earning) { | ||
$transactions->push($earning); | ||
} | ||
|
||
foreach (Spending::query()->where('space_id', $apiKey->user->spaces()->first()->id)->get() as $spending) { | ||
$transactions->push($spending); | ||
} | ||
|
||
return TransactionResource::collection($transactions); | ||
} | ||
|
||
public function store(Request $request) | ||
{ | ||
/** @var ApiKey $apiKey */ | ||
$apiKey = $request->get('apiKey'); | ||
|
||
$spaceId = $apiKey->user->spaces()->first()->id; | ||
|
||
$request->validate([ | ||
'type' => 'in:earning,spending', | ||
// TODO | ||
]); | ||
|
||
if ($request->input('type') === 'earning') { | ||
Earning::create([ | ||
'space_id' => $spaceId, | ||
'recurring_id' => null, // TODO | ||
'happened_on' => $request->input('happened_on'), | ||
'description' => $request->input('description'), | ||
'amount' => $request->input('amount') | ||
]); | ||
} | ||
|
||
if ($request->input('type') === 'spending') { | ||
Spending::create([ | ||
'space_id' => $spaceId, | ||
'import_id' => null, // TODO | ||
'recurring_id' => null, // TODO | ||
'tag_id' => null, // TODO | ||
'happened_on' => $request->input('happened_on'), | ||
'description' => $request->input('description'), | ||
'amount' => $request->input('amount') | ||
]); | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file contains 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,11 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
class PrototypeController extends Controller | ||
{ | ||
public function __invoke() | ||
{ | ||
return view('prototype'); | ||
} | ||
} |
This file contains 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 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,19 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class CheckIfSpaPrototypeIsEnabled | ||
{ | ||
public function handle(Request $request, Closure $next): Response | ||
{ | ||
if (!config('app.spa_prototype_enabled')) { | ||
abort(404); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains 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,26 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use App\Models\ApiKey; | ||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class ResolveApiKey | ||
{ | ||
public function handle(Request $request, Closure $next): Response | ||
{ | ||
$apiKey = ApiKey::query() | ||
->where('token', $request->header('api-key')) | ||
->first(); | ||
|
||
if (!$apiKey) { | ||
abort(401); | ||
} | ||
|
||
$request->attributes->add(['apiKey' => $apiKey]); | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains 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,23 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use App\Models\Earning; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class TransactionResource extends JsonResource | ||
{ | ||
public function toArray(Request $request): array | ||
{ | ||
$isEarning = get_class($this->resource) === Earning::class; | ||
|
||
return [ | ||
'id' => $this->id, | ||
'type' => $isEarning ? 'earning' : 'spending', | ||
'happened_on' => $this->happened_on, | ||
'description' => $this->description, | ||
'amount' => $this->amount, | ||
]; | ||
} | ||
} |
This file contains 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,23 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
class ApiKey extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $guarded = []; | ||
|
||
/** | ||
* Relationships | ||
*/ | ||
|
||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
} |
This file contains 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 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 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,16 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Illuminate\Support\Str; | ||
|
||
class ApiKeyFactory extends Factory | ||
{ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'token' => Str::random(32), | ||
]; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
database/migrations/2023_11_05_012524_create_api_keys_table.php
This file contains 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,23 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::create('api_keys', function (Blueprint $table) { | ||
$table->id(); | ||
$table->foreignId('user_id')->constrained(); | ||
$table->string('token')->unique(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists('api_keys'); | ||
} | ||
}; |
Oops, something went wrong.