forked from ebhomengo/niki
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
|
package mysqlagent
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"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) GetAllAgent(ctx context.Context) ([]entity.Admin, error) {
|
||
|
const op = "mysqladmin.GetAllAgent"
|
||
|
|
||
|
query := `SELECT id, first_name, last_name, phone_number FROM admins WHERE role = 'agent' AND status = 'active'`
|
||
|
//nolint
|
||
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAdminAgentGetAll, query)
|
||
|
if err != nil {
|
||
|
return nil, richerror.New(op).WithErr(err).
|
||
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
||
|
}
|
||
|
|
||
|
rows, err := stmt.QueryContext(ctx)
|
||
|
if err != nil {
|
||
|
return nil, richerror.New(op).WithErr(err).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||
|
}
|
||
|
defer rows.Close()
|
||
|
|
||
|
agents := make([]entity.Admin, 0)
|
||
|
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
|
||
|
}
|