2024-01-18 10:16:36 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2024-01-19 16:37:15 +00:00
|
|
|
func (d *DB) GetAllCities(ctx context.Context) ([]entity.City, error) {
|
2024-01-18 10:16:36 +00:00
|
|
|
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
|
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
err := scanner.Scan(&city.ID, &city.Name, &city.ProvinceID, &createdAt)
|
2024-01-18 10:16:36 +00:00
|
|
|
|
|
|
|
return city, err
|
|
|
|
}
|