forked from ebhomengo/niki
1
0
Fork 0

refactor(repository): use prepared statements

This commit is contained in:
Ruhollah 2024-07-30 14:35:41 +03:30
parent 7cfd9b31e0
commit c9f2865aa5
44 changed files with 538 additions and 152 deletions

View File

@ -3,10 +3,12 @@ package mysqladdress
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) CreateBenefactorAddress(ctx context.Context, address entity.Address) (entity.Address, error) {
@ -18,8 +20,15 @@ func (d *DB) CreateBenefactorAddress(ctx context.Context, address entity.Address
}
address.ProvinceID = provinceID
// Insert the new address
res, err := d.conn.Conn().ExecContext(ctx, `INSERT INTO addresses (postal_code, address, lat, lon, name, city_id, province_id, benefactor_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
query := `INSERT INTO addresses (postal_code, address, lat, lon, name, city_id, province_id, benefactor_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAddressCreateForBenefactor, query)
if err != nil {
return entity.Address{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
res, err := stmt.ExecContext(ctx,
address.PostalCode, address.Address, address.Lat, address.Lon, address.Name, address.CityID, provinceID, address.BenefactorID)
if err != nil {
return entity.Address{}, richerror.New(op).WithErr(err).
@ -40,9 +49,17 @@ func (d *DB) CreateBenefactorAddress(ctx context.Context, address entity.Address
func (d *DB) getProvinceIDByCityID(ctx context.Context, cityID uint) (uint, error) {
const op = "mysqladdress.getProvinceIDByCityID"
query := `SELECT province_id FROM cities WHERE id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyCityGetProvinceIDByID, query)
if err != nil {
return 0, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
var provinceID uint
pErr := d.conn.Conn().QueryRowContext(ctx, `SELECT province_id FROM cities WHERE id = ?`, cityID).Scan(&provinceID)
if pErr != nil && pErr != sql.ErrNoRows {
pErr := stmt.QueryRowContext(ctx, cityID).Scan(&provinceID)
if pErr != nil && !errors.Is(pErr, sql.ErrNoRows) {
return 0, richerror.New(op).WithErr(pErr).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}

View File

@ -5,12 +5,21 @@ import (
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) DeleteBenefactorAddress(ctx context.Context, addressID, benefactorID uint) error {
const op = "mysqladdress.DeleteBenefactorAddress"
_, err := d.conn.Conn().ExecContext(ctx, `UPDATE addresses SET deleted_at = CURRENT_TIMESTAMP WHERE id = ? AND benefactor_id = ? AND deleted_at IS NULL`, addressID, benefactorID)
query := `UPDATE addresses SET deleted_at = CURRENT_TIMESTAMP WHERE id = ? AND benefactor_id = ? AND deleted_at IS NULL`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAddressDeleteForBenefactor, query)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err = stmt.ExecContext(ctx, addressID, benefactorID)
if err != nil {
return richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgCantDeleteAddress)
}

View File

@ -7,14 +7,22 @@ import (
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) IsExistAddressByID(ctx context.Context, addressID, benefactorID uint) (bool, error) {
const op = "mysqladdress.IsExistAddressByID"
row := d.conn.Conn().QueryRowContext(ctx, `select * from addresses where id = ? and benefactor_id = ? and deleted_at is null `, addressID, benefactorID)
query := `select * from addresses where id = ? and benefactor_id = ? and deleted_at is null `
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAddressIsExistByID, query)
if err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err := scanAddress(row)
row := stmt.QueryRowContext(ctx, addressID, benefactorID)
_, err = scanAddress(row)
if err != nil {
sErr := sql.ErrNoRows
//TODO-errorsas: second argument to errors.As should not be *error

View File

@ -7,13 +7,22 @@ import (
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) IsExistCityByID(ctx context.Context, id uint) (bool, error) {
const op = "mysqladdress.IsExistCityByID"
row := d.conn.Conn().QueryRowContext(ctx, `select * from cities where id = ?`, id)
_, err := scanCity(row)
query := `select * from cities where id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyCityIsExistByID, query)
if err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
row := stmt.QueryRowContext(ctx, id)
_, err = scanCity(row)
if err != nil {
sErr := sql.ErrNoRows
//TODO-errorsas: second argument to errors.As should not be *error

View File

@ -7,14 +7,22 @@ import (
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) IsExistProvinceByID(ctx context.Context, id uint) (bool, error) {
const op = "mysqladdress.IsExistProvinceByID"
row := d.conn.Conn().QueryRowContext(ctx, `select * from provinces where id = ?`, id)
query := `select * from provinces where id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyProvinceIsExistByID, query)
if err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err := scanProvince(row)
row := stmt.QueryRowContext(ctx, id)
_, err = scanProvince(row)
if err != nil {
sErr := sql.ErrNoRows
//TODO-errorsas: second argument to errors.As should not be *error

View File

@ -16,8 +16,15 @@ import (
func (d *DB) GetAddressByID(ctx context.Context, id uint) (*entity.Address, error) {
const op = "mysqladdress.IsExistAddressByID"
row := d.conn.Conn().QueryRowContext(ctx, `select * from addresses where id = ? and deleted_at is null`, id)
query := `select * from addresses where id = ? and deleted_at is null`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAddressGetByID, query)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
row := stmt.QueryRowContext(ctx, id)
address, err := scanAddress(row)
if err != nil {
sErr := sql.ErrNoRows

View File

@ -6,12 +6,21 @@ import (
"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) GetAddresses(ctx context.Context, benefactorID uint) ([]entity.Address, error) {
const op = "mysqladdress.GetAddresses"
rows, err := d.conn.Conn().QueryContext(ctx, `select * from addresses where benefactor_id = ? and deleted_at is null`, benefactorID)
query := `select * from addresses where benefactor_id = ? and deleted_at is null`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAddressGetAllByBenefactorID, query)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
rows, err := stmt.QueryContext(ctx, benefactorID)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)

View File

@ -13,16 +13,22 @@ import (
func (d *DB) GetAllCities(ctx context.Context) ([]entity.City, error) {
const op = "mysqladdress.GetAllCities"
cities := make([]entity.City, 0)
query := `SELECT * FROM cities;`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyCityGetAll, query)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
rows, err := d.conn.Conn().QueryContext(ctx, `SELECT * FROM cities;`)
rows, err := stmt.QueryContext(ctx)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
defer rows.Close()
cities := make([]entity.City, 0)
for rows.Next() {
city, err := scanCity(rows)
if err != nil {
@ -33,7 +39,7 @@ func (d *DB) GetAllCities(ctx context.Context) ([]entity.City, error) {
cities = append(cities, city)
}
if err := rows.Err(); err != nil {
if err = rows.Err(); err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}

View File

@ -13,16 +13,22 @@ import (
func (d *DB) GetAllProvinces(ctx context.Context) ([]entity.Province, error) {
const op = "mysqladdress.GetAllProvinces"
provinces := make([]entity.Province, 0)
query := `SELECT * FROM provinces;`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyProvinceGetAll, query)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
rows, err := d.conn.Conn().QueryContext(ctx, `SELECT * FROM provinces;`)
rows, err := stmt.QueryContext(ctx)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
defer rows.Close()
provinces := make([]entity.Province, 0)
for rows.Next() {
province, err := scanProvince(rows)
if err != nil {
@ -33,7 +39,7 @@ func (d *DB) GetAllProvinces(ctx context.Context) ([]entity.Province, error) {
provinces = append(provinces, province)
}
if err := rows.Err(); err != nil {
if err = rows.Err(); err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}

View File

@ -6,6 +6,7 @@ import (
"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) UpdateAddress(ctx context.Context, address entity.Address) error {
@ -19,7 +20,14 @@ func (d *DB) UpdateAddress(ctx context.Context, address entity.Address) error {
query := `UPDATE addresses
SET postal_code = ?, address = ?, lat = ?, lon = ?, name = ?, city_id = ?, province_id = ?
WHERE id = ? AND benefactor_id = ? AND deleted_at IS NULL`
_, uErr := d.conn.Conn().ExecContext(ctx, query, address.PostalCode, address.Address, address.Lat, address.Lon, address.Name, address.CityID, provinceID, address.ID, address.BenefactorID)
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAddressUpdate, query)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, uErr := stmt.ExecContext(ctx, address.PostalCode, address.Address, address.Lat, address.Lon, address.Name, address.CityID, provinceID, address.ID, address.BenefactorID)
if err != nil {
return richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord).
WithKind(richerror.KindUnexpected)

View File

@ -1,6 +1,7 @@
package mysqladmin
import (
"context"
"slices"
"time"
@ -13,18 +14,23 @@ import (
func (d *DB) GetAdminPermissions(adminID uint, role entity.AdminRole) ([]entity.AdminPermission, error) {
const op = "mysqladmin.GetAdminPermissions"
// get adminRoleACL
adminRoleACL := make([]entity.AdminAccessControl, 0)
query := `select * from admin_access_controls where actor_type = ? and actor_id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(context.Background(), mysql.StatementKeyAdminAccessControlGetPermissions, query)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
adminRoleRows, err := d.conn.Conn().Query(`select * from admin_access_controls where actor_type = ? and actor_id = ?`,
entity.AdminRoleActorType, role)
// Get admin role ACL
adminRoleRows, err := stmt.Query(entity.AdminRoleActorType, role)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
defer adminRoleRows.Close()
adminRoleACL := make([]entity.AdminAccessControl, 0)
for adminRoleRows.Next() {
acl, err := scanAccessControl(adminRoleRows)
if err != nil {
@ -35,23 +41,20 @@ func (d *DB) GetAdminPermissions(adminID uint, role entity.AdminRole) ([]entity.
adminRoleACL = append(adminRoleACL, acl)
}
if err := adminRoleRows.Err(); err != nil {
if err = adminRoleRows.Err(); err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
// get adminACL
adminACL := make([]entity.AdminAccessControl, 0)
adminRows, err := d.conn.Conn().Query(`select * from admin_access_controls where actor_type = ? and actor_id = ?`,
entity.AdminAdminActorType, adminID)
// Get admin ACL
adminRows, err := stmt.Query(entity.AdminAdminActorType, adminID)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
defer adminRows.Close()
adminACL := make([]entity.AdminAccessControl, 0)
for adminRows.Next() {
acl, err := scanAccessControl(adminRows)
if err != nil {
@ -62,7 +65,7 @@ func (d *DB) GetAdminPermissions(adminID uint, role entity.AdminRole) ([]entity.
adminACL = append(adminACL, acl)
}
if err := adminRows.Err(); err != nil {
if err = adminRows.Err(); err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}

View File

@ -6,15 +6,25 @@ import (
"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) AddAdmin(ctx context.Context, admin entity.Admin) (entity.Admin, error) {
func (d *DB) AddAdmin(ctx context.Context, admin entity.Admin) (entity.Admin, error) {
const op = "mysqladmin.AddAdmin"
res, err := d.conn.Conn().ExecContext(ctx, `insert into admins(first_name,last_name,password,phone_number,
role,description,email,gender,status) values (?,?,?,?,?,?,?,?,?)`,
admin.FirstName, admin.LastName, admin.Password, admin.PhoneNumber, admin.Role.String(), admin.Description, admin.Email,
admin.Gender.String(), admin.Status.String())
query := `insert into admins(first_name,last_name,password,phone_number,
role,description,email,gender,status) values (?,?,?,?,?,?,?,?,?)`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAdminAdd, query)
if err != nil {
return entity.Admin{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
res, err := stmt.ExecContext(ctx,
admin.FirstName, admin.LastName, admin.Password,
admin.PhoneNumber, admin.Role.String(), admin.Description,
admin.Email, admin.Gender.String(), admin.Status.String())
if err != nil {
return entity.Admin{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)

View File

@ -8,14 +8,22 @@ import (
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) {
func (d *DB) AdminExistByPhoneNumber(ctx context.Context, phoneNumber string) (bool, error) {
const op = "mysqlbenefactor.IsExistBenefactorByID"
row := d.conn.Conn().QueryRowContext(ctx, `select * from admins where phone_number = ?`, phoneNumber)
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)
}
_, err := scanAdmin(row)
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
@ -32,12 +40,19 @@ func (d DB) AdminExistByPhoneNumber(ctx context.Context, phoneNumber string) (bo
return true, nil
}
func (d DB) AdminExistByEmail(ctx context.Context, email string) (bool, error) {
func (d *DB) AdminExistByEmail(ctx context.Context, email string) (bool, error) {
const op = "mysqlbenefactor.IsExistBenefactorByID"
row := d.conn.Conn().QueryRowContext(ctx, `select * from admins where email = ?`, email)
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)
}
_, err := scanAdmin(row)
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
@ -54,11 +69,18 @@ func (d DB) AdminExistByEmail(ctx context.Context, email string) (bool, error) {
return true, nil
}
func (d DB) GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error) {
func (d *DB) GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error) {
const op = "mysqladmin.GetAdminByID"
row := d.conn.Conn().QueryRowContext(ctx, `select * from admins where id = ?`, adminID)
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
@ -76,12 +98,19 @@ func (d DB) GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error
return admin, nil
}
func (d DB) AgentExistByID(ctx context.Context, agentID uint) (bool, error) {
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 := d.conn.Conn().QueryRowContext(ctx, query, entity.AdminAgentRole.String(), agentID).Scan(&count)
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)

View File

@ -15,9 +15,16 @@ import (
func (d *DB) IsExistBenefactorByID(ctx context.Context, id uint) (bool, error) {
const op = "mysqlbenefactor.IsExistBenefactorByID"
row := d.conn.Conn().QueryRowContext(ctx, `select * from benefactors where id = ?`, id)
query := `select * from benefactors where id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyBenefactorIsExistByID, query)
if err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err := scanBenefactor(row)
row := stmt.QueryRowContext(ctx, id)
_, err = scanBenefactor(row)
if err != nil {
sErr := sql.ErrNoRows
//TODO-errorsas: second argument to errors.As should not be *error

View File

@ -12,11 +12,18 @@ import (
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
)
func (d DB) GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error) {
const op = "mysqlbenefactor.IsExistBenefactorByID"
func (d *DB) GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error) {
const op = "mysqladmin.GetAdminByPhoneNumber"
row := d.conn.Conn().QueryRowContext(ctx, `select * from admins where phone_number = ?`, phoneNumber)
query := `select * from admins where phone_number = ?`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAdminGetByPhoneNumber, query)
if err != nil {
return entity.Admin{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
row := stmt.QueryRowContext(ctx, phoneNumber)
admin, err := scanAdmin(row)
if err != nil {
sErr := sql.ErrNoRows

View File

@ -6,19 +6,27 @@ import (
"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) {
func (d *DB) GetAllAgent(ctx context.Context) ([]entity.Admin, error) {
const op = "mysqladmin.GetAllAgent"
agents := make([]entity.Admin, 0)
query := `SELECT id, first_name, last_name, phone_number FROM admins WHERE role = 'agent' AND status = 'active'`
rows, err := d.conn.Conn().QueryContext(ctx, query)
//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)

View File

@ -15,8 +15,15 @@ import (
func (d *DB) GetAddressByID(ctx context.Context, id uint) (*entity.Address, error) {
const op = "mysqladdress.IsExistAddressByID"
row := d.conn.Conn().QueryRowContext(ctx, `select * from addresses where id = ? and deleted_at is null`, id)
query := `select * from addresses where id = ? and deleted_at is null`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAddressGetByID, query)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
row := stmt.QueryRowContext(ctx, id)
address, err := scanAddress(row)
if err != nil {
sErr := sql.ErrNoRows

View File

@ -6,13 +6,21 @@ import (
"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) CreateBenefactor(ctx context.Context, benefactor entity.Benefactor) (entity.Benefactor, error) {
func (d *DB) CreateBenefactor(ctx context.Context, benefactor entity.Benefactor) (entity.Benefactor, error) {
const op = "mysqlbenefactor.CreateBenefactor"
res, err := d.conn.Conn().ExecContext(ctx, `insert into benefactors(phone_number) values(?)`,
benefactor.PhoneNumber)
query := `insert into benefactors(phone_number) values(?)`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyBenefactorCreate, query)
if err != nil {
return entity.Benefactor{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
res, err := stmt.ExecContext(ctx, benefactor.PhoneNumber)
if err != nil {
return entity.Benefactor{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)

View File

@ -12,11 +12,18 @@ import (
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
)
func (d DB) IsExistBenefactorByPhoneNumber(ctx context.Context, phoneNumber string) (bool, entity.Benefactor, error) {
func (d *DB) IsExistBenefactorByPhoneNumber(ctx context.Context, phoneNumber string) (bool, entity.Benefactor, error) {
const op = "mysqlbenefactor.IsExistBenefactorByPhoneNumber"
row := d.conn.Conn().QueryRowContext(ctx, `select * from benefactors where phone_number = ?`, phoneNumber)
query := `select * from benefactors where phone_number = ?`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyBenefactorIsExistByPhoneNumber, query)
if err != nil {
return false, entity.Benefactor{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
row := stmt.QueryRowContext(ctx, phoneNumber)
Benefactor, err := scanBenefactor(row)
if err != nil {
sErr := sql.ErrNoRows
@ -37,9 +44,16 @@ func (d DB) IsExistBenefactorByPhoneNumber(ctx context.Context, phoneNumber stri
func (d *DB) IsExistBenefactorByID(ctx context.Context, id uint) (bool, error) {
const op = "mysqlbenefactor.IsExistBenefactorByID"
row := d.conn.Conn().QueryRowContext(ctx, `select * from benefactors where id = ?`, id)
query := `select * from benefactors where id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyBenefactorIsExistByID, query)
if err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err := scanBenefactor(row)
row := stmt.QueryRowContext(ctx, id)
_, err = scanBenefactor(row)
if err != nil {
sErr := sql.ErrNoRows
//TODO-errorsas: second argument to errors.As should not be *error

View File

@ -1,6 +1,7 @@
package mysql
import (
"context"
"database/sql"
"fmt"
"sync"
@ -21,7 +22,7 @@ type DB struct {
config Config
db *querier.SQLDB
mu sync.Mutex
statements map[string]*sql.Stmt
statements map[statementKey]*sql.Stmt
}
func (db *DB) Conn() *querier.SQLDB {
@ -53,11 +54,11 @@ func New(config Config) *DB {
return &DB{
config: config,
db: &querier.SQLDB{DB: db},
statements: make(map[string]*sql.Stmt),
statements: make(map[statementKey]*sql.Stmt),
}
}
func (db *DB) PrepareStatement(key, query string) (*sql.Stmt, error) {
func (db *DB) PrepareStatement(ctx context.Context, key statementKey, query string) (*sql.Stmt, error) {
db.mu.Lock()
defer db.mu.Unlock()
@ -65,7 +66,7 @@ func (db *DB) PrepareStatement(key, query string) (*sql.Stmt, error) {
return stmt, nil
}
stmt, err := db.db.Prepare(query)
stmt, err := db.db.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
@ -85,7 +86,5 @@ func (db *DB) CloseStatements() error {
}
}
db.statements = make(map[string]*sql.Stmt)
return nil
}

View File

@ -8,13 +8,21 @@ import (
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
querytransaction "git.gocasts.ir/ebhomengo/niki/pkg/query_transaction/sql"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
)
func (d DB) AddKindBox(ctx context.Context, kindBox entity.KindBox) error {
func (d *DB) AddKindBox(ctx context.Context, kindBox entity.KindBox) error {
const op = "mysqlkindbox.AddKindBox"
_, err := d.conn.Conn().ExecContext(ctx, `insert into kind_boxes(kind_box_req_id,benefactor_id,type,status,sender_agent_id) values (?,?,?,?,?)`,
kindBox.KindBoxReqID, kindBox.BenefactorID, kindBox.KindBoxType, entity.KindBoxDeliveredStatus.String(), kindBox.SenderAgentID)
query := `insert into kind_boxes(kind_box_req_id,benefactor_id,type,status,sender_agent_id) values (?,?,?,?,?)`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxAdd, query)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err = stmt.ExecContext(ctx, kindBox.KindBoxReqID, kindBox.BenefactorID, kindBox.KindBoxType, entity.KindBoxDeliveredStatus.String(), kindBox.SenderAgentID)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)
@ -23,7 +31,7 @@ func (d DB) AddKindBox(ctx context.Context, kindBox entity.KindBox) error {
return nil
}
func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error {
func (d *DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error {
const op = "mysqlkindbox.AddBatchKindBoxConcurrentlyRollback"
queryStr := "INSERT INTO kind_boxes (kind_box_req_id, benefactor_id, type, serial_number, status ,deliver_refer_time_id,deliver_refer_date,deliver_address_id,sender_agent_id,delivered_at) VALUES "

View File

@ -6,14 +6,21 @@ import (
"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) AssignReceiverAgent(ctx context.Context, kindBox entity.KindBox) error {
func (d *DB) AssignReceiverAgent(ctx context.Context, kindBox entity.KindBox) error {
const op = "mysqlkindbox.AssignReceiverAgent"
query := `UPDATE kind_boxes SET receiver_agent_id = ?, status = ? WHERE status = ? AND id = ?`
_, err := d.conn.Conn().ExecContext(ctx, query,
kindBox.ReceiverAgentID, entity.KindBoxAssignedReceiverAgentStatus.String(), entity.KindBoxReadyToReturnStatus.String(), kindBox.ID)
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxAssignReceiverAgent, query)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err = stmt.ExecContext(ctx, kindBox.ReceiverAgentID, entity.KindBoxAssignedReceiverAgentStatus.String(), entity.KindBoxReadyToReturnStatus.String(), kindBox.ID)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)

View File

@ -6,13 +6,21 @@ import (
"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) EnumerateKindBox(ctx context.Context, kindBoxID, amount uint) error {
func (d *DB) EnumerateKindBox(ctx context.Context, kindBoxID, amount uint) error {
const op = "mysqlkindbox.EnumerateKindBox"
query := `UPDATE kind_boxes SET amount = ?, status = ? WHERE id = ?`
_, err := d.conn.Conn().ExecContext(ctx, query, amount, entity.KindBoxEnumeratedStatus.String(), kindBoxID)
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxEnumerate, query)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err = stmt.ExecContext(ctx, amount, entity.KindBoxEnumeratedStatus.String(), kindBoxID)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)

View File

@ -2,37 +2,28 @@ 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) BenefactorKindBoxExist(ctx context.Context, benefactorID, kindBoxID uint) (bool, error) {
func (d *DB) BenefactorKindBoxExist(ctx context.Context, benefactorID, kindBoxID uint) (bool, error) {
const op = "mysqlkindbox.BenefactorKindBoxExist"
var count int
if err := d.conn.Conn().QueryRowContext(ctx, `SELECT COUNT(*) FROM kind_boxes WHERE benefactor_id = ? AND id = ?`, benefactorID, kindBoxID).Scan(&count); err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return count > 0, nil
}
func (d DB) KindBoxExistForAgent(ctx context.Context, kindBoxID, agentID uint) (bool, error) {
const op = "mysqlkindbox.KindBoxExistForAgent"
query := `SELECT COUNT(*) FROM kind_boxes WHERE id = ? AND receiver_agent_id = ? AND status = ? AND deleted_at IS NULL`
query := `SELECT COUNT(*) FROM kind_boxes WHERE benefactor_id = ? AND id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(op, query)
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxExistForBenefactor, query)
if err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
var count int
if err = stmt.QueryRowContext(ctx, kindBoxID, agentID, entity.KindBoxAssignedReceiverAgentStatus.String()).Scan(&count); err != nil {
if err = stmt.QueryRowContext(ctx, benefactorID, kindBoxID).Scan(&count); err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
@ -40,14 +31,52 @@ func (d DB) KindBoxExistForAgent(ctx context.Context, kindBoxID, agentID uint) (
return count > 0, nil
}
func (d DB) KindBoxExist(ctx context.Context, kindBoxID uint) (bool, error) {
func (d *DB) KindBoxExistForAgent(ctx context.Context, kindBoxID, agentID uint) (bool, error) {
const op = "mysqlkindbox.KindBoxExistForAgent"
query := `SELECT * FROM kind_boxes WHERE id = ? AND receiver_agent_id = ? AND status = ? AND deleted_at IS NULL`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxGetAwaitingReturnByAgent, query)
if err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
row := stmt.QueryRowContext(ctx, kindBoxID, agentID, entity.KindBoxAssignedReceiverAgentStatus.String())
_, err = scanKindBox(row)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
return true, nil
}
func (d *DB) KindBoxExist(ctx context.Context, kindBoxID uint) (bool, error) {
const op = "mysqlkindbox.KindBoxExist"
var count int
if err := d.conn.Conn().QueryRowContext(ctx, `SELECT COUNT(*) FROM kind_boxes WHERE id = ?`, kindBoxID).Scan(&count); err != nil {
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 false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
return count > 0, nil
row := stmt.QueryRowContext(ctx, kindBoxID)
_, err = scanKindBox(row)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
return true, nil
}

View File

@ -8,13 +8,21 @@ import (
"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) {
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`
row := d.conn.Conn().QueryRowContext(ctx, query, kindBoxID)
//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) {

View File

@ -10,7 +10,7 @@ import (
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (d DB) GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBox, uint, error) {
func (d *DB) GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBox, uint, error) {
const op = "mysqlkindbox.GetAllKindBox"
baseQuery := `SELECT * FROM kind_boxes WHERE deleted_at IS NULL`

View File

@ -8,14 +8,15 @@ import (
"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) GetAwaitingReturnByAgent(ctx context.Context, kindBoxID, agentID uint) (entity.KindBox, error) {
func (d *DB) GetAwaitingReturnByAgent(ctx context.Context, kindBoxID, agentID uint) (entity.KindBox, error) {
const op = "mysqlkindbox.GetAwaitingReturnByAgent"
query := `SELECT * FROM kind_boxes WHERE id = ? AND receiver_agent_id = ? AND status = ? AND deleted_at IS NULL`
//nolint
stmt, err := d.conn.PrepareStatement(op, query)
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxGetAwaitingReturnByAgent, query)
if err != nil {
return entity.KindBox{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)

View File

@ -6,16 +6,23 @@ import (
"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) RegisterEmptyingRequestForKindBox(ctx context.Context, kindBox entity.KindBox) error {
func (d *DB) RegisterEmptyingRequestForKindBox(ctx context.Context, kindBox entity.KindBox) error {
const op = "mysqlkindbox.RegisterEmptyingRequest"
query := `UPDATE kind_boxes
SET return_address_id = ?, return_refer_time_id = ?, return_refer_date = ?, status = ?
WHERE id = ? AND benefactor_id = ? AND status = ? AND deleted_at IS NULL`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxRegisterEmptyingRequest, query)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err := d.conn.Conn().ExecContext(ctx, query,
_, err = stmt.ExecContext(ctx,
kindBox.ReturnAddressID, kindBox.ReturnReferTimeID, kindBox.ReturnReferDate, kindBox.Status.String(),
kindBox.ID, kindBox.BenefactorID, entity.KindBoxDeliveredStatus.String())
if err != nil {

View File

@ -7,14 +7,15 @@ import (
"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) ReturnKindBox(ctx context.Context, kindBoxID uint, serialNumber string) error {
func (d *DB) ReturnKindBox(ctx context.Context, kindBoxID uint, serialNumber string) error {
const op = "mysqlkindbox.ReturnKindBox"
query := `update kind_boxes set serial_number = ?, status = ?, returned_at = ? where id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(op, query)
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReturn, query)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)

View File

@ -6,12 +6,21 @@ import (
"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) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) error {
op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq")
_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set count_accepted = ? , status = ? where id = ?`,
countAccepted, entity.KindBoxReqAcceptedStatus.String(), kindBoxReqID)
func (d *DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) error {
const op = "mysqlkindboxreq.AcceptKindBoxReq"
query := `update kind_box_reqs set count_accepted = ? , status = ? where id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReqAccept, query)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err = stmt.ExecContext(ctx, countAccepted, entity.KindBoxReqAcceptedStatus.String(), kindBoxReqID)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)

View File

@ -6,13 +6,24 @@ import (
"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) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) {
func (d *DB) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) {
const op = "mysqlkindboxreq.AddKindBoxReq"
res, err := d.conn.Conn().ExecContext(ctx, `insert into kind_box_reqs(benefactor_id,kind_box_type,deliver_address_id,count_requested,deliver_refer_date,deliver_refer_time_id,status) values (?,?,?,?,?,?,?)`,
kindBoxReq.BenefactorID, kindBoxReq.KindBoxType.String(), kindBoxReq.DeliverAddressID, kindBoxReq.CountRequested, kindBoxReq.DeliverReferDate, kindBoxReq.DeliverReferTimeID, kindBoxReq.Status.String())
query := `insert into kind_box_reqs(benefactor_id,kind_box_type,deliver_address_id,count_requested,deliver_refer_date,deliver_refer_time_id,status) values (?,?,?,?,?,?,?)`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReqAdd, query)
if err != nil {
return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
res, err := stmt.ExecContext(ctx,
kindBoxReq.BenefactorID, kindBoxReq.KindBoxType.String(),
kindBoxReq.DeliverAddressID, kindBoxReq.CountRequested, kindBoxReq.DeliverReferDate,
kindBoxReq.DeliverReferTimeID, kindBoxReq.Status.String())
if err != nil {
return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)

View File

@ -6,12 +6,21 @@ import (
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) AssignSenderAgentToKindBoxReq(ctx context.Context, kindBoxReqID, senderAgentID uint) error {
func (d *DB) AssignSenderAgentToKindBoxReq(ctx context.Context, kindBoxReqID, senderAgentID uint) error {
const op = "mysqlkindboxreq.AssignSenderAgentToKindBoxReq"
_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set sender_agent_id = ?, status = ? where id = ?`,
query := `update kind_box_reqs set sender_agent_id = ?, status = ? where id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReqAssignSenderAgent, query)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err = stmt.ExecContext(ctx,
senderAgentID, entity.KindBoxReqAssignedSenderAgentStatus.String(), kindBoxReqID)
if err != nil {
return richerror.New(op).WithErr(err).

View File

@ -6,13 +6,23 @@ import (
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) DeleteKindBoxReqByID(ctx context.Context, kindBoxReqID uint) error {
const op = richerror.Op("mysqlkindboxreq.DeleteKindBoxReqByID")
_, dErr := d.conn.Conn().ExecContext(ctx, "update kind_box_reqs set deleted_at = ? where id = ? and deleted_at is null", time.Now(), kindBoxReqID)
if dErr != nil {
return richerror.New(op).WithErr(dErr).
func (d *DB) DeleteKindBoxReqByID(ctx context.Context, kindBoxReqID uint) error {
const op = "mysqlkindboxreq.DeleteKindBoxReqByID"
query := `update kind_box_reqs set deleted_at = ? where id = ? and deleted_at is null`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReqDeleteByID, query)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err = stmt.ExecContext(ctx, time.Now(), kindBoxReqID)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}

View File

@ -10,7 +10,7 @@ import (
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (d DB) DeliverKindBoxReq(ctx context.Context, kindBoxReqID uint) error {
func (d *DB) DeliverKindBoxReq(ctx context.Context, kindBoxReqID uint) error {
const op = "mysqlkindboxreq.DeliverKindBoxReq"
q, cErr := querier.GetQuerierFromContextOrNew(ctx).Continue(ctx, d.conn.Conn())
if cErr != nil {

View File

@ -9,9 +9,10 @@ import (
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
querier "git.gocasts.ir/ebhomengo/niki/pkg/query_transaction/sql"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
)
func (d DB) GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) {
func (d *DB) GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) {
op := richerror.Op("mysqlkindboxreq.GetByID")
q, cErr := querier.GetQuerierFromContextOrNew(ctx).Continue(ctx, d.conn.Conn())
if cErr != nil {
@ -28,10 +29,18 @@ func (d DB) GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) {
return k, nil
}
func (d DB) GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) {
op := richerror.Op("mysqlkindboxreq.GetKindBoxReqByID")
row := d.conn.Conn().QueryRowContext(ctx,
"select * from kind_box_reqs where id = ? and deleted_at is null", kindBoxReqID)
func (d *DB) GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) {
const op = "mysqlkindboxreq.GetKindBoxReqByID"
query := `select * from kind_box_reqs where id = ? and deleted_at is null`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReqGetByID, query)
if err != nil {
return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
row := stmt.QueryRowContext(ctx, kindBoxReqID)
k, err := scanKindBoxReq(row)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {

View File

@ -10,7 +10,7 @@ import (
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (d DB) GetAllKindBoxReq(ctx context.Context, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest) ([]entity.KindBoxReq, uint, error) {
func (d *DB) GetAllKindBoxReq(ctx context.Context, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest) ([]entity.KindBoxReq, uint, error) {
const op = "mysqlkindboxreq.GetAllKindBoxReq"
baseQuery := `SELECT * FROM kind_box_reqs WHERE deleted_at IS NULL`

View File

@ -8,13 +8,21 @@ import (
"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) GetAwaitingDeliveryByAgent(ctx context.Context, kindBoxReqID, agentID uint) (entity.KindBoxReq, error) {
func (d *DB) GetAwaitingDeliveryByAgent(ctx context.Context, kindBoxReqID, agentID uint) (entity.KindBoxReq, error) {
const op = "mysqlkindboxreq.GetAwaitingDeliveryByAgent"
query := `SELECT * FROM kind_box_reqs WHERE id = ? AND sender_agent_id = ? AND status = ? AND deleted_at IS NULL `
row := d.conn.Conn().QueryRowContext(ctx, query, kindBoxReqID, agentID, entity.KindBoxReqAssignedSenderAgentStatus.String())
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReqGetAwaitingDeliveryByAgent, query)
if err != nil {
return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
row := stmt.QueryRowContext(ctx, kindBoxReqID, agentID, entity.KindBoxReqAssignedSenderAgentStatus.String())
k, err := scanKindBoxReq(row)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {

View File

@ -8,14 +8,15 @@ import (
"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) KindBoxRequestExist(id uint) (bool, error) {
func (d *DB) KindBoxRequestExist(ctx context.Context, id uint) (bool, error) {
const op = "mysqlkindboxreq.KindBoxRequestExist"
query := `select * from kind_box_reqs where id = ?`
query := `select * from kind_box_reqs where id = ? and deleted_at is null`
//nolint
stmt, err := d.conn.PrepareStatement(op, query)
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReqGetByID, query)
if err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
@ -35,9 +36,18 @@ func (d DB) KindBoxRequestExist(id uint) (bool, error) {
return true, nil
}
func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error {
op := richerror.Op("mysqlkindboxreq.RollbackKindBoxRequestStatus")
_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set status = ? where id = ?`, entity.KindBoxReqPendingStatus.String(), id)
func (d *DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error {
const op = "mysqlkindboxreq.RollbackKindBoxRequestStatus"
query := `update kind_box_reqs set status = ? where id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReqRollbackToPendingStatus, query)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err = stmt.ExecContext(ctx, entity.KindBoxReqPendingStatus.String(), id)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)

View File

@ -6,12 +6,21 @@ import (
"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) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error {
op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq")
_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set description = ? , status = ? where id = ?`,
description, entity.KindBoxReqRejectedStatus.String(), kindBoxReqID)
func (d *DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error {
const op = "mysqlkindboxreq.RejectKindBoxReq"
query := `update kind_box_reqs set description = ? , status = ? where id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReqReject, query)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err = stmt.ExecContext(ctx, description, entity.KindBoxReqRejectedStatus.String(), kindBoxReqID)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)

View File

@ -8,9 +8,10 @@ import (
"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) UpdateKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) error {
func (d *DB) UpdateKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) error {
const op = "mysqlkindboxreq.UpdateKindBoxReq"
var (
@ -33,7 +34,14 @@ func (d DB) UpdateKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq)
description = ?, status = ?, deliver_refer_time_id = ?, deliver_refer_date = ?,
deliver_address_id = ?, sender_agent_id = ?, delivered_at = ?
WHERE id = ? AND deleted_at IS NULL`
_, uErr := d.conn.Conn().ExecContext(ctx, query, kindBoxReq.BenefactorID, kindBoxReq.KindBoxType,
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReqUpdate, query)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, uErr := stmt.ExecContext(ctx, kindBoxReq.BenefactorID, kindBoxReq.KindBoxType,
kindBoxReq.CountRequested, kindBoxReq.CountAccepted, kindBoxReq.Description, kindBoxReq.Status,
kindBoxReq.DeliverReferTimeID, kindBoxReq.DeliverReferDate, kindBoxReq.DeliverAddressID,
senderAgentID, deliveredAt, kindBoxReq.ID)

View File

@ -0,0 +1,46 @@
package mysql
type statementKey uint
const (
StatementKeyAddressCreateForBenefactor statementKey = iota + 1
StatementKeyAddressDeleteForBenefactor
StatementKeyAddressIsExistByID
StatementKeyAddressGetByID
StatementKeyAddressGetAllByBenefactorID
StatementKeyAddressUpdate
StatementKeyCityGetProvinceIDByID
StatementKeyCityIsExistByID
StatementKeyCityGetAll
StatementKeyProvinceIsExistByID
StatementKeyProvinceGetAll
StatementKeyAdminAccessControlGetPermissions
StatementKeyAdminAdd
StatementKeyAdminExistByEmail
StatementKeyAdminExistByPhoneNumber
StatementKeyAdminAgentExistByID
StatementKeyAdminGetByID
StatementKeyAdminGetByPhoneNumber
StatementKeyAdminAgentGetAll
StatementKeyBenefactorIsExistByID
StatementKeyBenefactorIsExistByPhoneNumber
StatementKeyBenefactorCreate
StatementKeyKindBoxAdd
StatementKeyKindBoxAssignReceiverAgent
StatementKeyKindBoxEnumerate
StatementKeyKindBoxExistForBenefactor
StatementKeyKindBoxGetByID
StatementKeyKindBoxGetAwaitingReturnByAgent
StatementKeyKindBoxRegisterEmptyingRequest
StatementKeyKindBoxReturn
StatementKeyKindBoxReqAccept
StatementKeyKindBoxReqAdd
StatementKeyKindBoxReqAssignSenderAgent
StatementKeyKindBoxReqDeleteByID
StatementKeyKindBoxReqGetByID
StatementKeyKindBoxReqGetAwaitingDeliveryByAgent
StatementKeyKindBoxReqRollbackToPendingStatus
StatementKeyKindBoxReqReject
StatementKeyKindBoxReqUpdate
StatementKeyReferTimeGetByID
)

View File

@ -6,12 +6,21 @@ import (
"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) Get(ctx context.Context, referTimeID uint) (entity.ReferTime, error) {
func (d *DB) Get(ctx context.Context, referTimeID uint) (entity.ReferTime, error) {
const op = richerror.Op("mysqlrefertime.Get")
row := d.conn.Conn().QueryRowContext(ctx, `select * from refer_times where id = ?`, referTimeID)
query := `select * from refer_times where id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyReferTimeGetByID, query)
if err != nil {
return entity.ReferTime{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
row := stmt.QueryRowContext(ctx, referTimeID)
r, err := scanReferTime(row)
if err != nil {
return entity.ReferTime{}, richerror.New(op).WithErr(err).

View File

@ -22,7 +22,7 @@ const (
)
type Repository interface {
KindBoxRequestExist(id uint) (bool, error)
KindBoxRequestExist(ctx context.Context, id uint) (bool, error)
GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error)
}
@ -56,7 +56,7 @@ func (v Validator) doesKindBoxRequestExist(value interface{}) error {
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
if isExist, err := v.repo.KindBoxRequestExist(kindboxreqID); !isExist || err != nil {
if isExist, err := v.repo.KindBoxRequestExist(context.Background(), kindboxreqID); !isExist || err != nil {
if err != nil {
return err
}

View File

@ -34,7 +34,7 @@ type ReferTimeSvc interface {
}
type Repository interface {
KindBoxRequestExist(id uint) (bool, error)
KindBoxRequestExist(ctx context.Context, id uint) (bool, error)
GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error)
}
@ -215,7 +215,7 @@ func (v Validator) doesKindBoxRequestExist(value interface{}) error {
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
if isExist, err := v.repo.KindBoxRequestExist(kindboxreqID); !isExist || err != nil {
if isExist, err := v.repo.KindBoxRequestExist(context.Background(), kindboxreqID); !isExist || err != nil {
if err != nil {
return err
}