2.8 KiB
2.8 KiB
Articles Response - Client Name Field
Overview
Field clientName telah ditambahkan ke ArticlesResponse untuk memberikan informasi nama client yang memiliki artikel tersebut.
Field Baru
ArticlesResponse
type ArticlesResponse struct {
// ... existing fields ...
CreatedByName *string `json:"createdByName"`
ClientName *string `json:"clientName"` // NEW FIELD
// ... other fields ...
}
Implementasi
1. Response Structure
- Field
ClientNameditambahkan keArticlesResponsestruct - Type:
*string(optional field) - JSON tag:
"clientName"
2. Mapper Updates
ArticlesResponseMapperdiperbarui untuk melakukan lookup nama client- Menggunakan
clientsRepository.FindOneByClientId()untuk mendapatkan nama client - Field
ClientNamediisi berdasarkanarticlesReq.ClientId
3. Service Updates
ArticlesServicediperbarui untuk menyertakanclientsRepositorydependency- Constructor
NewArticlesServicemenambahkan parameterclientsRepository - Semua pemanggilan
ArticlesResponseMapperdiperbarui untuk menyertakan parameter baru
4. Repository Updates
ClientsRepositoryinterface ditambahkan methodFindOneByClientId(clientId *uuid.UUID)- Implementasi method untuk mencari client berdasarkan ID
API Response Example
Before
{
"success": true,
"data": {
"id": 1,
"title": "Sample Article",
"createdByName": "John Doe",
"clientId": "123e4567-e89b-12d3-a456-426614174000"
}
}
After
{
"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 keclients.idclients.name(string) adalah nama client yang akan ditampilkan
Error Handling
- Jika
articlesReq.ClientIdadalahnil, makaClientNameakan menjadi"" - Jika client tidak ditemukan, maka
ClientNameakan 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 articlesGET /articles/{id}- Get single articleGET /articles/old-id/{id}- Get article by old ID
Migration
Tidak ada perubahan database yang diperlukan karena:
- Field
client_idsudah ada di tabelarticles - Field
namesudah ada di tabelclients - Hanya menambahkan field response untuk menampilkan nama client