Files
sam-api/app/Services/ClientGroupService.php
hskwon b6f36cc967 feat: ClientGroup 및 Pricing API 완성 및 Swagger 문서 작성
- ClientGroupService 구현: 고객 그룹 관리 비즈니스 로직 (CRUD + toggle)
- ClientGroupController 구현: 6개 REST API 엔드포인트
- PricingController 구현: 5개 가격 관리 API 엔드포인트
- routes/api.php에 client-groups, pricing 라우트 등록
- ClientGroupApi.php Swagger 문서 작성 (OpenAPI 3.0)
- PricingApi.php Swagger 문서 작성 (OpenAPI 3.0)
- l5-swagger 재생성 완료

추가된 파일:
- app/Services/ClientGroupService.php
- app/Http/Controllers/Api/V1/ClientGroupController.php
- app/Http/Controllers/Api/V1/PricingController.php
- app/Swagger/v1/ClientGroupApi.php
- app/Swagger/v1/PricingApi.php

수정된 파일:
- routes/api.php (라우트 11개 추가)
- CURRENT_WORKS.md (작업 내용 문서화)

API 엔드포인트:
- GET/POST/PUT/DELETE /api/v1/client-groups
- GET/POST /api/v1/pricing (show, bulk, upsert 포함)
2025-10-13 22:06:42 +09:00

167 lines
4.8 KiB
PHP

<?php
namespace App\Services;
use App\Models\Orders\ClientGroup;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ClientGroupService extends Service
{
/** 목록(검색/페이징) */
public function index(array $params)
{
$tenantId = $this->tenantId();
$page = (int) ($params['page'] ?? 1);
$size = (int) ($params['size'] ?? 20);
$q = trim((string) ($params['q'] ?? ''));
$onlyActive = $params['only_active'] ?? null;
$query = ClientGroup::query()->where('tenant_id', $tenantId);
if ($q !== '') {
$query->where(function ($qq) use ($q) {
$qq->where('group_name', 'like', "%{$q}%")
->orWhere('group_code', 'like', "%{$q}%");
});
}
if ($onlyActive !== null) {
$query->where('is_active', $onlyActive ? 1 : 0);
}
$query->orderBy('group_code')->orderBy('id');
return $query->paginate($size, ['*'], 'page', $page);
}
/** 단건 */
public function show(int $id)
{
$tenantId = $this->tenantId();
$group = ClientGroup::where('tenant_id', $tenantId)->find($id);
if (! $group) {
throw new NotFoundHttpException(__('error.not_found'));
}
return $group;
}
/** 생성 */
public function store(array $params)
{
$tenantId = $this->tenantId();
$uid = $this->apiUserId();
$v = Validator::make($params, [
'group_code' => 'required|string|max:30',
'group_name' => 'required|string|max:100',
'price_rate' => 'required|numeric|min:0|max:99.9999',
'is_active' => 'nullable|boolean',
]);
if ($v->fails()) {
throw new BadRequestHttpException($v->errors()->first());
}
$data = $v->validated();
// group_code 중복 검사
$exists = ClientGroup::where('tenant_id', $tenantId)
->where('group_code', $data['group_code'])
->exists();
if ($exists) {
throw new BadRequestHttpException(__('error.duplicate_code'));
}
$data['tenant_id'] = $tenantId;
$data['is_active'] = $data['is_active'] ?? 1;
$data['created_by'] = $uid;
return ClientGroup::create($data);
}
/** 수정 */
public function update(int $id, array $params)
{
$tenantId = $this->tenantId();
$uid = $this->apiUserId();
$group = ClientGroup::where('tenant_id', $tenantId)->find($id);
if (! $group) {
throw new NotFoundHttpException(__('error.not_found'));
}
$v = Validator::make($params, [
'group_code' => 'sometimes|required|string|max:30',
'group_name' => 'sometimes|required|string|max:100',
'price_rate' => 'sometimes|required|numeric|min:0|max:99.9999',
'is_active' => 'nullable|boolean',
]);
if ($v->fails()) {
throw new BadRequestHttpException($v->errors()->first());
}
$payload = $v->validated();
// group_code 변경 시 중복 검사
if (isset($payload['group_code']) && $payload['group_code'] !== $group->group_code) {
$exists = ClientGroup::where('tenant_id', $tenantId)
->where('group_code', $payload['group_code'])
->exists();
if ($exists) {
throw new BadRequestHttpException(__('error.duplicate_code'));
}
}
$payload['updated_by'] = $uid;
$group->update($payload);
return $group->refresh();
}
/** 삭제 */
public function destroy(int $id)
{
$tenantId = $this->tenantId();
$uid = $this->apiUserId();
$group = ClientGroup::where('tenant_id', $tenantId)->find($id);
if (! $group) {
throw new NotFoundHttpException(__('error.not_found'));
}
// 해당 그룹에 속한 고객이 있는지 검사
if ($group->clients()->exists()) {
throw new BadRequestHttpException(__('error.has_clients'));
}
$group->deleted_by = $uid;
$group->save();
$group->delete();
return 'success';
}
/** 활성/비활성 토글 */
public function toggle(int $id)
{
$tenantId = $this->tenantId();
$uid = $this->apiUserId();
$group = ClientGroup::where('tenant_id', $tenantId)->find($id);
if (! $group) {
throw new NotFoundHttpException(__('error.not_found'));
}
$group->is_active = $group->is_active ? 0 : 1;
$group->updated_by = $uid;
$group->save();
return $group->refresh();
}
}