Skip to content

Commit 22a7be0

Browse files
committed
feat(stocks): enhance stock model and handlers to include Arabic and English names, logo, and timestamps
- Add NameAr and NameEn fields to the Stock model for better localization. - Include Logo field in the Stock model to store stock logos. - Update GetStocksByYearHandler to populate new fields from the database. - Modify SearchStocksHandler to filter stocks by both Arabic and English names. - Add CreatedAt and UpdatedAt fields to the Stock model to track timestamps.
1 parent 39c0eed commit 22a7be0

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

internal/handlers/stocks.go

+27-3
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,22 @@ func (h *Handler) GetStocksByYearHandler(c *fiber.Ctx) error {
3737
id, _ := doc["_id"].(primitive.ObjectID)
3838
stock := models.Stock{
3939
ID: id,
40-
Name: utils.SafeString(doc["name"]),
4140
Code: utils.SafeString(doc["code"]),
4241
Sector: utils.SafeString(doc["sector"]),
4342
ShariaOpinion: utils.SafeString(doc["sharia_opinion"]),
4443
Purification: utils.SafeString(doc["purification"]),
44+
NameAr: utils.SafeString(doc["name_ar"]),
45+
NameEn: utils.SafeString(doc["name_en"]),
46+
Logo: utils.SafeString(doc["logo"]),
4547
}
48+
49+
if createdAt, ok := doc["created_at"].(primitive.DateTime); ok {
50+
stock.CreatedAt = createdAt
51+
}
52+
if updatedAt, ok := doc["updated_at"].(primitive.DateTime); ok {
53+
stock.UpdatedAt = updatedAt
54+
}
55+
4656
result = append(result, stock)
4757
}
4858

@@ -54,7 +64,10 @@ func (h *Handler) SearchStocksHandler(c *fiber.Ctx) error {
5464
filter := bson.M{}
5565

5666
if name := c.Query("name"); name != "" {
57-
filter["name"] = bson.M{"$regex": name, "$options": "i"}
67+
filter["$or"] = []bson.M{
68+
{"name_ar": bson.M{"$regex": name, "$options": "i"}},
69+
{"name_en": bson.M{"$regex": name, "$options": "i"}},
70+
}
5871
}
5972
if code := c.Query("code"); code != "" {
6073
filter["code"] = code
@@ -85,12 +98,23 @@ func (h *Handler) SearchStocksHandler(c *fiber.Ctx) error {
8598
id, _ := doc["_id"].(primitive.ObjectID)
8699
stock := models.Stock{
87100
ID: id,
88-
Name: utils.SafeString(doc["name"]),
89101
Code: utils.SafeString(doc["code"]),
90102
Sector: utils.SafeString(doc["sector"]),
91103
ShariaOpinion: utils.SafeString(doc["sharia_opinion"]),
92104
Purification: utils.SafeString(doc["purification"]),
105+
NameAr: utils.SafeString(doc["name_ar"]),
106+
NameEn: utils.SafeString(doc["name_en"]),
107+
Logo: utils.SafeString(doc["logo"]),
108+
}
109+
110+
// Handle timestamps if they exist
111+
if createdAt, ok := doc["created_at"].(primitive.DateTime); ok {
112+
stock.CreatedAt = createdAt
113+
}
114+
if updatedAt, ok := doc["updated_at"].(primitive.DateTime); ok {
115+
stock.UpdatedAt = updatedAt
93116
}
117+
94118
result = append(result, stock)
95119
}
96120

internal/models/stock.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ import "go.mongodb.org/mongo-driver/bson/primitive"
99
// Stock represents a stock entity
1010
type Stock struct {
1111
ID primitive.ObjectID `json:"_id" bson:"_id,omitempty"`
12-
Name string `json:"name" bson:"name"`
1312
Code string `json:"code" bson:"code"`
1413
Sector string `json:"sector" bson:"sector"`
1514
ShariaOpinion string `json:"sharia_opinion" bson:"sharia_opinion"`
1615
Purification string `json:"purification" bson:"purification"`
16+
NameAr string `json:"name_ar" bson:"name_ar"`
17+
NameEn string `json:"name_en" bson:"name_en"`
18+
Logo string `json:"logo" bson:"logo"`
19+
CreatedAt primitive.DateTime `json:"created_at" bson:"created_at"`
20+
UpdatedAt primitive.DateTime `json:"updated_at" bson:"updated_at"`
1721
}
1822

1923
// StockResponse represents a response containing a list of stocks

0 commit comments

Comments
 (0)