Skip to content

Commit

Permalink
fix: column types
Browse files Browse the repository at this point in the history
  • Loading branch information
hrshadhin committed Dec 15, 2022
1 parent b31fb56 commit 368d507
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 83 deletions.
21 changes: 5 additions & 16 deletions app/location/delivery/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,31 @@ package http
import (
"net/http"
"ot-recorder/app/model"
"ot-recorder/infrastructure/config"
"time"
)

type PingRequest struct {
Type string `json:"_type" validate:"required"`
Tst int64 `json:"tst" validate:"required"`
Acc int8 `json:"acc"`
Alt int8 `json:"alt"`
Acc int16 `json:"acc"`
Alt int16 `json:"alt"`
Batt int8 `json:"batt"`
Bs int8 `json:"bs"`
Lat float64 `json:"lat" validate:"required"`
Lon float64 `json:"lon" validate:"required"`
M int8 `json:"m"`
T string `json:"t"`
Tid string `json:"tid"`
Vac int8 `json:"vac"`
Vel int8 `json:"vel"`
Vac int16 `json:"vac"`
Vel int16 `json:"vel"`
Bssid string `json:"BSSID"`
Ssid string `json:"SSID"`
}

func mapLocationRequestToModel(req *PingRequest, headers *http.Header) *model.Location {
createdAt := parseEpochTimeToLocal(req.Tst, config.Get().App.TimeZone)
createdAtC := model.CustomDateTime(createdAt.Format("2006-01-02 15:04:05"))

return &model.Location{
Username: headers.Get("x-limit-u"),
Device: headers.Get("x-limit-d"),
CreatedAt: createdAtC,
CreatedAt: req.Tst,
Acc: req.Acc,
Alt: req.Alt,
Batt: req.Batt,
Expand All @@ -49,9 +44,3 @@ func mapLocationRequestToModel(req *PingRequest, headers *http.Header) *model.Lo
IP: headers.Get("X-Real-IP"),
}
}

func parseEpochTimeToLocal(epoch int64, tz string) time.Time {
et := time.Unix(epoch, 0)
loc, _ := time.LoadLocation(tz)
return et.In(loc)
}
5 changes: 2 additions & 3 deletions app/location/repository/mysql/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import (
)

func TestCreateLocation(t *testing.T) {
nowTime := time.Now().UTC().Format("2006-01-02 15:04:05")
l := &model.Location{
Username: "dev",
Device: "phoneAndroid",
CreatedAt: model.CustomDateTime(nowTime),
CreatedAt: time.Now().Unix(),
Acc: 13,
Alt: -42,
Batt: 40,
Expand Down Expand Up @@ -75,7 +74,7 @@ func TestGetUserLastLocation(t *testing.T) {
rows := sqlmock.NewRows([]string{
"id", "username", "device", "created_at", "acc", "alt", "batt", "bs", "lat", "lon", "m", "t", "tid", "vac",
"vel", "bssid", "ssid", "ip"}).
AddRow(1, "dev", "phoneAndroid", time.Now().UTC(), 13, -42, 40, 1, 23.0000000, 90.0000000, 1, "p",
AddRow(1, "dev", "phoneAndroid", time.Now().Unix(), 13, -42, 40, 1, 23.0000000, 90.0000000, 1, "p",
"p1", 1, 0, "", "", "")

query := "SELECT \\* FROM locations WHERE username = \\? ORDER BY created_at DESC LIMIT 1"
Expand Down
5 changes: 2 additions & 3 deletions app/location/repository/pgsql/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import (
)

func TestCreateLocation(t *testing.T) {
nowTime := time.Now().UTC().Format("2006-01-02 15:04:05")
l := &model.Location{
Username: "dev",
Device: "phoneAndroid",
CreatedAt: model.CustomDateTime(nowTime),
CreatedAt: time.Now().Unix(),
Acc: 13,
Alt: -42,
Batt: 40,
Expand Down Expand Up @@ -75,7 +74,7 @@ func TestGetUserLastLocation(t *testing.T) {
rows := sqlmock.NewRows([]string{
"id", "username", "device", "created_at", "acc", "alt", "batt", "bs", "lat", "lon", "m", "t", "tid", "vac",
"vel", "bssid", "ssid", "ip"}).
AddRow(1, "dev", "phoneAndroid", time.Now().UTC(), 13, -42, 40, 1, 23.0000000, 90.0000000, 1, "p",
AddRow(1, "dev", "phoneAndroid", time.Now().Unix(), 13, -42, 40, 1, 23.0000000, 90.0000000, 1, "p",
"p1", 1, 0, "", "", "")

query := "SELECT \\* FROM locations WHERE username = \\$1 ORDER BY created_at DESC LIMIT 1"
Expand Down
5 changes: 2 additions & 3 deletions app/location/repository/sqlite/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import (
)

func TestCreateLocation(t *testing.T) {
nowTime := time.Now().UTC().Format("2006-01-02 15:04:05")
l := &model.Location{
Username: "dev",
Device: "phoneAndroid",
CreatedAt: model.CustomDateTime(nowTime),
CreatedAt: time.Now().Unix(),
Acc: 13,
Alt: -42,
Batt: 40,
Expand Down Expand Up @@ -75,7 +74,7 @@ func TestGetUserLastLocation(t *testing.T) {
rows := sqlmock.NewRows([]string{
"id", "username", "device", "created_at", "acc", "alt", "batt", "bs", "lat", "lon", "m", "t", "tid", "vac",
"vel", "bssid", "ssid", "ip"}).
AddRow(1, "dev", "phoneAndroid", time.Now().UTC(), 13, -42, 40, 1, 23.0000000, 90.0000000, 1, "p",
AddRow(1, "dev", "phoneAndroid", time.Now().Unix(), 13, -42, 40, 1, 23.0000000, 90.0000000, 1, "p",
"p1", 1, 0, "", "", "")

query := "SELECT \\* FROM locations WHERE username = \\? ORDER BY created_at DESC LIMIT 1"
Expand Down
9 changes: 8 additions & 1 deletion app/location/usecase/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"ot-recorder/app/model"
"ot-recorder/app/response"
"ot-recorder/infrastructure/config"
"time"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -64,7 +65,7 @@ func (u *locationUsecase) LastLocation(
location = &model.LocationDetails{
Username: l.Username,
Device: l.Device,
DateTime: l.CreatedAt.String(),
DateTime: parseEpochTimeToLocal(l.CreatedAt, config.Get().App.TimeZone).Format("2006-01-02 15:04:05"),
Accuracy: l.Acc,
Altitude: l.Alt,
BatteryLevel: fmt.Sprintf("%d%s", l.Batt, "%"),
Expand All @@ -81,3 +82,9 @@ func (u *locationUsecase) LastLocation(

return location, nil
}

func parseEpochTimeToLocal(epoch int64, tz string) time.Time {
et := time.Unix(epoch, 0)
loc, _ := time.LoadLocation(tz)
return et.In(loc)
}
7 changes: 2 additions & 5 deletions app/location/usecase/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ import (

func TestCreateLocation(t *testing.T) {
mockLocationRepo := new(mocks.LocationRepository)
nowTime := time.Now().UTC().Format("2006-01-02 15:04:05")

mockLocation := model.Location{
Username: "dev",
Device: "phoneAndroid",
CreatedAt: model.CustomDateTime(nowTime),
CreatedAt: time.Now().Unix(),
Acc: 13,
Alt: -42,
Batt: 40,
Expand Down Expand Up @@ -53,12 +52,10 @@ func TestCreateLocation(t *testing.T) {

func TestGetUserLastLocation(t *testing.T) {
mockLocationRepo := new(mocks.LocationRepository)
nowTime := time.Now().UTC().Format("2006-01-02 15:04:05")

mockLocation := model.Location{
Username: "dev",
Device: "phoneAndroid",
CreatedAt: model.CustomDateTime(nowTime),
CreatedAt: time.Now().Unix(),
Acc: 13,
Alt: -42,
Batt: 40,
Expand Down
68 changes: 22 additions & 46 deletions app/model/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,42 @@ package model

import (
"context"
"database/sql/driver"
"time"
)

type CustomDateTime string

func (cdt CustomDateTime) Value() (driver.Value, error) {
return string(cdt), nil
}

func (cdt *CustomDateTime) Scan(value interface{}) error {
if t, ok := value.(time.Time); ok {
*cdt = CustomDateTime(t.Format("2006-01-02 15:04:05"))
} else if t, ok := value.(string); ok {
*cdt = CustomDateTime(t)
} else if t, ok := value.([]uint8); ok {
*cdt = CustomDateTime(t)
}

return nil
}

func (cdt CustomDateTime) String() string {
return string(cdt)
}

type Location struct {
ID int64 `json:"id"`
Username string `json:"username"`
Device string `json:"device"`
CreatedAt CustomDateTime `json:"created_at"`
Acc int8 `json:"acc"`
Alt int8 `json:"alt"`
Batt int8 `json:"batt"`
Bs int8 `json:"bs"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
M int8 `json:"m"`
T string `json:"t"`
Tid string `json:"tid"`
Vac int8 `json:"vac"`
Vel int8 `json:"vel"`
Bssid string `json:"bssid"`
Ssid string `json:"ssid"`
IP string `json:"ip"`
ID int64 `json:"id"`
Username string `json:"username"`
Device string `json:"device"`
CreatedAt int64 `json:"created_at"`
Acc int16 `json:"acc"`
Alt int16 `json:"alt"`
Batt int8 `json:"batt"`
Bs int8 `json:"bs"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
M int8 `json:"m"`
T string `json:"t"`
Tid string `json:"tid"`
Vac int16 `json:"vac"`
Vel int16 `json:"vel"`
Bssid string `json:"bssid"`
Ssid string `json:"ssid"`
IP string `json:"ip"`
}

type LocationDetails struct {
Username string `json:"username"`
Device string `json:"device"`
DateTime string `json:"date_time"`
Accuracy int8 `json:"accuracy,omitempty"`
Altitude int8 `json:"altitude,omitempty"`
Accuracy int16 `json:"accuracy,omitempty"`
Altitude int16 `json:"altitude,omitempty"`
BatteryLevel string `json:"battery_level,omitempty"`
BatteryStatus string `json:"battery_status,omitempty"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Mode string `json:"mode,omitempty"`
VerticalAccuracy int8 `json:"vertical_accuracy,omitempty"`
Velocity int8 `json:"velocity,omitempty"`
VerticalAccuracy int16 `json:"vertical_accuracy,omitempty"`
Velocity int16 `json:"velocity,omitempty"`
WifiName string `json:"wifi_name,omitempty"`
WifiMAC string `json:"wifi_mac,omitempty"`
IPAddress string `json:"ip_address"`
Expand Down
4 changes: 2 additions & 2 deletions infrastructure/db/migrations/mysql/000001_init_schema.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ CREATE TABLE `locations` (
`id` bigint PRIMARY KEY AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`device` varchar(20) NOT NULL,
`created_at` timestamp NOT NULL,
`created_at` bigint NOT NULL,
`acc` smallint,
`alt` smallint,
`batt` smallint,
`bs` smallint,
`lat` decimal(9,6) NOT NULL,
`lat` decimal(8,6) NOT NULL,
`lon` decimal(9,6) NOT NULL,
`m` smallint,
`t` varchar(1),
Expand Down
6 changes: 3 additions & 3 deletions infrastructure/db/migrations/pgsql/000001_init_schema.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ CREATE TABLE "locations" (
"id" bigserial PRIMARY KEY,
"username" varchar(20) NOT NULL,
"device" varchar(20) NOT NULL,
"created_at" TIMESTAMP(0) NOT NULL,
"created_at" bigint NOT NULL,
"acc" smallint,
"alt" smallint,
"batt" smallint,
"bs" smallint,
"lat" decimal(9,6) NOT NULL,
"lon" decimal(9,6) NOT NULL,
"lat" REAL NOT NULL,
"lon" REAL NOT NULL,
"m" smallint,
"t" varchar(1),
"tid" varchar(2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CREATE TABLE `locations` (
`id` INTEGER NOT NULL,
`username` TEXT NOT NULL,
`device` TEXT NOT NULL,
`created_at` TEXT NOT NULL,
`created_at` INTEGER NOT NULL,
`acc` INTEGER,
`alt` INTEGER,
`batt` INTEGER,
Expand Down

0 comments on commit 368d507

Please sign in to comment.