package mysqladmin import ( "context" "database/sql" "errors" entity "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) AdminExistByPhoneNumber(ctx context.Context, phoneNumber string) (bool, error) { const op = "mysqlbenefactor.IsExistBenefactorByID" query := `select * from admins where phone_number = ?` //nolint stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAdminExistByPhoneNumber, query) if err != nil { return false, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected) } row := stmt.QueryRowContext(ctx, phoneNumber) _, err = scanAdmin(row) if err != nil { sErr := sql.ErrNoRows //TODO-errorsas: second argument to errors.As should not be *error //nolint if errors.As(err, &sErr) { return false, nil } // TODO - log unexpected error for better observability return false, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) } return true, nil } func (d *DB) AdminExistByEmail(ctx context.Context, email string) (bool, error) { const op = "mysqlbenefactor.IsExistBenefactorByID" query := `select * from admins where email = ?` //nolint stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAdminExistByEmail, query) if err != nil { return false, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected) } row := stmt.QueryRowContext(ctx, email) _, err = scanAdmin(row) if err != nil { sErr := sql.ErrNoRows //TODO-errorsas: second argument to errors.As should not be *error //nolint if errors.As(err, &sErr) { return false, nil } // TODO - log unexpected error for better observability return false, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) } return true, nil } func (d *DB) GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error) { const op = "mysqladmin.GetAdminByID" query := `select * from admins where id = ?` //nolint stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAdminGetByID, query) if err != nil { return entity.Admin{}, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected) } row := stmt.QueryRowContext(ctx, adminID) admin, err := scanAdmin(row) if err != nil { sErr := sql.ErrNoRows //TODO-errorsas: second argument to errors.As should not be *error //nolint if errors.As(err, &sErr) { return entity.Admin{}, nil } // TODO - log unexpected error for better observability return entity.Admin{}, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) } return admin, nil } func (d *DB) AgentExistByID(ctx context.Context, agentID uint) (bool, error) { const op = "mysqladmin.AgentExistByID" query := `select count(*) from admins where role = ? and id = ?` //nolint stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAdminAgentExistByID, query) if err != nil { return false, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected) } var count int err = stmt.QueryRowContext(ctx, entity.AdminAgentRole.String(), agentID).Scan(&count) if err != nil { return false, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) } return count > 0, nil }