summaryrefslogtreecommitdiff
path: root/pkg/handler/location.go
blob: 9f4a2c141bc0dd365e2e986dfdf5296d31f52cb7 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package handler

import (
	"encoding/json"
	"time"

	"github.com/alexedwards/scs/v2"
	"github.com/labstack/echo/v4"
	"gitrepo.ru/neonxp/track/pkg/models"
	"go.etcd.io/bbolt"
	"go.neonxp.ru/objectid"
)

type Location struct {
	db       *bbolt.DB
	sessions *scs.SessionManager
}

func NewLocation(db *bbolt.DB, sessions *scs.SessionManager) *Location {
	return &Location{
		db:       db,
		sessions: sessions,
	}
}

func (l *Location) AddPoint(c echo.Context) error {
	uu := l.sessions.Get(c.Request().Context(), "user")
	if uu == nil {
		return echo.ErrForbidden
	}
	user, ok := uu.(*models.User)
	if !ok {
		return echo.ErrForbidden
	}

	req := new(PointArgs)
	if err := c.Bind(req); err != nil {
		return err
	}

	point := &models.Point{
		ID:        objectid.FromTime(req.Time),
		UserID:    user.ID,
		Lat:       req.Lat,
		Lon:       req.Lon,
		Time:      req.Time,
		Speed:     req.Speed,
		Direction: req.Direction,
		Accuracy:  req.Accuracy,
	}

	err := l.db.Update(func(tx *bbolt.Tx) error {
		points, err := tx.CreateBucketIfNotExists([]byte("points"))
		if err != nil {
			return err
		}
		jp, err := json.Marshal(point)
		if err != nil {
			return err
		}

		return points.Put(point.ID, jp)
	})
	if err != nil {
		return err
	}

	return c.NoContent(201)
}

func (l *Location) GetPoints(c echo.Context) error {
	// uu := l.sessions.Get(c.Request().Context(), "user")
	// if uu == nil {
	// 	return echo.ErrForbidden
	// }
	// user, ok := uu.(*models.User)
	// if !ok {
	// 	return echo.ErrForbidden
	// }

	pointsResp := []models.Point{}
	err := l.db.View(func(tx *bbolt.Tx) error {
		points := tx.Bucket([]byte("points"))
		point := new(models.Point)
		return points.ForEach(func(k, v []byte) error {
			if err := json.Unmarshal(v, point); err != nil {
				return err
			}
			// if slices.Equal(point.UserID, user.ID) {
			pointsResp = append(pointsResp, *point)
			// }
			return nil
		})
	})
	if err != nil {
		return err
	}

	return c.JSON(200, pointsResp)
}

func (l *Location) GetLast(c echo.Context) error {
	// uu := l.sessions.Get(c.Request().Context(), "user")
	// if uu == nil {
	// 	return echo.ErrForbidden
	// }
	// user, ok := uu.(*models.User)
	// if !ok {
	// 	return echo.ErrForbidden
	// }

	lastPoint := new(models.Point)
	err := l.db.View(func(tx *bbolt.Tx) error {
		points := tx.Bucket([]byte("points"))
		point := new(models.Point)
		return points.ForEach(func(k, v []byte) error {
			if err := json.Unmarshal(v, point); err != nil {
				return err
			}
			// if slices.Equal(point.UserID, user.ID) && lastPoint.Time.Before(point.Time) {
			if lastPoint.Time.Before(point.Time) {
				lastPoint = point
			}
			return nil
		})
	})
	if err != nil {
		return err
	}

	return c.JSON(200, lastPoint)
}

type PointArgs struct {
	Lat       float64   `query:"lat"`
	Lon       float64   `query:"lon"`
	Time      time.Time `query:"time"`
	Speed     float64   `query:"spd"`
	Direction float64   `query:"dir"`
	Accuracy  float64   `query:"acc"`
}