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"
|
|
|
|
)
|
|
|
|
|
2024-01-19 16:37:15 +00:00
|
|
|
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
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
query := `SELECT * FROM provinces;`
|
|
|
|
//nolint
|
|
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyProvinceGetAll, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
2024-01-17 20:17:06 +00:00
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
rows, err := stmt.QueryContext(ctx)
|
2024-01-17 20:17:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
provinces := make([]entity.Province, 0)
|
2024-01-17 20:17:06 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
if err = rows.Err(); err != nil {
|
2024-01-17 20:17:06 +00:00
|
|
|
return nil, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return provinces, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func scanProvince(scanner mysql.Scanner) (entity.Province, error) {
|
2024-05-17 20:16:28 +00:00
|
|
|
var createdAt, updatedAt time.Time
|
2024-01-17 20:17:06 +00:00
|
|
|
var province entity.Province
|
|
|
|
|
2024-05-17 20:16:28 +00:00
|
|
|
err := scanner.Scan(&province.ID, &province.Name, &createdAt, &updatedAt)
|
2024-01-17 20:17:06 +00:00
|
|
|
|
|
|
|
return province, err
|
|
|
|
}
|