pembaruhan swagger dan tool
This commit is contained in:
164
internal/handlers/vclaim/peserta.go
Normal file
164
internal/handlers/vclaim/peserta.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"api-service/internal/config"
|
||||
models "api-service/internal/models/vclaim"
|
||||
services "api-service/internal/services/bpjs"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// PesertaHandler handles peserta BPJS services
|
||||
type PesertaHandler struct {
|
||||
service services.VClaimService
|
||||
}
|
||||
|
||||
// NewPesertaHandler creates a new PesertaHandler
|
||||
func NewPesertaHandler(cfg config.BpjsConfig) *PesertaHandler {
|
||||
return &PesertaHandler{
|
||||
service: services.NewService(cfg),
|
||||
}
|
||||
}
|
||||
|
||||
// CreatePeserta godoc
|
||||
// @Summary Create a new PESERTA
|
||||
// @Description Create a new Peserta in BPJS system
|
||||
// @Tags vclaim-peserta
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.PesertaPostRequest true "Peserta creation request"
|
||||
// @Success 200 {object} models.PesertaResponse "Peserta created successfully"
|
||||
// @Failure 400 {object} gin.H "Invalid request"
|
||||
// @Failure 500 {object} gin.H "Internal server error"
|
||||
// @Router /api/v1/vclaim/peserta [post]
|
||||
func (h *PesertaHandler) CreatePeserta(c *gin.Context) {
|
||||
var req models.PesertaPostRequest
|
||||
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, "PESERTA/2.0/insert", req, &result); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "create failed", "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, models.PesertaResponse{
|
||||
Message: "Peserta berhasil dibuat",
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
|
||||
// UpdatePeserta godoc
|
||||
// @Summary Update an existing PESERTA
|
||||
// @Description Update an existing Peserta in BPJS system
|
||||
// @Tags vclaim-peserta
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.PesertaPutRequest true "Peserta update request"
|
||||
// @Success 200 {object} models.PesertaResponse "Peserta updated successfully"
|
||||
// @Failure 400 {object} gin.H "Invalid request"
|
||||
// @Failure 500 {object} gin.H "Internal server error"
|
||||
// @Router /api/v1/vclaim/peserta [put]
|
||||
func (h *PesertaHandler) UpdatePeserta(c *gin.Context) {
|
||||
var req models.PesertaPutRequest
|
||||
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, "PESERTA/2.0/update", req, &result); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "update failed", "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, models.PesertaResponse{
|
||||
Message: "Peserta berhasil diperbarui",
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
|
||||
// DeletePeserta godoc
|
||||
// @Summary Delete an existing PESERTA
|
||||
// @Description Delete a Peserta by ID
|
||||
// @Tags vclaim-peserta
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "Peserta ID"
|
||||
// @Param user query string true "User"
|
||||
// @Success 200 {object} models.PesertaResponse "Peserta deleted successfully"
|
||||
// @Failure 400 {object} gin.H "Invalid request"
|
||||
// @Failure 500 {object} gin.H "Internal server error"
|
||||
// @Router /api/v1/vclaim/peserta/{id} [delete]
|
||||
func (h *PesertaHandler) DeletePeserta(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
user := c.Query("user")
|
||||
if id == "" || user == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "id & user required"})
|
||||
return
|
||||
}
|
||||
|
||||
body := models.PesertaDeleteRequest{}
|
||||
body.TPeserta.ID = id
|
||||
body.TPeserta.User = user
|
||||
|
||||
ctx, cancel := context.WithTimeout(c, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := h.service.Delete(ctx, "PESERTA/2.0/delete", body); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "delete failed", "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, models.PesertaResponse{
|
||||
Message: "Peserta berhasil dihapus",
|
||||
Data: nil,
|
||||
})
|
||||
}
|
||||
|
||||
// GetPeserta godoc
|
||||
// @Summary Get an existing PESERTA
|
||||
// @Description Retrieve a Peserta by ID
|
||||
// @Tags vclaim-peserta
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "Peserta ID"
|
||||
// @Success 200 {object} models.PesertaResponse "Data Peserta retrieved successfully"
|
||||
// @Failure 400 {object} gin.H "Invalid request"
|
||||
// @Failure 500 {object} gin.H "Internal server error"
|
||||
// @Router /api/v1/vclaim/peserta/{id} [get]
|
||||
func (h *PesertaHandler) GetPeserta(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "id required"})
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(c, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
endpoint := fmt.Sprintf("PESERTA/%s", id)
|
||||
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, models.PesertaResponse{
|
||||
Message: "Data Peserta berhasil diambil",
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user