Skip to content

Commit

Permalink
docs(README.md): add installation and usage instructions to improve p…
Browse files Browse the repository at this point in the history
…roject onboarding

feat(handlers/stocks.go): implement getStringValue function to handle different data types for stock attributes
  • Loading branch information
Abdullah Alqahtani committed Dec 27, 2024
1 parent bc34309 commit b0c3994
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ cp .env.example .env
# Edit .env with your configurations
```

## Installation
1. Clone the project.
2. Install Go dependencies:
```
go mod download
```

## Environment
Set the following environment variables or edit the .env file:
- MONGO_URI
- MONGO_DATABASE
- PORT

## Usage
1. Run the server:
```
go run cmd/api/main.go
```
2. Access the API at:
```
http://localhost:3000/api/v1
```

## Environment Variables

Before running the application, make sure to set up your environment variables:
Expand Down
47 changes: 46 additions & 1 deletion internal/handlers/stocks.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
package handlers
package handlers

import (
"github.com/gofiber/fiber/v2"
"strconv"
)

func getStringValue(val interface{}) string {
switch v := val.(type) {
case string:
return v
case float64:
return strconv.FormatFloat(v, 'f', -1, 64)
default:
return ""
}
}

func (h *Handler) GetStocksByYearHandler(c *fiber.Ctx) error {
// ...existing code...
for _, doc := range stockDocs {
stock := Stock{
Name: getStringValue(doc["name"]),
Code: getStringValue(doc["code"]),
Sector: getStringValue(doc["sector"]),
ShariaOpinion: getStringValue(doc["sharia_opinion"]),
}
// ...existing code...
}
// ...existing code...
}

func (h *Handler) SearchStocksHandler(c *fiber.Ctx) error {
// ...existing code...
for _, doc := range stockDocs {
stock := Stock{
Name: getStringValue(doc["name"]),
Code: getStringValue(doc["code"]),
Sector: getStringValue(doc["sector"]),
ShariaOpinion: getStringValue(doc["sharia_opinion"]),
}
// ...existing code...
}
// ...existing code...
}

import (
"fmt"
Expand Down Expand Up @@ -66,7 +111,7 @@ func (h *Handler) GetStocksByYearHandler(c *fiber.Ctx) error {
if err != nil {
return c.Status(500).JSON(ErrorResponse{Error: "Failed to fetch stocks"})
}

var stockDocs []bson.M
if err = cursor.All(c.Context(), &stockDocs); err != nil {
return c.Status(500).JSON(ErrorResponse{Error: "Failed to parse stocks"})
Expand Down

0 comments on commit b0c3994

Please sign in to comment.