feat: logging
This commit is contained in:
parent
b3784b77b1
commit
ceb4f36ba1
@ -3,36 +3,33 @@
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Requests\StoreUserRequest;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Throwable;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function registerUser(StoreUserRequest $request)
|
||||
{
|
||||
$data = $request->validated();
|
||||
$data['password'] = Str::random(8);
|
||||
|
||||
$validated = $request->validated();
|
||||
$validated['is_admin'] = false;
|
||||
$validated['password'] = Str::random(8);
|
||||
$validated['status'] = true;
|
||||
try {
|
||||
$user = User::create($validated);
|
||||
}catch (Throwable $exception){
|
||||
$user = User::create($data);
|
||||
} catch (Throwable $exception) {
|
||||
return response()
|
||||
->json($exception);
|
||||
->json($exception);
|
||||
}
|
||||
|
||||
|
||||
return response()
|
||||
->json(['message' => 'Successfully created user ' . $user->firstname]);
|
||||
->json(['message' => 'Successfully created user ' . $user->firstname]);
|
||||
}
|
||||
|
||||
public function getUser(Request $request, User $user)
|
||||
{
|
||||
return response()
|
||||
->json(["status" => true]);
|
||||
->json(['status' => true]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Log;
|
||||
use App\Models\Quote;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
@ -12,6 +13,7 @@ class WebHookController extends Controller
|
||||
{
|
||||
public function webHookSend(string $payload)
|
||||
{
|
||||
// TODO: move this to a helper class so we can reuse code between API and FE
|
||||
$data = ['text' => $payload];
|
||||
$response = Http::post(config('bot.webhook'), $data);
|
||||
}
|
||||
@ -19,17 +21,30 @@ class WebHookController extends Controller
|
||||
public function sendQuote(Request $request)
|
||||
{
|
||||
$quote = $request->input('quote');
|
||||
|
||||
if (empty($quote)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'content' => "Quote sent. {$quote}",
|
||||
]);
|
||||
|
||||
$this->webHookSend($quote);
|
||||
}
|
||||
|
||||
public function sendRandomQuote()
|
||||
{
|
||||
$quote = Quote::inRandomOrder()->first()->quote;
|
||||
$this->webHookSend($quote);
|
||||
$quote = Quote::inRandomOrder()->first();
|
||||
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'quote_id' => $quote->id,
|
||||
'content' => 'Quote sent.',
|
||||
]);
|
||||
|
||||
$this->webHookSend($quote->quote);
|
||||
}
|
||||
|
||||
public function test()
|
||||
|
||||
18
app/Models/Log.php
Normal file
18
app/Models/Log.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @mixin IdeHelperLog
|
||||
*/
|
||||
class Log extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'quote_id',
|
||||
'requested_quote_id',
|
||||
'content',
|
||||
];
|
||||
}
|
||||
@ -14,11 +14,37 @@ class Quote extends Model
|
||||
|
||||
public function request(): void
|
||||
{
|
||||
// Send the notification
|
||||
// Send the quote
|
||||
// If success, add it to the transactions
|
||||
}
|
||||
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'quote_id' => $this->id,
|
||||
'content' => 'Quote requested.'
|
||||
]);
|
||||
}
|
||||
protected $fillable = [
|
||||
'quote',
|
||||
];
|
||||
|
||||
public static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
self::created(function ($model) {
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'quote_id' => $model->id,
|
||||
'content' => 'Quote created.'
|
||||
]);
|
||||
});
|
||||
|
||||
self::deleted(function ($model) {
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'quote_id' => $model->id,
|
||||
'content' => 'Quote deleted.'
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,7 +19,12 @@ class RequestedQuote extends Model
|
||||
|
||||
public function approve(): void
|
||||
{
|
||||
// TODO: we'll probs want to log who approved this quote
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'requested_quote_id' => $this->id,
|
||||
'content' => 'Quote approved.'
|
||||
]);
|
||||
|
||||
Quote::create([
|
||||
'quote' => $this->quote,
|
||||
]);
|
||||
@ -29,7 +34,11 @@ class RequestedQuote extends Model
|
||||
|
||||
public function reject(): void
|
||||
{
|
||||
// TODO: we'll probs want to log who reject this quote
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'requested_quote_id' => $this->id,
|
||||
'content' => 'Quote rejected.'
|
||||
]);
|
||||
|
||||
$this->delete();
|
||||
}
|
||||
|
||||
@ -14,6 +14,16 @@ class User extends Authenticatable
|
||||
{
|
||||
use HasApiTokens, HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The model's default values for attributes.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $attributes = [
|
||||
'is_admin' => false,
|
||||
'status' => true,
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
@ -46,6 +56,23 @@ class User extends Authenticatable
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $casts = [
|
||||
'is_admin' => 'boolean',
|
||||
'status' => 'boolean',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
|
||||
public static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
// These will only ever get created by the api/bot.
|
||||
// If we want to add something like by whom later on,
|
||||
// we can just append it to the content e.g auth()?->user()?->id ?? 'API'.
|
||||
self::created(function ($model) {
|
||||
Log::create([
|
||||
'user_id' => $model->id,
|
||||
'content' => 'User created.'
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
28
database/migrations/2023_10_10_200426_create_logs_table.php
Normal file
28
database/migrations/2023_10_10_200426_create_logs_table.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Quote;
|
||||
use App\Models\RequestedQuote;
|
||||
use App\Models\User;
|
||||
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('logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignIdFor(User::class)->nullable();
|
||||
$table->foreignIdFor(Quote::class)->nullable();
|
||||
$table->foreignIdFor(RequestedQuote::class)->nullable();
|
||||
$table->string('content');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('logs');
|
||||
}
|
||||
};
|
||||
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Log;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Attributes\Rule;
|
||||
@ -14,13 +15,18 @@ new #[Layout('layouts.app')] class extends Component
|
||||
{
|
||||
$validated = $this->validate();
|
||||
|
||||
// TODO: move this to a helper class so we can reuse code between API and FE
|
||||
$response = Http::post(config('bot.webhook'), ['text' => $validated['quote']]);
|
||||
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'content' => "Quote sent. {$validated['quote']}"
|
||||
]);
|
||||
|
||||
$this->quote = '';
|
||||
}
|
||||
}; ?>
|
||||
|
||||
|
||||
<div class="px-4 sm:px-6 lg:px-8">
|
||||
<form wire:submit="sendQuote">
|
||||
<x-text-input
|
||||
@ -32,6 +38,8 @@ new #[Layout('layouts.app')] class extends Component
|
||||
<x-input-error :messages="$errors->get('quote')" class="mt-2"/>
|
||||
<x-primary-button wire:loading.attr="disabled"
|
||||
wire:loading.class="opacity-50"
|
||||
class="mt-4">{{ __('Send') }}</x-primary-button>
|
||||
class="mt-4">
|
||||
{{ __('Send') }}
|
||||
</x-primary-button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Log;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Auth\Events\Lockout;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
@ -39,6 +40,11 @@ new #[Layout('layouts.guest')] class extends Component
|
||||
|
||||
session()->regenerate();
|
||||
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'content' => "Quote sent. {$quote}"
|
||||
]);
|
||||
|
||||
$this->redirect(
|
||||
session('url.intended', RouteServiceProvider::HOME),
|
||||
navigate: true
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Log;
|
||||
use Illuminate\Auth\Events\PasswordReset;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Password;
|
||||
@ -58,6 +59,11 @@ new #[Layout('layouts.guest')] class extends Component
|
||||
|
||||
session()->flash('status', __($status));
|
||||
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'content' => "Quote sent. {$quote}"
|
||||
]);
|
||||
|
||||
$this->redirectRoute('login', navigate: true);
|
||||
}
|
||||
}; ?>
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Log;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rules\Password;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
@ -28,6 +29,11 @@ new class extends Component
|
||||
'password' => Hash::make($validated['password']),
|
||||
]);
|
||||
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'content' => "Quote sent. {$quote}"
|
||||
]);
|
||||
|
||||
$this->reset('current_password', 'password', 'password_confirmation');
|
||||
|
||||
$this->dispatch('password-updated');
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\Api\WebHookController;
|
||||
use App\Http\Controllers\Api\UserController;
|
||||
use App\Http\Controllers\Api\WebHookController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user