feat: [boards] 게시판 API 시스템 구현

- BoardController, PostController 추가
- Board, BoardSetting 모델 수정
- BoardService 추가
- FormRequest 클래스 추가
- Swagger 문서 추가 (BoardApi, PostApi)
- 게시판 시스템 필드 마이그레이션 추가
This commit is contained in:
2025-11-30 21:05:33 +09:00
parent d9192045da
commit d27e47108d
14 changed files with 1944 additions and 4 deletions

View File

@@ -2,27 +2,214 @@
namespace App\Models\Boards;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* 게시판 모델
*
* @property int $id
* @property int|null $tenant_id
* @property bool $is_system 시스템 게시판 여부 (true=전체 테넌트 공개)
* @property string|null $board_type 게시판 유형 (notice, qna, faq, free 등)
* @property string $board_code 게시판 코드
* @property string $name 게시판명
* @property string|null $description 설명
* @property string $editor_type 에디터 타입
* @property bool $allow_files 파일 첨부 허용
* @property int $max_file_count 최대 파일 수
* @property int $max_file_size 최대 파일 크기 (KB)
* @property array|null $extra_settings 추가 설정 (권한, 옵션 등)
* @property bool $is_active 활성 여부
*
* @mixin IdeHelperBoard
*/
class Board extends Model
{
use SoftDeletes;
protected $table = 'boards';
protected $fillable = [
'tenant_id', 'board_code', 'name', 'description', 'editor_type',
'allow_files', 'max_file_count', 'max_file_size', 'extra_settings', 'is_active',
'tenant_id',
'is_system',
'board_type',
'board_code',
'name',
'description',
'editor_type',
'allow_files',
'max_file_count',
'max_file_size',
'extra_settings',
'is_active',
'created_by',
'updated_by',
'deleted_by',
];
protected $casts = [
'is_system' => 'boolean',
'is_active' => 'boolean',
'allow_files' => 'boolean',
'extra_settings' => 'array',
];
protected $attributes = [
'is_system' => false,
'is_active' => true,
'allow_files' => true,
'max_file_count' => 5,
'max_file_size' => 20480,
'editor_type' => 'wysiwyg',
];
// =========================================================================
// Scopes
// =========================================================================
/**
* 현재 테넌트에서 접근 가능한 게시판
* - 시스템 게시판 (is_system = true)
* - 해당 테넌트 게시판 (tenant_id = 현재 테넌트)
*/
public function scopeAccessible(Builder $query, int $tenantId): Builder
{
return $query->where(function ($q) use ($tenantId) {
$q->where('is_system', true)
->orWhere('tenant_id', $tenantId);
})->where('is_active', true);
}
/**
* 시스템 게시판만 (mng용)
*/
public function scopeSystemOnly(Builder $query): Builder
{
return $query->where('is_system', true);
}
/**
* 테넌트 게시판만
*/
public function scopeTenantOnly(Builder $query, int $tenantId): Builder
{
return $query->where('is_system', false)
->where('tenant_id', $tenantId);
}
/**
* 게시판 유형으로 필터링
*/
public function scopeOfType(Builder $query, string $type): Builder
{
return $query->where('board_type', $type);
}
// =========================================================================
// Relationships
// =========================================================================
public function customFields()
{
return $this->hasMany(BoardSetting::class, 'board_id');
return $this->hasMany(BoardSetting::class, 'board_id')
->orderBy('sort_order');
}
public function fields()
{
return $this->customFields();
}
public function posts()
{
return $this->hasMany(Post::class, 'board_id');
}
public function tenant()
{
return $this->belongsTo(\App\Models\Tenant::class);
}
public function creator()
{
return $this->belongsTo(User::class, 'created_by');
}
public function updater()
{
return $this->belongsTo(User::class, 'updated_by');
}
// =========================================================================
// Helper Methods
// =========================================================================
/**
* extra_settings에서 특정 키 값 가져오기
*/
public function getSetting(string $key, $default = null)
{
return data_get($this->extra_settings, $key, $default);
}
/**
* extra_settings에 값 설정하기
*/
public function setSetting(string $key, $value): self
{
$settings = $this->extra_settings ?? [];
data_set($settings, $key, $value);
$this->extra_settings = $settings;
return $this;
}
/**
* 읽기 권한 확인
*/
public function canRead(User $user): bool
{
$roles = $this->getSetting('permissions.read', ['*']);
return in_array('*', $roles) || $user->hasAnyRole($roles);
}
/**
* 쓰기 권한 확인
*/
public function canWrite(User $user): bool
{
$roles = $this->getSetting('permissions.write', ['*']);
return in_array('*', $roles) || $user->hasAnyRole($roles);
}
/**
* 관리 권한 확인
*/
public function canManage(User $user): bool
{
$roles = $this->getSetting('permissions.manage', ['admin']);
return $user->hasAnyRole($roles);
}
/**
* 시스템 게시판 여부 확인
*/
public function isSystemBoard(): bool
{
return $this->is_system === true;
}
/**
* 테넌트 게시판 여부 확인
*/
public function isTenantBoard(): bool
{
return $this->is_system === false;
}
}

View File

@@ -5,6 +5,17 @@
use Illuminate\Database\Eloquent\Model;
/**
* 게시판 필드 설정 (EAV 스키마 정의)
*
* @property int $id
* @property int $board_id
* @property string $name 필드명
* @property string $field_key 필드 키
* @property string $field_type 필드 타입 (text, number, select, date 등)
* @property array|null $field_meta 필드 메타 (옵션, 유효성 등)
* @property bool $is_required 필수 여부
* @property int $sort_order 정렬 순서
*
* @mixin IdeHelperBoardSetting
*/
class BoardSetting extends Model
@@ -12,11 +23,38 @@ class BoardSetting extends Model
protected $table = 'board_settings';
protected $fillable = [
'board_id', 'name', 'field_key', 'field_type', 'field_meta', 'is_required', 'sort_order',
'board_id',
'name',
'field_key',
'field_type',
'field_meta',
'is_required',
'sort_order',
'created_by',
'updated_by',
];
protected $casts = [
'field_meta' => 'array',
'is_required' => 'boolean',
'sort_order' => 'integer',
];
protected $attributes = [
'is_required' => false,
'sort_order' => 0,
];
public function board()
{
return $this->belongsTo(Board::class, 'board_id');
}
/**
* field_meta에서 특정 키 값 가져오기
*/
public function getMeta(string $key, $default = null)
{
return data_get($this->field_meta, $key, $default);
}
}