2024-06-10 14:49:13 +00:00
|
|
|
package mysqladmin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-07-24 23:45:04 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
2024-06-10 14:49:13 +00:00
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
|
|
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d DB) GetAllAgent(ctx context.Context) ([]entity.Admin, error) {
|
|
|
|
const op = "mysqladmin.GetAllAgent"
|
|
|
|
|
|
|
|
agents := make([]entity.Admin, 0)
|
|
|
|
query := `SELECT id, first_name, last_name, phone_number FROM admins WHERE role = 'agent' AND status = 'active'`
|
|
|
|
rows, err := d.conn.Conn().QueryContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, richerror.New(op).WithErr(err).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var agent entity.Admin
|
|
|
|
sErr := rows.Scan(&agent.ID, &agent.FirstName, &agent.LastName, &agent.PhoneNumber)
|
|
|
|
if err != nil {
|
|
|
|
return nil, richerror.New(op).WithErr(sErr).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
agents = append(agents, agent)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rErr := rows.Err(); rErr != nil {
|
|
|
|
return nil, richerror.New(op).WithErr(rErr).
|
|
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return agents, nil
|
|
|
|
}
|