feat(API): Position API 추가 (직급/직책 통합)
- Position 모델 생성 (type: rank | title) - PositionService: CRUD + reorder 구현 - PositionController: REST API 엔드포인트 - Swagger 문서 작성 (PositionApi.php) - 마이그레이션: positions 테이블 + common_codes 등록 - routes/api.php에 라우트 등록 Phase L-3 (직급관리), L-4 (직책관리) 백엔드 완료 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
141
app/Services/PositionService.php
Normal file
141
app/Services/PositionService.php
Normal file
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Tenants\Position;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PositionService extends Service
|
||||
{
|
||||
/**
|
||||
* 목록 조회
|
||||
*/
|
||||
public function index(array $params)
|
||||
{
|
||||
$query = Position::query()
|
||||
->when(isset($params['type']), fn ($q) => $q->where('type', $params['type']))
|
||||
->when(isset($params['is_active']), fn ($q) => $q->where('is_active', (bool) $params['is_active']))
|
||||
->when(! empty($params['q']), fn ($q) => $q->where('name', 'like', '%'.$params['q'].'%'))
|
||||
->ordered();
|
||||
|
||||
if (isset($params['per_page'])) {
|
||||
return $query->paginate((int) $params['per_page']);
|
||||
}
|
||||
|
||||
return $query->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 단건 조회
|
||||
*/
|
||||
public function show(int $id)
|
||||
{
|
||||
$position = Position::find($id);
|
||||
|
||||
if (! $position) {
|
||||
return ['error' => __('error.not_found'), 'code' => 404];
|
||||
}
|
||||
|
||||
return $position;
|
||||
}
|
||||
|
||||
/**
|
||||
* 생성
|
||||
*/
|
||||
public function store(array $data)
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
|
||||
// 중복 체크
|
||||
$exists = Position::where('tenant_id', $tenantId)
|
||||
->where('type', $data['type'])
|
||||
->where('name', $data['name'])
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
return ['error' => __('error.duplicate'), 'code' => 409];
|
||||
}
|
||||
|
||||
// 정렬 순서 자동 설정
|
||||
if (! isset($data['sort_order'])) {
|
||||
$maxOrder = Position::where('tenant_id', $tenantId)
|
||||
->where('type', $data['type'])
|
||||
->max('sort_order') ?? 0;
|
||||
$data['sort_order'] = $maxOrder + 1;
|
||||
}
|
||||
|
||||
$position = Position::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'type' => $data['type'],
|
||||
'name' => $data['name'],
|
||||
'sort_order' => $data['sort_order'],
|
||||
'is_active' => $data['is_active'] ?? true,
|
||||
]);
|
||||
|
||||
return $position;
|
||||
}
|
||||
|
||||
/**
|
||||
* 수정
|
||||
*/
|
||||
public function update(int $id, array $data)
|
||||
{
|
||||
$position = Position::find($id);
|
||||
|
||||
if (! $position) {
|
||||
return ['error' => __('error.not_found'), 'code' => 404];
|
||||
}
|
||||
|
||||
// 이름 중복 체크 (자기 자신 제외)
|
||||
if (isset($data['name'])) {
|
||||
$exists = Position::where('tenant_id', $position->tenant_id)
|
||||
->where('type', $position->type)
|
||||
->where('name', $data['name'])
|
||||
->where('id', '!=', $id)
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
return ['error' => __('error.duplicate'), 'code' => 409];
|
||||
}
|
||||
}
|
||||
|
||||
$position->update([
|
||||
'name' => $data['name'] ?? $position->name,
|
||||
'sort_order' => $data['sort_order'] ?? $position->sort_order,
|
||||
'is_active' => $data['is_active'] ?? $position->is_active,
|
||||
]);
|
||||
|
||||
return $position->fresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* 삭제
|
||||
*/
|
||||
public function destroy(int $id)
|
||||
{
|
||||
$position = Position::find($id);
|
||||
|
||||
if (! $position) {
|
||||
return ['error' => __('error.not_found'), 'code' => 404];
|
||||
}
|
||||
|
||||
$position->delete();
|
||||
|
||||
return ['id' => $id, 'deleted_at' => now()->toDateTimeString()];
|
||||
}
|
||||
|
||||
/**
|
||||
* 순서 변경 (벌크)
|
||||
*/
|
||||
public function reorder(array $items)
|
||||
{
|
||||
DB::transaction(function () use ($items) {
|
||||
foreach ($items as $item) {
|
||||
Position::where('id', $item['id'])
|
||||
->update(['sort_order' => $item['sort_order']]);
|
||||
}
|
||||
});
|
||||
|
||||
return ['success' => true, 'updated' => count($items)];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user