feat&refact: add haversine algoritm and refact filed schema in address

This commit is contained in:
pahmiudahgede 2025-05-08 12:33:25 +07:00
parent 02db7d5dca
commit 47ccae326a
5 changed files with 34 additions and 15 deletions

View File

@ -11,7 +11,8 @@ type AddressResponseDTO struct {
Village string `json:"village"` Village string `json:"village"`
PostalCode string `json:"postalCode"` PostalCode string `json:"postalCode"`
Detail string `json:"detail"` Detail string `json:"detail"`
Geography string `json:"geography"` Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
CreatedAt string `json:"createdAt"` CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"` UpdatedAt string `json:"updatedAt"`
} }
@ -23,7 +24,8 @@ type CreateAddressDTO struct {
Village string `json:"village_id"` Village string `json:"village_id"`
PostalCode string `json:"postalCode"` PostalCode string `json:"postalCode"`
Detail string `json:"detail"` Detail string `json:"detail"`
Geography string `json:"geography"` Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
} }
func (r *CreateAddressDTO) ValidateAddress() (map[string][]string, bool) { func (r *CreateAddressDTO) ValidateAddress() (map[string][]string, bool) {
@ -49,8 +51,11 @@ func (r *CreateAddressDTO) ValidateAddress() (map[string][]string, bool) {
if strings.TrimSpace(r.Detail) == "" { if strings.TrimSpace(r.Detail) == "" {
errors["detail"] = append(errors["detail"], "Detail address is required") errors["detail"] = append(errors["detail"], "Detail address is required")
} }
if strings.TrimSpace(r.Geography) == "" { if strings.TrimSpace(r.Latitude) == "" {
errors["geography"] = append(errors["geography"], "Geographic coordinates are required") errors["latitude"] = append(errors["latitude"], "Geographic coordinates are required")
}
if strings.TrimSpace(r.Longitude) == "" {
errors["longitude"] = append(errors["longitude"], "Geographic coordinates are required")
} }
if len(errors) > 0 { if len(errors) > 0 {

1
go.mod
View File

@ -14,6 +14,7 @@ require (
) )
require ( require (
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26 // indirect
golang.org/x/term v0.30.0 // indirect golang.org/x/term v0.30.0 // indirect
rsc.io/qr v0.2.0 // indirect rsc.io/qr v0.2.0 // indirect
) )

2
go.sum
View File

@ -72,6 +72,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26 h1:UFHFmFfixpmfRBcxuu+LA9l8MdURWVdVNUHxO5n1d2w=
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26/go.mod h1:IGhd0qMDsUa9acVjsbsT7bu3ktadtGOHI79+idTew/M=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA=

View File

@ -60,7 +60,8 @@ func (s *addressService) CreateAddress(userID string, addressDTO dto.CreateAddre
Village: village.Name, Village: village.Name,
PostalCode: addressDTO.PostalCode, PostalCode: addressDTO.PostalCode,
Detail: addressDTO.Detail, Detail: addressDTO.Detail,
Geography: addressDTO.Geography, Latitude: addressDTO.Latitude,
Longitude: addressDTO.Longitude,
} }
err = s.AddressRepo.CreateAddress(&address) err = s.AddressRepo.CreateAddress(&address)
@ -83,7 +84,8 @@ func (s *addressService) CreateAddress(userID string, addressDTO dto.CreateAddre
Village: address.Village, Village: address.Village,
PostalCode: address.PostalCode, PostalCode: address.PostalCode,
Detail: address.Detail, Detail: address.Detail,
Geography: address.Geography, Latitude: address.Latitude,
Longitude: address.Longitude,
CreatedAt: createdAt, CreatedAt: createdAt,
UpdatedAt: updatedAt, UpdatedAt: updatedAt,
} }
@ -116,7 +118,8 @@ func (s *addressService) CreateAddress(userID string, addressDTO dto.CreateAddre
Village: addr.Village, Village: addr.Village,
PostalCode: addr.PostalCode, PostalCode: addr.PostalCode,
Detail: addr.Detail, Detail: addr.Detail,
Geography: addr.Geography, Latitude: addr.Latitude,
Longitude: addr.Longitude,
CreatedAt: createdAt, CreatedAt: createdAt,
UpdatedAt: updatedAt, UpdatedAt: updatedAt,
}) })
@ -152,7 +155,8 @@ func (s *addressService) GetAddressByUserID(userID string) ([]dto.AddressRespons
Village: addressData["village"].(string), Village: addressData["village"].(string),
PostalCode: addressData["postalCode"].(string), PostalCode: addressData["postalCode"].(string),
Detail: addressData["detail"].(string), Detail: addressData["detail"].(string),
Geography: addressData["geography"].(string), Latitude: addressData["latitude"].(string),
Longitude: addressData["longitude"].(string),
CreatedAt: addressData["createdAt"].(string), CreatedAt: addressData["createdAt"].(string),
UpdatedAt: addressData["updatedAt"].(string), UpdatedAt: addressData["updatedAt"].(string),
}) })
@ -181,7 +185,8 @@ func (s *addressService) GetAddressByUserID(userID string) ([]dto.AddressRespons
Village: address.Village, Village: address.Village,
PostalCode: address.PostalCode, PostalCode: address.PostalCode,
Detail: address.Detail, Detail: address.Detail,
Geography: address.Geography, Latitude: address.Latitude,
Longitude: address.Longitude,
CreatedAt: createdAt, CreatedAt: createdAt,
UpdatedAt: updatedAt, UpdatedAt: updatedAt,
}) })
@ -222,7 +227,8 @@ func (s *addressService) GetAddressByID(userID, id string) (*dto.AddressResponse
Village: addressData["village"].(string), Village: addressData["village"].(string),
PostalCode: addressData["postalCode"].(string), PostalCode: addressData["postalCode"].(string),
Detail: addressData["detail"].(string), Detail: addressData["detail"].(string),
Geography: addressData["geography"].(string), Latitude: addressData["latitude"].(string),
Longitude: addressData["longitude"].(string),
CreatedAt: addressData["createdAt"].(string), CreatedAt: addressData["createdAt"].(string),
UpdatedAt: addressData["updatedAt"].(string), UpdatedAt: addressData["updatedAt"].(string),
} }
@ -242,7 +248,8 @@ func (s *addressService) GetAddressByID(userID, id string) (*dto.AddressResponse
Village: address.Village, Village: address.Village,
PostalCode: address.PostalCode, PostalCode: address.PostalCode,
Detail: address.Detail, Detail: address.Detail,
Geography: address.Geography, Latitude: address.Latitude,
Longitude: address.Longitude,
CreatedAt: createdAt, CreatedAt: createdAt,
UpdatedAt: updatedAt, UpdatedAt: updatedAt,
} }
@ -295,7 +302,8 @@ func (s *addressService) UpdateAddress(userID, id string, addressDTO dto.CreateA
address.Village = village.Name address.Village = village.Name
address.PostalCode = addressDTO.PostalCode address.PostalCode = addressDTO.PostalCode
address.Detail = addressDTO.Detail address.Detail = addressDTO.Detail
address.Geography = addressDTO.Geography address.Latitude = addressDTO.Latitude
address.Longitude = addressDTO.Longitude
address.UpdatedAt = time.Now() address.UpdatedAt = time.Now()
err = s.AddressRepo.UpdateAddress(address) err = s.AddressRepo.UpdateAddress(address)
@ -321,7 +329,8 @@ func (s *addressService) UpdateAddress(userID, id string, addressDTO dto.CreateA
Village: address.Village, Village: address.Village,
PostalCode: address.PostalCode, PostalCode: address.PostalCode,
Detail: address.Detail, Detail: address.Detail,
Geography: address.Geography, Latitude: address.Latitude,
Longitude: address.Longitude,
CreatedAt: createdAt, CreatedAt: createdAt,
UpdatedAt: updatedAt, UpdatedAt: updatedAt,
} }
@ -353,7 +362,8 @@ func (s *addressService) UpdateAddress(userID, id string, addressDTO dto.CreateA
Village: addr.Village, Village: addr.Village,
PostalCode: addr.PostalCode, PostalCode: addr.PostalCode,
Detail: addr.Detail, Detail: addr.Detail,
Geography: addr.Geography, Latitude: addr.Latitude,
Longitude: addr.Longitude,
CreatedAt: createdAt, CreatedAt: createdAt,
UpdatedAt: updatedAt, UpdatedAt: updatedAt,
}) })

View File

@ -12,7 +12,8 @@ type Address struct {
Village string `gorm:"not null" json:"village"` Village string `gorm:"not null" json:"village"`
PostalCode string `gorm:"not null" json:"postalCode"` PostalCode string `gorm:"not null" json:"postalCode"`
Detail string `gorm:"not null" json:"detail"` Detail string `gorm:"not null" json:"detail"`
Geography string `gorm:"not null" json:"geography"` Latitude string `gorm:"not null" json:"latitude"`
Longitude string `gorm:"not null" json:"longitude"`
CreatedAt time.Time `gorm:"default:current_timestamp" json:"createdAt"` CreatedAt time.Time `gorm:"default:current_timestamp" json:"createdAt"`
UpdatedAt time.Time `gorm:"default:current_timestamp" json:"updatedAt"` UpdatedAt time.Time `gorm:"default:current_timestamp" json:"updatedAt"`
} }