Skip to content

Commit

Permalink
add locations endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitsethiaDH committed Jul 2, 2024
1 parent 596b82e commit 5f5e36d
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 6 deletions.
46 changes: 46 additions & 0 deletions app/application/location_service.go
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 {

Check failure on line 8 in app/application/location_service.go

View workflow job for this annotation

GitHub Actions / build

exported: exported type ILocation should have comment or be unexported (revive)
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 {

Check failure on line 18 in app/application/location_service.go

View workflow job for this annotation

GitHub Actions / build

exported: exported function NewLocationService should have comment or be unexported (revive)
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)
}
10 changes: 10 additions & 0 deletions app/domain/location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package domain

type Location struct {

Check failure on line 3 in app/domain/location.go

View workflow job for this annotation

GitHub Actions / build

exported: exported type Location should have comment or be unexported (revive)
Lat float64 `json:"lat"`
Long float64 `json:"long"`
}

//[14, 15, 16] - Lat
//[16, 17, 18] - Long
// 0 , 1, 2
58 changes: 58 additions & 0 deletions app/http/location_handler.go
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 {

Check failure on line 13 in app/http/location_handler.go

View workflow job for this annotation

GitHub Actions / build

exported: exported type LocationHandler should have comment or be unexported (revive)
locationService application.ILocation
}

func NewLocationHandler(locationService application.ILocation) *LocationHandler {

Check failure on line 17 in app/http/location_handler.go

View workflow job for this annotation

GitHub Actions / build

exported: exported function NewLocationHandler should have comment or be unexported (revive)
return &LocationHandler{
locationService: locationService,
}
}

func (h *LocationHandler) GetLastNLocation(w http.ResponseWriter, r *http.Request) {

Check failure on line 23 in app/http/location_handler.go

View workflow job for this annotation

GitHub Actions / build

exported: exported method LocationHandler.GetLastNLocation should have comment or be unexported (revive)
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) {

Check failure on line 38 in app/http/location_handler.go

View workflow job for this annotation

GitHub Actions / build

exported: exported method LocationHandler.UpdateLocation should have comment or be unexported (revive)
//'{
//"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)
}
24 changes: 18 additions & 6 deletions cmd/link-identity-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ func main() {

repo := repository.NewContactRepository(db)

service := application.NewService(repo)
handler := httpHandler.NewLinkIdentityHandler(service)
identityService := application.NewService(repo)
identityHandler := httpHandler.NewLinkIdentityHandler(identityService)

locationService := application.NewLocationService()
locationHandler := httpHandler.NewLocationHandler(locationService)

// setup the http server
router := SetupRouters(handler)
router := SetupRouters(identityHandler, locationHandler)

// service address will be changed as port in next PR.
// identityService address will be changed as port in next PR.
srv := &http.Server{
Addr: fmt.Sprintf(":%s", appconfig.Values.Server.Port),
ReadTimeout: 10 * time.Second,
Expand Down Expand Up @@ -96,7 +99,7 @@ func main() {
}

// SetupRouters ...
func SetupRouters(handler *httpHandler.LinkIdentityHandler) *chi.Mux {
func SetupRouters(identityHandler *httpHandler.LinkIdentityHandler, locationHandler *httpHandler.LocationHandler) *chi.Mux {
// Base route initialize.
router := chi.NewRouter()
router.Use(infrastructure.NewLoggerMiddleware(logEntry).Wrap)
Expand All @@ -107,7 +110,16 @@ func SetupRouters(handler *httpHandler.LinkIdentityHandler) *chi.Mux {

// Register Contact get handler
{
router.Post("/identify", handler.Identify)
router.Post("/identify", identityHandler.Identify)
}

// location handler
{
//'localhost:8080/location/
//steve?max=3
router.Get("/location/{rider}", locationHandler.GetLastNLocation)
//'localhost:8080/location/steve/now'
router.Post("/location/{rider}/now", locationHandler.UpdateLocation)
}
return router
}
Expand Down

0 comments on commit 5f5e36d

Please sign in to comment.