reprogl
37 строк · 595.0 Байт
1package repositories2
3import (4"database/sql"5"errors"6"net"7
8"xelbot.com/reprogl/models"9)
10
11type GeolocationRepository struct {12DB *sql.DB13}
14
15func (gr *GeolocationRepository) FindByIP(ip net.IP) (*models.Geolocation, error) {16query := `17SELECT
18gl.ip_long
19FROM geo_location AS gl
20WHERE gl.ip_addr = ?`
21
22location := &models.Geolocation{IpAddr: ip.String()}23
24err := gr.DB.QueryRow(query, ip.String()).Scan(25&location.ID,26)27
28if err != nil {29if errors.Is(err, sql.ErrNoRows) {30return nil, models.RecordNotFound31} else {32return nil, err33}34}35
36return location, nil37}
38