forked from ebhomengo/niki
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
|
package mysqladdress
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"time"
|
||
|
|
||
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
||
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||
|
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
|
||
|
)
|
||
|
|
||
|
func (d DB) GetAllCities(ctx context.Context) ([]entity.City, error) {
|
||
|
const op = "mysqladdress.GetAllCities"
|
||
|
|
||
|
cities := make([]entity.City, 0)
|
||
|
|
||
|
rows, err := d.conn.Conn().QueryContext(ctx, `SELECT * FROM cities;`)
|
||
|
if err != nil {
|
||
|
return nil, richerror.New(op).WithErr(err).
|
||
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||
|
}
|
||
|
|
||
|
defer rows.Close()
|
||
|
|
||
|
for rows.Next() {
|
||
|
city, err := scanCity(rows)
|
||
|
if err != nil {
|
||
|
return nil, richerror.New(op).WithErr(err).
|
||
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||
|
}
|
||
|
|
||
|
cities = append(cities, city)
|
||
|
}
|
||
|
|
||
|
if err := rows.Err(); err != nil {
|
||
|
return nil, richerror.New(op).WithErr(err).
|
||
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||
|
}
|
||
|
|
||
|
return cities, nil
|
||
|
}
|
||
|
|
||
|
func scanCity(scanner mysql.Scanner) (entity.City, error) {
|
||
|
var createdAt time.Time
|
||
|
var city entity.City
|
||
|
|
||
|
err := scanner.Scan(&city.ID, &city.ProvinceID, &city.Name, &createdAt)
|
||
|
|
||
|
return city, err
|
||
|
}
|