forked from ebhomengo/niki
1
0
Fork 0
niki/repository/mysql/kind_box/get.go

39 lines
1.1 KiB
Go

package mysqlkindbox
import (
"context"
"database/sql"
"errors"
"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) GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error) {
const op = "mysqlkindbox.GetKindBox"
query := `SELECT * FROM kind_boxes WHERE id = ? AND deleted_at IS NULL`
//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)
k, err := scanKindBox(row)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
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
}