feat: logging

This commit is contained in:
llama committed 2023-10-10 20:49:51 +11:00
1 parent b3784b77b1
commit faaed8fa1b
12 files changed
+169 -24

No files matched your search

+11 -14
View File
@@ -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]);
}
}
+17 -2
View File
@@ -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
View 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',
];
}
+28 -2
View File
@@ -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.'
]);
});
}
}
+11 -2
View File
@@ -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();
}
+27
View File
@@ -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.'
]);
});
}
}