File size: 1,592 Bytes
a020582
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f7dd45
a020582
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package options_controller

import (
	"net/http"

	"api.qobiltu.id/models"
	"api.qobiltu.id/response"
	"api.qobiltu.id/services"
	"github.com/gin-gonic/gin"
)

type OptionsController interface {
	CreateOptions(ctx *gin.Context)
	ListOptions(ctx *gin.Context)
	ListOptionsBySlug(ctx *gin.Context)
}

type optionsController struct {
	optionService services.OptionsService
}

func NewOptionsController(optionService services.OptionsService) OptionsController {
	return &optionsController{
		optionService: optionService,
	}
}

func (c *optionsController) CreateOptions(ctx *gin.Context) {
	var req []models.MultipleOptionsRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		response.HandleError(ctx, err)
		return
	}

	res, err := c.optionService.CreateOptions(ctx, &models.CreateOptionsRequest{
		Options: req,
	})
	if err != nil {
		response.HandleError(ctx, err)
		return
	}

	response.HandleSuccess(ctx, http.StatusOK, "Options created successfully", res, nil)
}

func (c *optionsController) ListOptions(ctx *gin.Context) {
	res, err := c.optionService.ListOptions(ctx)
	if err != nil {
		response.HandleError(ctx, err)
		return
	}

	response.HandleSuccess(ctx, http.StatusOK, "Options retrieved successfully", res.Options, nil)
}

func (c *optionsController) ListOptionsBySlug(ctx *gin.Context) {
	slug := ctx.Param("slug")

	res, err := c.optionService.ListOptionsBySlug(ctx, &models.ListOptionsBySlugRequest{
		Slug: slug,
	})
	if err != nil {
		response.HandleError(ctx, err)
		return
	}

	response.HandleSuccess(ctx, http.StatusOK, "Options retrieved successfully", res, nil)
}