-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
596b82e
commit 5f5e36d
Showing
4 changed files
with
132 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package application | ||
|
||
import ( | ||
"github.com/link-identity/app/domain" | ||
"sync" | ||
) | ||
|
||
type ILocation interface { | ||
GetLastNLocation(rider string, lastN int) []domain.Location | ||
UpdateLocation(rider string, currLocation domain.Location) | ||
} | ||
|
||
type location struct { | ||
riderLocations map[string][]domain.Location | ||
sync sync.RWMutex | ||
} | ||
|
||
func NewLocationService() ILocation { | ||
return &location{} | ||
} | ||
|
||
func (l *location) GetLastNLocation(rider string, lastN int) []domain.Location { | ||
//slice := l.riderLocations[riderLocations][len()] | ||
//addr val addr | ||
var locations []domain.Location | ||
for i := len(l.riderLocations[rider]) - 1; i >= len(l.riderLocations[rider])-lastN && i >= 0; i-- { | ||
locations = append(locations, l.riderLocations[rider][i]) | ||
} | ||
return locations | ||
} | ||
|
||
func (l *location) UpdateLocation(rider string, currLocation domain.Location) { | ||
if l.riderLocations == nil { | ||
l.riderLocations = make(map[string][]domain.Location) | ||
} | ||
if l.riderLocations[rider] == nil { | ||
l.riderLocations[rider] = make([]domain.Location, 0) | ||
} | ||
l.sync.Lock() | ||
defer func() { | ||
l.sync.Unlock() | ||
}() | ||
|
||
//err | ||
l.riderLocations[rider] = append(l.riderLocations[rider], currLocation) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package domain | ||
|
||
type Location struct { | ||
Lat float64 `json:"lat"` | ||
Long float64 `json:"long"` | ||
} | ||
|
||
//[14, 15, 16] - Lat | ||
//[16, 17, 18] - Long | ||
// 0 , 1, 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package http | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/go-chi/chi" | ||
"github.com/link-identity/app/application" | ||
"github.com/link-identity/app/domain" | ||
"github.com/link-identity/app/utils" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
type LocationHandler struct { | ||
locationService application.ILocation | ||
} | ||
|
||
func NewLocationHandler(locationService application.ILocation) *LocationHandler { | ||
return &LocationHandler{ | ||
locationService: locationService, | ||
} | ||
} | ||
|
||
func (h *LocationHandler) GetLastNLocation(w http.ResponseWriter, r *http.Request) { | ||
lastN := r.URL.Query().Get("max") | ||
intLastN, ok := strconv.Atoi(lastN) | ||
if ok != nil || intLastN == 0 { | ||
intLastN = 5 | ||
} | ||
rider := chi.URLParam(r, "rider") | ||
locations := h.locationService.GetLastNLocation(rider, intLastN) | ||
resp := utils.ResponseDTO{ | ||
StatusCode: http.StatusOK, | ||
Data: locations, | ||
} | ||
utils.ResponseJSON(w, http.StatusOK, resp) | ||
} | ||
|
||
func (h *LocationHandler) UpdateLocation(w http.ResponseWriter, r *http.Request) { | ||
//'{ | ||
//"lat": 12.34, | ||
//"long": 56.78 | ||
//}' | ||
//'localhost:8080/location/steve/now' | ||
rider := chi.URLParam(r, "rider") | ||
|
||
var location domain.Location | ||
if err := json.NewDecoder(r.Body).Decode(&location); err != nil { | ||
resp := utils.NewErrorResponse(http.StatusBadRequest, err.Error()) | ||
utils.ResponseJSON(w, http.StatusBadRequest, resp) | ||
return | ||
} | ||
h.locationService.UpdateLocation(rider, location) | ||
resp := utils.ResponseDTO{ | ||
StatusCode: http.StatusOK, | ||
Data: nil, | ||
} | ||
utils.ResponseJSON(w, http.StatusOK, resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters