package handler import ( "kemendagri/sipd/services/master-sipd/controllers" "log" "strconv" "github.com/go-playground/validator/v10" "github.com/gofiber/fiber/v2" ) type KecamatanHandler struct { Controller *controllers.KecamatanController Validate *validator.Validate } func NewKecamatanHandler(app *fiber.App, vld *validator.Validate, controller *controllers.KecamatanController) { handler := &KecamatanHandler{ Controller: controller, Validate: vld, } // public route rpub := app.Group("/kecamatan") rpub.Get("/", handler.Index) rpub.Get("/:id", handler.View) // rpub.Post("/", handler.Create) // rpub.Put("/:id", handler.Update) // rpub.Delete("/:id", handler.Delete) } // Index func for get lists of kecamatan. // // @Summary get lists of kecamatan // @Description get lists of kecamatan // @Tags Master Kecamatan // @Accept json // @Param page query int false "Halaman yang ditampilkan" // @Param limit query int false "Jumlah data per halaman, maksimal 50 data" // @param filter query string false "Key value pencarian ([kode_kecamatan,nama_kecamatan]). Contoh: kode_kecamatan.11.01.01 -> (akan menampilkan data dengan kode kecamatan = '11.01.01')" // @Produce json // @success 200 {array} models.MstKecamatanModel "Success" // @Failure 400 {object} utils.RequestError "Bad request" // @Failure 401 {object} utils.RequestError "Unauthorized" // @Failure 404 {object} utils.RequestError "Not found" // @Failure 422 {array} utils.RequestError "Data validation failed" // @Failure 500 {object} utils.RequestError "Server error" // @Router /kecamatan [get] func (h *KecamatanHandler) Index(c *fiber.Ctx) error { page, err := strconv.Atoi(c.Query("page", "1")) if err != nil { return err } log.Println("page: ", page) var limit int limit, err = strconv.Atoi(c.Query("limit", "20")) if err != nil { return err } log.Println("page: ", page) //limit max 50 if limit > 50 { limit = 50 } resp, totalCount, pageCount, err := h.Controller.Index(page, limit, c.Query("filter", "")) if err != nil { return err } log.Printf("totalCount: %d, perPage: %d, pageCount: %d\n", totalCount, limit, pageCount) c.Append("x-pagination-total-count", strconv.Itoa(totalCount)) c.Append("x-pagination-page-count", strconv.Itoa(pageCount)) c.Append("x-pagination-page-size", strconv.Itoa(limit)) if page > 1 { c.Append("x-pagination-previous-page", strconv.Itoa(page-1)) } c.Append("x-pagination-current-page", strconv.Itoa(page)) if page < pageCount { c.Append("x-pagination-next-page", strconv.Itoa(page+1)) } return c.JSON(resp) } // Index func for get detail of kecamatan. // // @Summary get detail of kecamatan // @Description get detail of kecamatan // @Tags Master Kecamatan // @Accept json // @Param id path int false "data yang dipilih berdasarkan id kecamatan" // @Produce json // @success 200 {object} models.MstKecamatanDetilModel "Success" // @Failure 400 {object} utils.RequestError "Bad request" // @Failure 401 {object} utils.RequestError "Unauthorized" // @Failure 404 {object} utils.RequestError "Not found" // @Failure 422 {array} utils.RequestError "Data validation failed" // @Failure 500 {object} utils.RequestError "Server error" // @Router /kecamatan/{id} [get] func (h *KecamatanHandler) View(c *fiber.Ctx) error { id, err := c.ParamsInt("id", 0) if err != nil { return err } resp, err := h.Controller.View(id) if err != nil { return err } return c.JSON(resp) } // // Create func for create new daerah. // // // // @Summary create new daerah // // @Description create new daerah. // // @Tags Ref Daerah // // @Accept json // // @Param payload body models.RefDaerahModelPayload true "Payload" // // @Produce json // // @success 200 {object} bool "Create success" // // @Failure 400 {object} utils.RequestError "Bad request" // // @Failure 401 {object} utils.RequestError "Unauthorized" // // @Failure 404 {object} utils.RequestError "Not found" // // @Failure 422 {array} utils.DataValidationError "Data validation failed" // // @Failure 500 {object} utils.RequestError "Server error" // // @Security ApiKeyAuth // // @Router /daerah [post] // func (h *DaerahHandler) Create(c *fiber.Ctx) error { // // siapkan penampung payload dari fe // payloadl := new(models.RefDaerahModelPayload) // // baca kiriman dari fe // err := c.BodyParser(payloadl) // if err != nil { // return err // } // // Validate form input // err = h.Validate.Struct(payloadl) // if err != nil { // return err // } // err = h.Controller.Create(*payloadl) // if err != nil { // return err // } // return c.JSON("true") // } // // Create func for update daerah. // // // // @Summary update daerah // // @Description update daerah. // // @Tags Ref Daerah // // @Accept json // // @Param id path int false "data yang dipilih untuk diubah berdasarkan id daerah" // // @Param payload body models.RefDaerahModelPayload true "Payload" // // @Produce json // // @success 200 {object} bool "Update success" // // @Failure 400 {object} utils.RequestError "Bad request" // // @Failure 401 {object} utils.RequestError "Unauthorized" // // @Failure 404 {object} utils.RequestError "Not found" // // @Failure 422 {array} utils.DataValidationError "Data validation failed" // // @Failure 500 {object} utils.RequestError "Server error" // // @Security ApiKeyAuth // // @Router /daerah/{id} [put] // func (h *DaerahHandler) Update(c *fiber.Ctx) error { // id, err := c.ParamsInt("id", 0) // if err != nil { // return err // } // // siapkan penampung payload dari fe // payloadl := new(models.RefDaerahModelPayload) // // baca kiriman dari fe // err = c.BodyParser(payloadl) // if err != nil { // return err // } // // Validate form input // err = h.Validate.Struct(payloadl) // if err != nil { // return err // } // err = h.Controller.Update(id, *payloadl) // if err != nil { // return err // } // return c.JSON("true") // } // // Create func for delete daerah. // // // // @Summary delete daerah // // @Description delete daerah. // // @Tags Ref Daerah // // @Accept json // // @Param id path int false "data yang dipilih untuk dihapus berdasarkan id daerah" // // @Produce json // // @success 200 {object} bool "Delete success" // // @Failure 400 {object} utils.RequestError "Bad request" // // @Failure 401 {object} utils.RequestError "Unauthorized" // // @Failure 404 {object} utils.RequestError "Not found" // // @Failure 422 {array} utils.DataValidationError "Data validation failed" // // @Failure 500 {object} utils.RequestError "Server error" // // @Security ApiKeyAuth // // @Router /daerah/{id} [delete] // func (h *DaerahHandler) Delete(c *fiber.Ctx) error { // id, err := c.ParamsInt("id", 0) // if err != nil { // return err // } // err = h.Controller.Delete(id) // if err != nil { // return err // } // return c.JSON("true") // }