summaryrefslogtreecommitdiff
path: root/official_climb/model.go
blob: ae66f465029decdf242ee2f48585e4480512aef2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package official_climb

import (
	"fmt"
	"math"
	"strings"
	"time"

	"github.com/jftuga/geodist"
)

const DefaultMatchRadiusM = 100.0

type OfficialClimb struct {
	ID         int64
	Name       string
	StartCoord geodist.Coord
	EndCoord   geodist.Coord
	CreatedAt  time.Time
	UpdatedAt  time.Time
}

type MatchPolicy struct {
	EndpointRadiusM float64
}

func DefaultMatchPolicy() MatchPolicy {
	return MatchPolicy{EndpointRadiusM: DefaultMatchRadiusM}
}

func (policy MatchPolicy) Validate() error {
	if math.IsNaN(policy.EndpointRadiusM) || math.IsInf(policy.EndpointRadiusM, 0) || policy.EndpointRadiusM <= 0 {
		return fmt.Errorf("official climb endpoint radius must be greater than zero")
	}
	return nil
}

func (climb OfficialClimb) validate() error {
	if strings.TrimSpace(climb.Name) == "" {
		return fmt.Errorf("official climb name is required")
	}
	if !validCoord(climb.StartCoord) || !validCoord(climb.EndCoord) {
		return fmt.Errorf("official climb coordinates are invalid")
	}
	return nil
}

func validCoord(coord geodist.Coord) bool {
	return coord.Lat >= -90 && coord.Lat <= 90 && coord.Lon >= -180 && coord.Lon <= 180
}