kontenhumas-be/docs/ARTICLES_CLIENT_NAME_FIELD.md

105 lines
2.8 KiB
Markdown
Raw Normal View History

# Articles Response - Client Name Field
## Overview
Field `clientName` telah ditambahkan ke `ArticlesResponse` untuk memberikan informasi nama client yang memiliki artikel tersebut.
## Field Baru
### ArticlesResponse
```go
type ArticlesResponse struct {
// ... existing fields ...
CreatedByName *string `json:"createdByName"`
ClientName *string `json:"clientName"` // NEW FIELD
// ... other fields ...
}
```
## Implementasi
### 1. Response Structure
- Field `ClientName` ditambahkan ke `ArticlesResponse` struct
- Type: `*string` (optional field)
- JSON tag: `"clientName"`
### 2. Mapper Updates
- `ArticlesResponseMapper` diperbarui untuk melakukan lookup nama client
- Menggunakan `clientsRepository.FindOneByClientId()` untuk mendapatkan nama client
- Field `ClientName` diisi berdasarkan `articlesReq.ClientId`
### 3. Service Updates
- `ArticlesService` diperbarui untuk menyertakan `clientsRepository` dependency
- Constructor `NewArticlesService` menambahkan parameter `clientsRepository`
- Semua pemanggilan `ArticlesResponseMapper` diperbarui untuk menyertakan parameter baru
### 4. Repository Updates
- `ClientsRepository` interface ditambahkan method `FindOneByClientId(clientId *uuid.UUID)`
- Implementasi method untuk mencari client berdasarkan ID
## API Response Example
### Before
```json
{
"success": true,
"data": {
"id": 1,
"title": "Sample Article",
"createdByName": "John Doe",
"clientId": "123e4567-e89b-12d3-a456-426614174000"
}
}
```
### After
```json
{
"success": true,
"data": {
"id": 1,
"title": "Sample Article",
"createdByName": "John Doe",
"clientName": "Acme Corporation",
"clientId": "123e4567-e89b-12d3-a456-426614174000"
}
}
```
## Database Relationship
```
articles.client_id -> clients.id -> clients.name
```
- `articles.client_id` (UUID) merujuk ke `clients.id`
- `clients.name` (string) adalah nama client yang akan ditampilkan
## Error Handling
- Jika `articlesReq.ClientId` adalah `nil`, maka `ClientName` akan menjadi `""`
- Jika client tidak ditemukan, maka `ClientName` akan menjadi `""`
- Tidak ada error yang di-throw untuk missing client (graceful handling)
## Performance Considerations
- Lookup client dilakukan per artikel (N+1 query pattern)
- Untuk performa yang lebih baik di masa depan, pertimbangkan:
- Batch lookup untuk multiple articles
- Preloading client data dalam repository query
- Caching client names
## Usage
Field `clientName` akan otomatis tersedia di semua endpoint articles:
- `GET /articles` - List articles
- `GET /articles/{id}` - Get single article
- `GET /articles/old-id/{id}` - Get article by old ID
## Migration
Tidak ada perubahan database yang diperlukan karena:
- Field `client_id` sudah ada di tabel `articles`
- Field `name` sudah ada di tabel `clients`
- Hanya menambahkan field response untuk menampilkan nama client