forked from ebhomengo/niki
1
0
Fork 0

feat(service): Get all province

This commit is contained in:
mohammad mahdi rezaei 2024-01-17 23:47:06 +03:30
parent 5c41903221
commit 3d9590000e
9 changed files with 171 additions and 10 deletions

View File

@ -0,0 +1,22 @@
package benefactorbasehandler
import (
"net/http"
addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4"
)
func (h Handler) GetAllProvinces(c echo.Context) error {
var req addressparam.GetAllProvincesRequest
listProvinces, err := h.addressSvc.GetAllProvinces(c.Request().Context(), req)
if err != nil {
msg, code := httpmsg.Error(err)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusOK, listProvinces)
}

View File

@ -0,0 +1,15 @@
package benefactorbasehandler
import (
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
)
type Handler struct {
addressSvc benefactoraddressservice.Service
}
func New(addressSvc benefactoraddressservice.Service) Handler {
return Handler{
addressSvc: addressSvc,
}
}

View File

@ -0,0 +1,9 @@
package benefactorbasehandler
import "github.com/labstack/echo/v4"
func (h Handler) SetRoutes(e *echo.Echo) {
r := e.Group("/base")
r.GET("/provinces", h.GetAllProvinces)
}

View File

@ -4,9 +4,11 @@ import (
"fmt" "fmt"
config "git.gocasts.ir/ebhomengo/niki/config" config "git.gocasts.ir/ebhomengo/niki/config"
benefactorbasehandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/base"
benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor" benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor"
benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req" benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor"
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req" benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor" benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
@ -19,7 +21,8 @@ type Server struct {
config config.Config config config.Config
Router *echo.Echo Router *echo.Echo
benefactorHandler benefactorhandler.Handler benefactorHandler benefactorhandler.Handler
benefactorkindboxreqhandler benefactorkindboxreqhandler.Handler benefactorKindBoxReqHandler benefactorkindboxreqhandler.Handler
benefactorBaseHandler benefactorbasehandler.Handler
} }
func New( func New(
@ -29,12 +32,14 @@ func New(
authSvc authservice.Service, authSvc authservice.Service,
benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, benefactorKindBoxReqSvc benefactorkindboxreqservice.Service,
benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator, benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator,
benefactorAddressSvc benefactoraddressservice.Service,
) Server { ) Server {
return Server{ return Server{
Router: echo.New(), Router: echo.New(),
config: cfg, config: cfg,
benefactorHandler: benefactorhandler.New(cfg.Auth, benefactorSvc, benefactorVld), benefactorHandler: benefactorhandler.New(cfg.Auth, benefactorSvc, benefactorVld),
benefactorkindboxreqhandler: benefactorkindboxreqhandler.New(cfg.Auth, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld), benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(cfg.Auth, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld),
benefactorBaseHandler: benefactorbasehandler.New(benefactorAddressSvc),
} }
} }
@ -46,7 +51,8 @@ func (s Server) Serve() {
// Routes // Routes
s.Router.GET("/health-check", s.healthCheck) s.Router.GET("/health-check", s.healthCheck)
s.benefactorHandler.SetRoutes(s.Router) s.benefactorHandler.SetRoutes(s.Router)
s.benefactorkindboxreqhandler.SetRoutes(s.Router) s.benefactorKindBoxReqHandler.SetRoutes(s.Router)
s.benefactorBaseHandler.SetRoutes(s.Router)
// Start server // Start server
address := fmt.Sprintf(":%d", s.config.HTTPServer.Port) address := fmt.Sprintf(":%d", s.config.HTTPServer.Port)

15
main.go
View File

@ -27,15 +27,16 @@ func main() {
mgr := migrator.New(cfg.Mysql) mgr := migrator.New(cfg.Mysql)
mgr.Up() mgr.Up()
authSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld := setupServices(cfg) authSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc := setupServices(cfg)
server := httpserver.New(cfg, benefactorSvc, benefactorVld, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld) server := httpserver.New(cfg, benefactorSvc, benefactorVld, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc)
server.Serve() server.Serve()
} }
//nolint:nakedret // we are sure of this //nolint:nakedret,gocritic // we are sure of this
func setupServices(cfg config.Config) ( func setupServices(cfg config.Config) (
authSvc authservice.Service, benefactorSvc benefactorservice.Service, benefactorVld benefactorvalidator.Validator, authSvc authservice.Service, benefactorSvc benefactorservice.Service, benefactorVld benefactorvalidator.Validator,
benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator, benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator,
benefactorAddressSvc benefactoraddressservice.Service,
) { ) {
authSvc = authservice.New(cfg.Auth) authSvc = authservice.New(cfg.Auth)
@ -52,13 +53,13 @@ func setupServices(cfg config.Config) (
benefactorVld = benefactorvalidator.New() benefactorVld = benefactorvalidator.New()
benefactorkindBoxReqMysql := mysqlkindboxreq.New(MysqlRepo) benefactorKindBoxReqMysql := mysqlkindboxreq.New(MysqlRepo)
benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorkindBoxReqMysql) benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorKindBoxReqMysql)
benefactorAddressMysql := mysqladdress.New(MysqlRepo) benefactorAddressMysql := mysqladdress.New(MysqlRepo)
benefactorAddressSvc := benefactoraddressservice.New(benefactorAddressMysql) benefactorAddressSvc = benefactoraddressservice.New(benefactorAddressMysql)
benefactorKindBoxReqVld = benefactorkindboxreqvalidator.New(benefactorkindBoxReqMysql, benefactorSvc, benefactorAddressSvc) benefactorKindBoxReqVld = benefactorkindboxreqvalidator.New(benefactorKindBoxReqMysql, benefactorSvc, benefactorAddressSvc)
return return
} }

View File

@ -0,0 +1,10 @@
package addressparam
import "git.gocasts.ir/ebhomengo/niki/entity"
type (
GetAllProvincesRequest struct{}
GetAllProvincesResponse struct {
Provinces []entity.Province `json:"provinces"`
}
)

View File

@ -0,0 +1,51 @@
package mysqladdress
import (
"context"
"time"
"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) GetAllProvinces(ctx context.Context) ([]entity.Province, error) {
const op = "mysqladdress.IsExistAddressByID"
provinces := make([]entity.Province, 0)
rows, err := d.conn.Conn().QueryContext(ctx, `SELECT * FROM provinces;`)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
defer rows.Close()
for rows.Next() {
province, err := scanProvince(rows)
if err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
provinces = append(provinces, province)
}
if err := rows.Err(); err != nil {
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return provinces, nil
}
func scanProvince(scanner mysql.Scanner) (entity.Province, error) {
var createdAt time.Time
var province entity.Province
err := scanner.Scan(&province.ID, &province.Name, &createdAt)
return province, err
}

View File

@ -0,0 +1,35 @@
-- +migrate Up
INSERT INTO `provinces` (`id`, `name`) VALUES (1, 'آذربایجان شرقی');
INSERT INTO `provinces` (`id`, `name`) VALUES (2, 'آذربایجان غربی');
INSERT INTO `provinces` (`id`, `name`) VALUES (3, 'اردبیل');
INSERT INTO `provinces` (`id`, `name`) VALUES (4, 'اصفهان');
INSERT INTO `provinces` (`id`, `name`) VALUES (5, 'البرز');
INSERT INTO `provinces` (`id`, `name`) VALUES (6, 'ایلام');
INSERT INTO `provinces` (`id`, `name`) VALUES (7, 'بوشهر');
INSERT INTO `provinces` (`id`, `name`) VALUES (8, 'تهران');
INSERT INTO `provinces` (`id`, `name`) VALUES (9, 'چهارمحال و بختیاری');
INSERT INTO `provinces` (`id`, `name`) VALUES (10, 'خراسان جنوبی');
INSERT INTO `provinces` (`id`, `name`) VALUES (11, 'خراسان رضوی');
INSERT INTO `provinces` (`id`, `name`) VALUES (12, 'خراسان شمالی');
INSERT INTO `provinces` (`id`, `name`) VALUES (13, 'خوزستان');
INSERT INTO `provinces` (`id`, `name`) VALUES (14, 'زنجان');
INSERT INTO `provinces` (`id`, `name`) VALUES (15, 'سمنان');
INSERT INTO `provinces` (`id`, `name`) VALUES (16, 'سیستان و بلوچستان');
INSERT INTO `provinces` (`id`, `name`) VALUES (17, 'فارس');
INSERT INTO `provinces` (`id`, `name`) VALUES (18, 'قزوین');
INSERT INTO `provinces` (`id`, `name`) VALUES (19, 'قم');
INSERT INTO `provinces` (`id`, `name`) VALUES (20, 'كردستان');
INSERT INTO `provinces` (`id`, `name`) VALUES (21, 'كرمان');
INSERT INTO `provinces` (`id`, `name`) VALUES (22, 'كرمانشاه');
INSERT INTO `provinces` (`id`, `name`) VALUES (23, 'کهگیلویه و بویراحمد');
INSERT INTO `provinces` (`id`, `name`) VALUES (24, 'گلستان');
INSERT INTO `provinces` (`id`, `name`) VALUES (25, 'گیلان');
INSERT INTO `provinces` (`id`, `name`) VALUES (26, 'لرستان');
INSERT INTO `provinces` (`id`, `name`) VALUES (27, 'مازندران');
INSERT INTO `provinces` (`id`, `name`) VALUES (28, 'مركزی');
INSERT INTO `provinces` (`id`, `name`) VALUES (29, 'هرمزگان');
INSERT INTO `provinces` (`id`, `name`) VALUES (30, 'همدان');
INSERT INTO `provinces` (`id`, `name`) VALUES (31, 'یزد');
-- +migrate Down
DELETE FROM `provinces` WHERE id BETWEEN '1' AND '31';

View File

@ -11,6 +11,7 @@ import (
type Repository interface { type Repository interface {
CreateBenefactorAddress(ctx context.Context, address entity.Address) (entity.Address, error) CreateBenefactorAddress(ctx context.Context, address entity.Address) (entity.Address, error)
GetAddressByID(ctx context.Context, id uint) (*entity.Address, error) GetAddressByID(ctx context.Context, id uint) (*entity.Address, error)
GetAllProvinces(ctx context.Context) ([]entity.Province, error)
} }
type Service struct { type Service struct {
@ -50,3 +51,14 @@ func (s Service) AddressExistByID(ctx context.Context, req param.GetAddressByIDR
return param.GetAddressByIDResponse{Address: address}, nil return param.GetAddressByIDResponse{Address: address}, nil
} }
func (s Service) GetAllProvinces(ctx context.Context, _ param.GetAllProvincesRequest) (param.GetAllProvincesResponse, error) {
const op = "benefactoraddressservice.GetAllProvinces"
provinces, err := s.repo.GetAllProvinces(ctx)
if err != nil {
return param.GetAllProvincesResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.GetAllProvincesResponse{Provinces: provinces}, nil
}