forked from ebhomengo/niki
87 lines
2.5 KiB
Go
87 lines
2.5 KiB
Go
package mysql
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/domain/benefactor/entity"
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
|
"git.gocasts.ir/ebhomengo/niki/pkg/types"
|
|
)
|
|
|
|
func (d *DB) Create(ctx context.Context, b entity.Benefactor) (entity.Benefactor, error) {
|
|
const op = "repository.mysql.benefactor.create"
|
|
|
|
query := `INSERT INTO benefactors
|
|
(first_name, last_name, phone_number, description, email, gender, birthdate)
|
|
VALUES(?, ?, ?, ?, ?, ?, ?)`
|
|
|
|
res, err := d.conn.Conn().ExecContext(ctx, query,
|
|
b.FirstName, b.LastName, b.PhoneNumber,b.Description, b.Email, b.Gender, b.BirthDate)
|
|
|
|
if err != nil {
|
|
return entity.Benefactor{}, richerror.New(op).WithErr(err).
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
}
|
|
|
|
id, _ := res.LastInsertId()
|
|
b.ID = types.ID(id)
|
|
|
|
return b, nil
|
|
}
|
|
|
|
func (d *DB) GetBenefactorByID(ctx context.Context, benefactorID types.ID) (entity.Benefactor, error) {
|
|
const op = "repository.mysql.benefactor.getBenefactorById"
|
|
var b entity.Benefactor
|
|
|
|
query := `SELECT * FROM benefactors WHERE id = ?`
|
|
|
|
row := d.conn.Conn().QueryRowContext(ctx, query, benefactorID)
|
|
err := row.Scan(&b.ID, &b.FirstName, &b.LastName, &b.PhoneNumber,
|
|
&b.Description, &b.Email, b.Gender, b.BirthDate, b.Status)
|
|
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return entity.Benefactor{}, richerror.New(op).WithErr(err).
|
|
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindNotFound)
|
|
}
|
|
|
|
return entity.Benefactor{}, richerror.New(op).WithErr(err).
|
|
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
func (d *DB) Activate(ctx context.Context, benefactorID types.ID) error {
|
|
const op = "repository.mysql.benefactor.Activate"
|
|
|
|
query := `UPDATE benefactors SET status ='active' WHERE id = ?`
|
|
|
|
_, err := d.conn.Conn().ExecContext(ctx, query, benefactorID)
|
|
|
|
if err != nil {
|
|
return richerror.New(op).WithErr(err).
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *DB) Deactivate(ctx context.Context, benefactorID types.ID) error {
|
|
const op = "repository.mysql.benefactor.Deativate"
|
|
|
|
query := `UPDATE benefactors SET status ='inactive' WHERE id = ?`
|
|
|
|
_, err := d.conn.Conn().ExecContext(ctx, query, benefactorID)
|
|
|
|
if err != nil {
|
|
return richerror.New(op).WithErr(err).
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
}
|
|
|
|
return nil
|
|
}
|