2024-06-25 21:58:11 +00:00
|
|
|
package mysqlkindbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2024-07-24 23:45:04 +00:00
|
|
|
"errors"
|
2024-06-25 21:58:11 +00:00
|
|
|
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
|
|
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
2024-07-30 11:05:41 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
|
2024-06-25 21:58:11 +00:00
|
|
|
)
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
func (d *DB) GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error) {
|
2024-06-25 21:58:11 +00:00
|
|
|
const op = "mysqlkindbox.GetKindBox"
|
|
|
|
|
|
|
|
query := `SELECT * FROM kind_boxes WHERE id = ? AND deleted_at IS NULL`
|
2024-07-30 11:05:41 +00:00
|
|
|
//nolint
|
|
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxGetByID, query)
|
|
|
|
if err != nil {
|
|
|
|
return entity.KindBox{}, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
row := stmt.QueryRowContext(ctx, kindBoxID)
|
2024-06-25 21:58:11 +00:00
|
|
|
k, err := scanKindBox(row)
|
|
|
|
if err != nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2024-06-25 21:58:11 +00:00
|
|
|
return entity.KindBox{}, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entity.KindBox{}, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return k, nil
|
|
|
|
}
|