Two month of work
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\TimesheetEntry;
|
||||
use App\Models\Timesheet;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Support\ApiDataTransformer;
|
||||
|
||||
class TimesheetEntryController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$entries = TimesheetEntry::with(['timesheet', 'user'])->orderBy('date', 'asc')->get();
|
||||
|
||||
return $entries->map(function ($entry) {
|
||||
return ApiDataTransformer::snakeToCamel($entry->toArray());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all entries for a specific timesheet
|
||||
*/
|
||||
public function getEntriesForTimesheet(Timesheet $timesheet)
|
||||
{
|
||||
$entries = $timesheet->entries()->with('user')->orderBy('date', 'asc')->get();
|
||||
|
||||
return $entries->map(function ($entry) {
|
||||
return ApiDataTransformer::snakeToCamel($entry->toArray());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validatedData = $request->validate([
|
||||
'timesheetId' => 'required|exists:timesheets,id',
|
||||
'date' => 'required|date',
|
||||
'userId' => 'required|exists:users,id',
|
||||
'description' => 'nullable|string',
|
||||
'hours' => 'required|numeric',
|
||||
'billed' => 'sometimes|boolean',
|
||||
]);
|
||||
|
||||
// Convert camelCase to snake_case
|
||||
$snakeCaseData = ApiDataTransformer::camelToSnake($validatedData);
|
||||
|
||||
$entry = TimesheetEntry::create($snakeCaseData);
|
||||
$entry = TimesheetEntry::with(['timesheet', 'user'])->find($entry->id);
|
||||
return ApiDataTransformer::snakeToCamel($entry->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(TimesheetEntry $timesheetEntry)
|
||||
{
|
||||
$entry = $timesheetEntry->load(['timesheet', 'user']);
|
||||
return ApiDataTransformer::snakeToCamel($entry->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, TimesheetEntry $timesheetEntry)
|
||||
{
|
||||
$validatedData = $request->validate([
|
||||
'timesheetId' => 'sometimes|exists:timesheets,id',
|
||||
'date' => 'sometimes|date',
|
||||
'userId' => 'sometimes|exists:users,id',
|
||||
'description' => 'nullable|string',
|
||||
'hours' => 'sometimes|numeric',
|
||||
'billed' => 'sometimes|boolean',
|
||||
]);
|
||||
// Convert camelCase to snake_case
|
||||
$snakeCaseData = ApiDataTransformer::camelToSnake($validatedData);
|
||||
|
||||
$timesheetEntry->update($snakeCaseData);
|
||||
|
||||
return ApiDataTransformer::snakeToCamel($timesheetEntry->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(TimesheetEntry $timesheetEntry)
|
||||
{
|
||||
$timesheetEntry->delete();
|
||||
return response()->noContent();
|
||||
}
|
||||
|
||||
public function toggleBilled(TimesheetEntry $timesheetEntry)
|
||||
{
|
||||
$timesheetEntry = $timesheetEntry->update(['billed' => !$timesheetEntry->billed]);
|
||||
return ApiDataTransformer::snakeToCamel($timesheetEntry->toArray());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user