initial commit
This commit is contained in:
commit
1253955070
75 changed files with 13972 additions and 0 deletions
29
api/internal/service/helpers.go
Normal file
29
api/internal/service/helpers.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"go-places-api/internal/domain"
|
||||
)
|
||||
|
||||
// marshalPayload encodes an event payload as JSON.
|
||||
func marshalPayload(payload map[string]any) (json.RawMessage, error) {
|
||||
body, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("service: marshal event payload: %w", err)
|
||||
}
|
||||
return json.RawMessage(body), nil
|
||||
}
|
||||
|
||||
// sortRelated orders related places by distance, then by identifier so the
|
||||
// result is stable for equal distances.
|
||||
func sortRelated(in []domain.RelatedPlace) {
|
||||
sort.SliceStable(in, func(i, j int) bool {
|
||||
if in[i].DistanceMeters != in[j].DistanceMeters {
|
||||
return in[i].DistanceMeters < in[j].DistanceMeters
|
||||
}
|
||||
return in[i].Place.ID < in[j].Place.ID
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue