feat: add post method for areas domain (restore) existing code
This commit is contained in:
parent
0dbe367bb0
commit
7cd2106428
|
@ -69,3 +69,39 @@ func NewSubdistrictResponse(id, subdistrict, createdAt, updatedAt string) Subdis
|
||||||
UpdatedAt: updatedAt,
|
UpdatedAt: updatedAt,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CoverageAreaCreateRequest struct {
|
||||||
|
Province string `json:"province" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCoverageAreaCreateRequest(province string) CoverageAreaCreateRequest {
|
||||||
|
return CoverageAreaCreateRequest{
|
||||||
|
Province: province,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type CoverageDistrictCreateRequest struct {
|
||||||
|
CoverageAreaID string `json:"coverage_area_id" validate:"required"`
|
||||||
|
District string `json:"district" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCoverageDistrictCreateRequest(coverageAreaID, district string) CoverageDistrictCreateRequest {
|
||||||
|
return CoverageDistrictCreateRequest{
|
||||||
|
CoverageAreaID: coverageAreaID,
|
||||||
|
District: district,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type CoverageSubdistrictCreateRequest struct {
|
||||||
|
CoverageAreaID string `json:"coverage_area_id" validate:"required"`
|
||||||
|
CoverageDistrictId string `json:"coverage_district_id" validate:"required"`
|
||||||
|
Subdistrict string `json:"subdistrict" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCoverageSubdistrictCreateRequest(coverageAreaID, coverageDistrictId, subdistrict string) CoverageSubdistrictCreateRequest {
|
||||||
|
return CoverageSubdistrictCreateRequest{
|
||||||
|
CoverageAreaID: coverageAreaID,
|
||||||
|
CoverageDistrictId: coverageDistrictId,
|
||||||
|
Subdistrict: subdistrict,
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,9 +24,9 @@ func AppRouter(app *fiber.App) {
|
||||||
api.Get("/coverage-areas", controllers.GetCoverageAreas)
|
api.Get("/coverage-areas", controllers.GetCoverageAreas)
|
||||||
api.Get("/coverage-areas-district/:id", controllers.GetCoverageAreaByIDProvince)
|
api.Get("/coverage-areas-district/:id", controllers.GetCoverageAreaByIDProvince)
|
||||||
api.Get("/coverage-areas-subdistrict/:id", controllers.GetCoverageAreaByIDDistrict)
|
api.Get("/coverage-areas-subdistrict/:id", controllers.GetCoverageAreaByIDDistrict)
|
||||||
// api.Post("/coverage-areas", controllers.CreateCoverageArea)
|
api.Post("/coverage-areas", controllers.CreateCoverageArea)
|
||||||
// api.Post("/coverage-areas-district", controllers.CreateCoverageDetail)
|
api.Post("/coverage-areas-district", controllers.CreateCoverageDistrict)
|
||||||
// api.Post("/coverage-areas-subdistrict", controllers.CreateLocationSpecific)
|
api.Post("/coverage-areas-subdistrict", controllers.CreateCoverageSubdistrict)
|
||||||
|
|
||||||
// # role
|
// # role
|
||||||
api.Get("/roles", controllers.GetAllUserRoles)
|
api.Get("/roles", controllers.GetAllUserRoles)
|
||||||
|
|
|
@ -135,3 +135,109 @@ func GetCoverageAreaByIDDistrict(c *fiber.Ctx) error {
|
||||||
coverageAreaResponse,
|
coverageAreaResponse,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateCoverageArea(c *fiber.Ctx) error {
|
||||||
|
var request dto.CoverageAreaCreateRequest
|
||||||
|
if err := c.BodyParser(&request); err != nil {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
"Invalid request payload",
|
||||||
|
nil,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
coverageArea, err := services.CreateCoverageArea(request.Province)
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusInternalServerError,
|
||||||
|
"Failed to create coverage area",
|
||||||
|
nil,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
coverageAreaResponse := dto.NewCoverageAreaResponse(
|
||||||
|
coverageArea.ID,
|
||||||
|
coverageArea.Province,
|
||||||
|
utils.FormatDateToIndonesianFormat(coverageArea.CreatedAt),
|
||||||
|
utils.FormatDateToIndonesianFormat(coverageArea.UpdatedAt),
|
||||||
|
)
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusOK,
|
||||||
|
"Coverage area has been created",
|
||||||
|
coverageAreaResponse,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateCoverageDistrict(c *fiber.Ctx) error {
|
||||||
|
var request dto.CoverageDistrictCreateRequest
|
||||||
|
if err := c.BodyParser(&request); err != nil {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
"Invalid request payload",
|
||||||
|
nil,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
coverageDistrict, err := services.CreateCoverageDistrict(request.CoverageAreaID, request.District)
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusInternalServerError,
|
||||||
|
"Failed to create coverage district",
|
||||||
|
nil,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
coverageDistrictResponse := dto.NewCoverageAreaResponse(
|
||||||
|
coverageDistrict.ID,
|
||||||
|
coverageDistrict.District,
|
||||||
|
utils.FormatDateToIndonesianFormat(coverageDistrict.CreatedAt),
|
||||||
|
utils.FormatDateToIndonesianFormat(coverageDistrict.UpdatedAt),
|
||||||
|
)
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusOK,
|
||||||
|
"Coverage district has been created",
|
||||||
|
coverageDistrictResponse,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateCoverageSubdistrict(c *fiber.Ctx) error {
|
||||||
|
|
||||||
|
var request dto.CoverageSubdistrictCreateRequest
|
||||||
|
if err := c.BodyParser(&request); err != nil {
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
"Invalid request payload",
|
||||||
|
nil,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
coverageSubdistrict, err := services.CreateCoverageSubdistrict(
|
||||||
|
request.CoverageAreaID,
|
||||||
|
request.CoverageDistrictId,
|
||||||
|
request.Subdistrict,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusInternalServerError,
|
||||||
|
"Failed to create coverage subdistrict",
|
||||||
|
nil,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
coverageSubdistrictResponse := dto.NewCoverageAreaResponse(
|
||||||
|
coverageSubdistrict.ID,
|
||||||
|
coverageSubdistrict.Subdistrict,
|
||||||
|
utils.FormatDateToIndonesianFormat(coverageSubdistrict.CreatedAt),
|
||||||
|
utils.FormatDateToIndonesianFormat(coverageSubdistrict.UpdatedAt),
|
||||||
|
)
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusOK,
|
||||||
|
"Coverage subdistrict has been created",
|
||||||
|
coverageSubdistrictResponse,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
|
@ -44,3 +44,24 @@ func GetSubdistrictsByCoverageDistrictID(districtID string) ([]domain.CoverageSu
|
||||||
}
|
}
|
||||||
return subdistricts, nil
|
return subdistricts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateCoverageArea(coverageArea *domain.CoverageArea) error {
|
||||||
|
if err := config.DB.Create(&coverageArea).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateCoverageDistrict(coverageDistrict *domain.CoverageDistric) error {
|
||||||
|
if err := config.DB.Create(&coverageDistrict).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateCoverageSubdistrict(coverageSubdistrict *domain.CoverageSubdistrict) error {
|
||||||
|
if err := config.DB.Create(&coverageSubdistrict).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -24,3 +24,42 @@ func GetCoverageDistricsByCoverageAreaID(areaID string) ([]domain.CoverageDistri
|
||||||
func GetSubdistrictsByCoverageDistrictID(districtID string) ([]domain.CoverageSubdistrict, error) {
|
func GetSubdistrictsByCoverageDistrictID(districtID string) ([]domain.CoverageSubdistrict, error) {
|
||||||
return repositories.GetSubdistrictsByCoverageDistrictID(districtID)
|
return repositories.GetSubdistrictsByCoverageDistrictID(districtID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateCoverageArea(province string) (*domain.CoverageArea, error) {
|
||||||
|
coverageArea := &domain.CoverageArea{
|
||||||
|
Province: province,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := repositories.CreateCoverageArea(coverageArea); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return coverageArea, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateCoverageDistrict(coverageAreaID, district string) (*domain.CoverageDistric, error) {
|
||||||
|
coverageDistrict := &domain.CoverageDistric{
|
||||||
|
CoverageAreaID: coverageAreaID,
|
||||||
|
District: district,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := repositories.CreateCoverageDistrict(coverageDistrict); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return coverageDistrict, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateCoverageSubdistrict(coverageAreaID, coverageDistrictId, subdistrict string) (*domain.CoverageSubdistrict, error) {
|
||||||
|
coverageSubdistrict := &domain.CoverageSubdistrict{
|
||||||
|
CoverageAreaID: coverageAreaID,
|
||||||
|
CoverageDistrictId: coverageDistrictId,
|
||||||
|
Subdistrict: subdistrict,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := repositories.CreateCoverageSubdistrict(coverageSubdistrict); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return coverageSubdistrict, nil
|
||||||
|
}
|
Loading…
Reference in New Issue