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

52 lines
1.3 KiB
Go
Raw Normal View History

2024-01-17 20:17:06 +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"
)
func (d DB) GetAllProvinces(ctx context.Context) ([]entity.Province, error) {
2024-01-18 10:16:36 +00:00
const op = "mysqladdress.GetAllProvinces"
2024-01-17 20:17:06 +00:00
provinces := make([]entity.Province, 0)
rows, err := d.conn.Conn().QueryContext(ctx, `SELECT * FROM provinces;`)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
defer rows.Close()
for rows.Next() {
province, err := scanProvince(rows)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
provinces = append(provinces, province)
}
if err := rows.Err(); err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return provinces, nil
}
func scanProvince(scanner mysql.Scanner) (entity.Province, error) {
var createdAt time.Time
var province entity.Province
err := scanner.Scan(&province.ID, &province.Name, &createdAt)
return province, err
}