Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
Coordinates can be modified
Browse files Browse the repository at this point in the history
  • Loading branch information
dreske committed Mar 29, 2022
1 parent 857562f commit 7d044ca
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 62 deletions.
2 changes: 2 additions & 0 deletions resources/db/migration/V1.3.0.1__add_fixed_coordinates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table centers
add fixed boolean default false;
5 changes: 3 additions & 2 deletions src/api/centers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"com.t-systems-mms.cwa/external/geocoding"
"com.t-systems-mms.cwa/repositories"
"com.t-systems-mms.cwa/services"
"context"
"encoding/csv"
"github.com/go-chi/chi"
"github.com/go-chi/jwtauth"
Expand Down Expand Up @@ -209,12 +210,12 @@ func (c *Centers) getAllCenters(_ http.ResponseWriter, r *http.Request) (interfa
}, nil
}

func (c *Centers) geocodeAllCenters(_ http.ResponseWriter, _ *http.Request) (interface{}, error) {
func (c *Centers) geocodeAllCenters(_ http.ResponseWriter, r *http.Request) (interface{}, error) {
centers, err := c.centersRepository.FindAll()
if err != nil {
return nil, err
}
go c.centersService.PerformGeocoding(centers)
go c.centersService.PerformGeocoding(context.Background(), centers)
return nil, nil
}

Expand Down
53 changes: 35 additions & 18 deletions src/api/model/centers.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,35 +149,36 @@ func MapToCenterDTOs(centers []domain.Center) []CenterDTO {
}

type EditCenterDTO struct {
UserReference *string `json:"userReference"`
Name string `json:"name" validate:"required"`
Email *string `json:"email" validate:"omitempty,email"`
Website *string `json:"website"`
Address string `json:"address" validate:"required"`
OpeningHours []string `json:"openingHours" validate:"dive,max=64"`
AddressNote *string `json:"addressNote"`
Appointment *string `json:"appointment" validate:"omitempty,oneof=Required NotRequired Possible"`
TestKinds []string `json:"testKinds" validate:"dive,oneof=Antigen PCR Vaccination Antibody"`
DCC *bool `json:"dcc"`
EnterDate *string `json:"enterDate"`
LeaveDate *string `json:"leaveDate"`
Note *string `json:"note"`
Visible *bool `json:"visible"`
LabId *string `json:"labId"`
OperatorName *string `json:"operatorName"`
UserReference *string `json:"userReference"`
Name string `json:"name" validate:"required"`
Email *string `json:"email" validate:"omitempty,email"`
Website *string `json:"website"`
Address string `json:"address" validate:"required"`
OpeningHours []string `json:"openingHours" validate:"dive,max=64"`
AddressNote *string `json:"addressNote"`
Appointment *string `json:"appointment" validate:"omitempty,oneof=Required NotRequired Possible"`
TestKinds []string `json:"testKinds" validate:"dive,oneof=Antigen PCR Vaccination Antibody"`
DCC *bool `json:"dcc"`
EnterDate *string `json:"enterDate"`
LeaveDate *string `json:"leaveDate"`
Note *string `json:"note"`
Visible *bool `json:"visible"`
LabId *string `json:"labId"`
OperatorName *string `json:"operatorName"`
Coordinates *CoordinatesDTO `json:"coordinates"`
}

func (c EditCenterDTO) CopyToDomain(dst *domain.Center) *domain.Center {
dst.EnterDate = nil
if util.IsNotNilOrEmpty(c.EnterDate) {
if date, err := time.Parse("02.01.2006", *c.EnterDate); err == nil {
if date, err := time.Parse("_2.1.2006", *c.EnterDate); err == nil {
dst.EnterDate = &date
}
}

dst.LeaveDate = nil
if util.IsNotNilOrEmpty(c.LeaveDate) {
if date, err := time.Parse("02.01.2006", *c.LeaveDate); err == nil {
if date, err := time.Parse("_2.1.2006", *c.LeaveDate); err == nil {
dst.LeaveDate = &date
}
}
Expand All @@ -200,6 +201,18 @@ func (c EditCenterDTO) CopyToDomain(dst *domain.Center) *domain.Center {
dst.Visible = &tmpTrue
}

if c.Coordinates != nil &&
c.Coordinates.Latitude != nil && *c.Coordinates.Latitude != 0.0 &&
c.Coordinates.Longitude != nil && *c.Coordinates.Longitude != 0.0 {
dst.Coordinates = domain.Coordinates{
Longitude: *c.Coordinates.Longitude,
Latitude: *c.Coordinates.Latitude,
Fixed: true,
}
} else {
dst.Coordinates = domain.Coordinates{Fixed: false}
}

return dst
}

Expand All @@ -224,6 +237,10 @@ func (EditCenterDTO) MapFromDomain(center domain.Center) EditCenterDTO {
Visible: center.Visible,
LabId: center.LabId,
OperatorName: center.OperatorName,
Coordinates: &CoordinatesDTO{
Longitude: &center.Longitude,
Latitude: &center.Latitude,
},
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/api/model/geolocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ func (b BoundsDTO) MapFromModel(bounds *domain.Bounds) *BoundsDTO {
}

type CoordinatesDTO struct {
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
Longitude *float64 `json:"longitude"`
Latitude *float64 `json:"latitude"`
}

func (c CoordinatesDTO) MapFromModel(coordinates *domain.Coordinates) *CoordinatesDTO {
if coordinates == nil {
return nil
}

c.Longitude = coordinates.Longitude
c.Latitude = coordinates.Latitude
c.Longitude = &coordinates.Longitude
c.Latitude = &coordinates.Latitude
return &c
}
1 change: 1 addition & 0 deletions src/domain/geolocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ type Bounds struct {
type Coordinates struct {
Longitude float64
Latitude float64
Fixed bool
}
44 changes: 10 additions & 34 deletions src/services/centers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var (
type Centers interface {
ImportCenters(ctx context.Context, centers []domain.Center, deleteAll bool) ([]domain.Center, error)
Save(ctx context.Context, center *domain.Center, geocoding bool) error
PerformGeocoding(centers []domain.Center)
PerformGeocoding(ctx context.Context, centers []domain.Center)
}

type centersService struct {
Expand Down Expand Up @@ -150,15 +150,15 @@ func (s *centersService) ImportCenters(ctx context.Context, centers []domain.Cen
return nil, err
}

go s.PerformGeocoding(centers)
go s.PerformGeocoding(context.Background(), centers)
return centers, err
}

func (s *centersService) GeocodeCenter(ctx context.Context, center *domain.Center) error {
logrus.WithFields(logrus.Fields{
"center": center.UUID,
"address": center.Address,
}).Trace("Geocoding center")
}).Info("Geocoding center")

g, err := s.geocoder.GetCoordinates(ctx, center.Address)
if err != nil {
Expand All @@ -177,9 +177,11 @@ func (s *centersService) GeocodeCenter(ctx context.Context, center *domain.Cente
} else {
center.Zip = &g.Zip
center.Region = &g.Region
center.Coordinates = domain.Coordinates{
Longitude: g.Coordinates.Longitude,
Latitude: g.Coordinates.Latitude,
if !center.Coordinates.Fixed {
center.Coordinates = domain.Coordinates{
Longitude: g.Coordinates.Longitude,
Latitude: g.Coordinates.Latitude,
}
}
}

Expand All @@ -190,7 +192,7 @@ func (s *centersService) GeocodeCenter(ctx context.Context, center *domain.Cente
return err
}

func (s *centersService) PerformGeocoding(centers []domain.Center) {
func (s *centersService) PerformGeocoding(ctx context.Context, centers []domain.Center) {
logrus.WithFields(logrus.Fields{
"count": len(centers),
}).Info("Starting geocoding of importet centers")
Expand All @@ -206,33 +208,7 @@ func (s *centersService) PerformGeocoding(centers []domain.Center) {
continue
}

g, err := s.geocoder.GetCoordinates(context.Background(), center.Address)
if err != nil {
logrus.
WithFields(logrus.Fields{
"center": center.UUID,
"address": center.Address,
}).
WithError(err).
Error("Error geocoding center")

if err == geocoding.ErrTooManyResults || err == geocoding.ErrNoResult {
msg := fmt.Sprintf("Geocoding: %s", err.Error())
center.Message = &msg
}
} else {
center.Zip = &g.Zip
center.Region = &g.Region
center.Coordinates = domain.Coordinates{
Longitude: g.Coordinates.Longitude,
Latitude: g.Coordinates.Latitude,
}
}

err = s.centersRepository.Save(context.Background(), &center)
if err != nil {
logrus.WithError(err).Error("Error saving center")
}
_ = s.GeocodeCenter(ctx, &center)
}
logrus.WithFields(logrus.Fields{
"count": len(centers),
Expand Down
32 changes: 28 additions & 4 deletions src/services/csvparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/lib/pq"
"github.com/sirupsen/logrus"
"io"
"strconv"
"strings"
"time"
)
Expand All @@ -55,6 +56,8 @@ const (
dccIndex = "Ausstellung eines Dicital Covid Zertifikates (DCC)"
noteIndex = "Adresshinweis"
visibleIndex = "Sichtbar"
latitudeIndex = "Breitengrad"
longitudeIndex = "Längengrad"
)

const (
Expand Down Expand Up @@ -94,6 +97,8 @@ func (c *CsvParser) Parse(reader io.Reader) ([]ImportCenterResult, error) {
dccIndex: fieldNotFound,
noteIndex: fieldNotFound,
visibleIndex: fieldNotFound,
latitudeIndex: fieldNotFound,
longitudeIndex: fieldNotFound,
}

headerRows := 0
Expand Down Expand Up @@ -234,7 +239,7 @@ func (c *CsvParser) parseCsvRow(entry []string, columnMappings map[string]int) I
var enterDate *time.Time
if index, hasColumn := columnMappings[enterDateIndex]; hasColumn && index > fieldNotFound {
if dateEntry := strings.TrimSpace(entry[index]); dateEntry != "" {
if date, err := time.Parse("_2._1.2006", dateEntry); err == nil {
if date, err := time.Parse("_2.1.2006", dateEntry); err == nil {
enterDate = &date
} else {
result.Errors = append(result.Errors, "invalid date: "+dateEntry)
Expand All @@ -245,7 +250,7 @@ func (c *CsvParser) parseCsvRow(entry []string, columnMappings map[string]int) I
var leaveDate *time.Time
if index, hasColumn := columnMappings[leaveDateIndex]; hasColumn && index > fieldNotFound {
if dateEntry := strings.TrimSpace(entry[index]); dateEntry != "" {
if date, err := time.Parse("_2._1.2006", dateEntry); err == nil {
if date, err := time.Parse("_2.1.2006", dateEntry); err == nil {
leaveDate = &date
} else {
result.Errors = append(result.Errors, "invalid date: "+dateEntry)
Expand All @@ -272,14 +277,33 @@ func (c *CsvParser) parseCsvRow(entry []string, columnMappings map[string]int) I
}
}

var latitude, longitude float64
if index, hasColumn := columnMappings[latitudeIndex]; hasColumn && index > fieldNotFound {
if entry := strings.TrimSpace(entry[index]); entry != "" {
latitude, _ = strconv.ParseFloat(entry, 64)
}
}

if index, hasColumn := columnMappings[longitudeIndex]; hasColumn && index > fieldNotFound {
if entry := strings.TrimSpace(entry[index]); entry != "" {
longitude, _ = strconv.ParseFloat(entry, 64)
}
}

fixedCoordinates := false
if latitude != 0.0 && longitude != 0.0 {
fixedCoordinates = true
}

result.Center = domain.Center{
UserReference: userReference,
Name: strings.TrimSpace(entry[columnMappings[nameIndex]]),
Email: email,
Website: website,
Coordinates: domain.Coordinates{
Longitude: 0,
Latitude: 0,
Longitude: longitude,
Latitude: latitude,
Fixed: fixedCoordinates,
},
Address: address,
AddressNote: note,
Expand Down

0 comments on commit 7d044ca

Please sign in to comment.