forked from ebhomengo/niki
1
0
Fork 0
niki/repository/mysql/address/get_all_cities.go

52 lines
1.3 KiB
Go
Raw Normal View History

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, updatedAt time.Time
2024-01-18 10:16:36 +00:00
var city entity.City
err := scanner.Scan(&city.ID, &city.Name, &city.ProvinceID, &createdAt, &updatedAt)
2024-01-18 10:16:36 +00:00
return city, err
}