191 lines
4.7 KiB
Go
191 lines
4.7 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"api-service/internal/config"
|
|
vclaimModels "api-service/internal/models/vclaim"
|
|
services "api-service/internal/services/bpjs"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type SepHandler struct {
|
|
service services.VClaimService
|
|
}
|
|
|
|
func NewSepHandler(cfg config.BpjsConfig) *SepHandler {
|
|
return &SepHandler{
|
|
service: services.NewService(cfg),
|
|
}
|
|
}
|
|
|
|
// CreateSEP godoc
|
|
// @Summary Create a new SEP
|
|
// @Description Create a new Surat Eligibilitas Peserta
|
|
// @Tags SEP
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body vclaimModels.SepPostRequest true "SEP creation request"
|
|
// @Success 200 {object} vclaimModels.SepResponse "SEP created successfully"
|
|
// @Failure 400 {object} gin.H "Invalid request"
|
|
// @Failure 500 {object} gin.H "Internal server error"
|
|
// @Router /sep [post]
|
|
func (h *SepHandler) CreateSEP(c *gin.Context) {
|
|
var req vclaimModels.SepPostRequest
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "invalid body",
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(c, 30*time.Second)
|
|
defer cancel()
|
|
|
|
var result map[string]interface{}
|
|
if err := h.service.Post(ctx, "SEP/2.0/insert", req, &result); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "create failed",
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, vclaimModels.SepResponse{
|
|
Message: "SEP berhasil dibuat",
|
|
Data: result,
|
|
})
|
|
}
|
|
|
|
// UpdateSEP godoc
|
|
// @Summary Update SEP
|
|
// @Description Update Surat Eligibilitas Peserta
|
|
// @Tags SEP
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body vclaimModels.SepPutRequest true "SEP update request"
|
|
// @Success 200 {object} vclaimModels.SepResponse "SEP updated successfully"
|
|
// @Failure 400 {object} gin.H "Invalid request"
|
|
// @Failure 500 {object} gin.H "Internal server error"
|
|
// @Router /sep [put]
|
|
func (h *SepHandler) UpdateSEP(c *gin.Context) {
|
|
var req vclaimModels.SepPutRequest
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "invalid body",
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(c, 30*time.Second)
|
|
defer cancel()
|
|
|
|
var result map[string]interface{}
|
|
if err := h.service.Put(ctx, "SEP/2.0/update", req, &result); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "update failed",
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, vclaimModels.SepResponse{
|
|
Message: "SEP berhasil diperbarui",
|
|
Data: result,
|
|
})
|
|
}
|
|
|
|
// DeleteSEP godoc
|
|
// @Summary Delete SEP
|
|
// @Description Delete a Surat Eligibilitas Peserta by noSep
|
|
// @Tags SEP
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param noSep path string true "No SEP"
|
|
// @Param user query string true "User"
|
|
// @Success 200 {object} vclaimModels.SepResponse "SEP deleted successfully"
|
|
// @Failure 400 {object} gin.H "Invalid request"
|
|
// @Failure 500 {object} gin.H "Internal server error"
|
|
// @Router /sep/{noSep} [delete]
|
|
func (h *SepHandler) DeleteSEP(c *gin.Context) {
|
|
noSep := c.Param("noSep")
|
|
user := c.Query("user")
|
|
|
|
if noSep == "" || user == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "noSep and user required",
|
|
})
|
|
return
|
|
}
|
|
|
|
body := vclaimModels.SepDeleteRequest{}
|
|
body.TSep.NoSep = noSep
|
|
body.TSep.User = user
|
|
|
|
ctx, cancel := context.WithTimeout(c, 30*time.Second)
|
|
defer cancel()
|
|
|
|
if err := h.service.Delete(ctx, "SEP/2.0/delete", body); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "delete failed",
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
var result map[string]interface{}
|
|
c.JSON(http.StatusOK, vclaimModels.SepResponse{
|
|
Message: "SEP berhasil dihapus",
|
|
Data: result,
|
|
})
|
|
}
|
|
|
|
// GetSEP godoc
|
|
// @Summary Get SEP
|
|
// @Description Retrieve a Surat Eligibilitas Peserta by noSep
|
|
// @Tags SEP
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param noSep path string true "No SEP"
|
|
// @Success 200 {object} vclaimModels.SepResponse "Data SEP retrieved successfully"
|
|
// @Failure 400 {object} gin.H "Invalid request"
|
|
// @Failure 500 {object} gin.H "Internal server error"
|
|
// @Router /sep/{noSep} [get]
|
|
func (h *SepHandler) GetSEP(c *gin.Context) {
|
|
noSep := c.Param("noSep")
|
|
|
|
if noSep == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "noSep required",
|
|
})
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(c, 30*time.Second)
|
|
defer cancel()
|
|
|
|
endpoint := fmt.Sprintf("SEP/%s", noSep)
|
|
var result map[string]interface{}
|
|
|
|
if err := h.service.Get(ctx, endpoint, &result); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "fetch failed",
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, vclaimModels.SepResponse{
|
|
Message: "Data SEP berhasil diambil",
|
|
Data: result,
|
|
})
|
|
}
|