From 3d9590000eb90d2351e2ab451da9908a5cf648f4 Mon Sep 17 00:00:00 2001 From: mohammad mahdi rezaei Date: Wed, 17 Jan 2024 23:47:06 +0330 Subject: [PATCH] feat(service): Get all province --- .../benefactor/base/get_all_provinces.go | 22 ++++++++ .../http_server/benefactor/base/handler.go | 15 ++++++ delivery/http_server/benefactor/base/route.go | 9 ++++ delivery/http_server/server.go | 12 +++-- main.go | 15 +++--- param/benefactor/address/get_all_provinces.go | 10 ++++ repository/mysql/address/get_all_provinces.go | 51 +++++++++++++++++++ .../1705510025_seeder_add_provinces_table.sql | 35 +++++++++++++ service/benefactor/address/service.go | 12 +++++ 9 files changed, 171 insertions(+), 10 deletions(-) create mode 100644 delivery/http_server/benefactor/base/get_all_provinces.go create mode 100644 delivery/http_server/benefactor/base/handler.go create mode 100644 delivery/http_server/benefactor/base/route.go create mode 100644 param/benefactor/address/get_all_provinces.go create mode 100644 repository/mysql/address/get_all_provinces.go create mode 100644 repository/mysql/migration/1705510025_seeder_add_provinces_table.sql diff --git a/delivery/http_server/benefactor/base/get_all_provinces.go b/delivery/http_server/benefactor/base/get_all_provinces.go new file mode 100644 index 0000000..881aa79 --- /dev/null +++ b/delivery/http_server/benefactor/base/get_all_provinces.go @@ -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) +} diff --git a/delivery/http_server/benefactor/base/handler.go b/delivery/http_server/benefactor/base/handler.go new file mode 100644 index 0000000..833d21e --- /dev/null +++ b/delivery/http_server/benefactor/base/handler.go @@ -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, + } +} diff --git a/delivery/http_server/benefactor/base/route.go b/delivery/http_server/benefactor/base/route.go new file mode 100644 index 0000000..7b6062a --- /dev/null +++ b/delivery/http_server/benefactor/base/route.go @@ -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) +} diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index 9b78b28..f740c73 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -4,9 +4,11 @@ import ( "fmt" 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" benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req" 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" benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req" benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor" @@ -19,7 +21,8 @@ type Server struct { config config.Config Router *echo.Echo benefactorHandler benefactorhandler.Handler - benefactorkindboxreqhandler benefactorkindboxreqhandler.Handler + benefactorKindBoxReqHandler benefactorkindboxreqhandler.Handler + benefactorBaseHandler benefactorbasehandler.Handler } func New( @@ -29,12 +32,14 @@ func New( authSvc authservice.Service, benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator, + benefactorAddressSvc benefactoraddressservice.Service, ) Server { return Server{ Router: echo.New(), config: cfg, 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 s.Router.GET("/health-check", s.healthCheck) s.benefactorHandler.SetRoutes(s.Router) - s.benefactorkindboxreqhandler.SetRoutes(s.Router) + s.benefactorKindBoxReqHandler.SetRoutes(s.Router) + s.benefactorBaseHandler.SetRoutes(s.Router) // Start server address := fmt.Sprintf(":%d", s.config.HTTPServer.Port) diff --git a/main.go b/main.go index b587710..8640685 100644 --- a/main.go +++ b/main.go @@ -27,15 +27,16 @@ func main() { mgr := migrator.New(cfg.Mysql) mgr.Up() - authSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld := setupServices(cfg) - server := httpserver.New(cfg, benefactorSvc, benefactorVld, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld) + authSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc := setupServices(cfg) + server := httpserver.New(cfg, benefactorSvc, benefactorVld, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc) server.Serve() } -//nolint:nakedret // we are sure of this +//nolint:nakedret,gocritic // we are sure of this func setupServices(cfg config.Config) ( authSvc authservice.Service, benefactorSvc benefactorservice.Service, benefactorVld benefactorvalidator.Validator, benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator, + benefactorAddressSvc benefactoraddressservice.Service, ) { authSvc = authservice.New(cfg.Auth) @@ -52,13 +53,13 @@ func setupServices(cfg config.Config) ( benefactorVld = benefactorvalidator.New() - benefactorkindBoxReqMysql := mysqlkindboxreq.New(MysqlRepo) - benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorkindBoxReqMysql) + benefactorKindBoxReqMysql := mysqlkindboxreq.New(MysqlRepo) + benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorKindBoxReqMysql) 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 } diff --git a/param/benefactor/address/get_all_provinces.go b/param/benefactor/address/get_all_provinces.go new file mode 100644 index 0000000..4963bd6 --- /dev/null +++ b/param/benefactor/address/get_all_provinces.go @@ -0,0 +1,10 @@ +package addressparam + +import "git.gocasts.ir/ebhomengo/niki/entity" + +type ( + GetAllProvincesRequest struct{} + GetAllProvincesResponse struct { + Provinces []entity.Province `json:"provinces"` + } +) diff --git a/repository/mysql/address/get_all_provinces.go b/repository/mysql/address/get_all_provinces.go new file mode 100644 index 0000000..770e05e --- /dev/null +++ b/repository/mysql/address/get_all_provinces.go @@ -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 +} diff --git a/repository/mysql/migration/1705510025_seeder_add_provinces_table.sql b/repository/mysql/migration/1705510025_seeder_add_provinces_table.sql new file mode 100644 index 0000000..ef4d613 --- /dev/null +++ b/repository/mysql/migration/1705510025_seeder_add_provinces_table.sql @@ -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'; \ No newline at end of file diff --git a/service/benefactor/address/service.go b/service/benefactor/address/service.go index bed51da..c6c964a 100644 --- a/service/benefactor/address/service.go +++ b/service/benefactor/address/service.go @@ -11,6 +11,7 @@ import ( type Repository interface { CreateBenefactorAddress(ctx context.Context, address entity.Address) (entity.Address, error) GetAddressByID(ctx context.Context, id uint) (*entity.Address, error) + GetAllProvinces(ctx context.Context) ([]entity.Province, error) } type Service struct { @@ -50,3 +51,14 @@ func (s Service) AddressExistByID(ctx context.Context, req param.GetAddressByIDR 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 +}