fix the get method

This commit is contained in:
areeqakbr 2026-04-11 14:43:59 +07:00
parent 1dbd96eebd
commit 128bc30680
3 changed files with 42 additions and 12 deletions

View File

@ -57,6 +57,8 @@ backend_nam/
go run main.go # Local dev
go build -o main . # Build binary
docker build -t nam-backend . # Docker build
go test ./... # Run all tests
go test -v ./usecase/... # Run usecase tests
```
## NOTES

View File

@ -37,7 +37,7 @@ func (c *NearestDeviceController) Route() {
nearestDevices.POST("/towers/search", c.getNearestTowers)
nearestDevices.GET("/towers/:id", c.getNearestTowerByID)
nearestDevices.POST("/odp/search", c.getNearestODPDevices)
nearestDevices.GET("/odp/search", c.getNearestODPDevices)
}
}
@ -176,14 +176,44 @@ func (c *NearestDeviceController) getNearestDeviceByID(ctx *gin.Context) {
}
func (c *NearestDeviceController) getNearestODPDevices(ctx *gin.Context) {
var request req.NearestDeviceDTO
if err := ctx.ShouldBindJSON(&request); err != nil {
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
latStr := ctx.Query("latitude")
lngStr := ctx.Query("longitude")
radiusStr := ctx.Query("radius")
if latStr == "" || lngStr == "" {
common.ErrorResponses(ctx, http.StatusBadRequest, "latitude and longitude are required")
return
}
latitude, err := strconv.ParseFloat(latStr, 64)
if err != nil {
common.ErrorResponses(ctx, http.StatusBadRequest, "invalid latitude")
return
}
longitude, err := strconv.ParseFloat(lngStr, 64)
if err != nil {
common.ErrorResponses(ctx, http.StatusBadRequest, "invalid longitude")
return
}
radius := 0.3
if radiusStr != "" {
radius, err = strconv.ParseFloat(radiusStr, 64)
if err != nil || radius <= 0 {
common.ErrorResponses(ctx, http.StatusBadRequest, "invalid radius")
return
}
}
odpType := "ODP"
request.DeviceType = &odpType
request := req.NearestDeviceDTO{
Latitude: latitude,
Longitude: longitude,
Radius: radius,
DeviceType: &odpType,
Limit: 10,
}
devices, err := c.nearestDeviceUC.GetNearestDevices(request)
if err != nil {
@ -195,13 +225,11 @@ func (c *NearestDeviceController) getNearestODPDevices(ctx *gin.Context) {
"devices": devices,
"total": len(devices),
"search_params": gin.H{
"latitude": request.Latitude,
"longitude": request.Longitude,
"radius": request.Radius,
"latitude": latitude,
"longitude": longitude,
"radius": radius,
"device_type": "ODP",
"province": request.Province,
"city": request.City,
"district": request.District,
"unit": "kilometers",
},
}

View File

@ -14,7 +14,7 @@ CONVENTIONS
- Validation uses validator.New() inside use cases
- GeocodingService is injected via dependency injection (not instantiated in use cases)
- File naming uses snake_case; interfaces named {Entity}UseCase / {Entity}Repo
- Tests follow existing project patterns and naming
- Test file: `{name}_test.go` with mock implementations
ANTI-PATTERNS
- Do not skip validation in use cases; always validate inputs via validator