diff --git a/AGENTS.md b/AGENTS.md index b4d7d2e..d1ef2bc 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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 diff --git a/delivery/controller/nearest_device_controller.go b/delivery/controller/nearest_device_controller.go index 405fa66..f35cb7d 100644 --- a/delivery/controller/nearest_device_controller.go +++ b/delivery/controller/nearest_device_controller.go @@ -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", }, } diff --git a/usecase/AGENTS.md b/usecase/AGENTS.md index 77fe329..4e97c54 100644 --- a/usecase/AGENTS.md +++ b/usecase/AGENTS.md @@ -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