File size: 3,170 Bytes
ab82ffd
 
 
 
 
 
124871e
ab82ffd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0722d3
 
 
 
 
 
 
360774b
c0722d3
 
ab82ffd
 
124871e
360774b
 
 
 
 
 
 
 
 
ab82ffd
 
c0722d3
 
 
 
 
 
 
 
 
 
ab82ffd
 
 
 
 
 
c0722d3
 
 
 
 
 
 
 
 
ab82ffd
c0722d3
 
ab82ffd
 
 
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
70
71
72
73
74
75
76
77
78
79
package controllers

import (
	"abdanhafidz.com/go-boilerplate/models/dto"
	"abdanhafidz.com/go-boilerplate/services"
	"github.com/gin-gonic/gin"
	"time"
)

type EmailVerificationController interface {
	Create(ctx *gin.Context)
	Validate(ctx *gin.Context)
	Delete(ctx *gin.Context)
}

type emailVerificationController struct {
	emailVerificationService services.EmailVerificationService
}

func NewEmailVerificationController(emailVerificationService services.EmailVerificationService) EmailVerificationController {
	return &emailVerificationController{emailVerificationService: emailVerificationService}
}

// Create Email Verification godoc
// @Summary      Create Email Verification Token
// @Description  Generate a verification token and send it to the specified email address
// @Tags         Email Verification
// @Accept       json
// @Produce      json
// @Param        request  body      dto.CreateEmailVerificationRequest  true  "Create Email Verification Request"
// @Success      200      {object}  dto.SuccessResponse[dto.CreateEmailVerificationResponse]
// @Failure      400      {object}  dto.ErrorResponse
// @Router       /api/v1/email/create-verification [post]
func (c *emailVerificationController) Create(ctx *gin.Context) {
	req := RequestJSON[dto.CreateEmailVerificationRequest](ctx)
	res, err := c.emailVerificationService.CreateToken(ctx.Request.Context(), req.Email, 0, time.Time{})
	if err != nil {
		ResponseJSON(ctx, req, dto.CreateEmailVerificationResponse{}, err)
		return
	}

	ResponseJSON(ctx, req, dto.CreateEmailVerificationResponse{
		Email:     req.Email,
		ExpiredAt: res.ExpiredAt,
	}, nil)
}

// Validate Email Verification godoc
// @Summary      Validate Email Verification Token
// @Description  Validate the provided verification token for the specified email address
// @Tags         Email Verification
// @Accept       json
// @Produce      json
// @Param        request  body      dto.ValidateVerifyEmailRequest  true  "Validate Verify Email Request"
// @Success      200      {object}  dto.SuccessResponse[any]
// @Failure      400      {object}  dto.ErrorResponse
// @Router       /api/v1/email/verify [post]
func (c *emailVerificationController) Validate(ctx *gin.Context) {
	req := RequestJSON[dto.ValidateVerifyEmailRequest](ctx)
	err := c.emailVerificationService.VerifyToken(ctx.Request.Context(), req.Email, req.Token)
	ResponseJSON[any](ctx, req, gin.H{"status": "ok"}, err)
}

// Delete Email Verification godoc
// @Summary      Delete Email Verification Token
// @Description  Delete the verification token after successful validation
// @Tags         Email Verification
// @Accept       json
// @Produce      json
// @Param        request  body      dto.DeleteEmailVerificationRequest  true  "Delete Email Verification Request"
// @Success      200      {object}  dto.SuccessResponse[any]
// @Failure      400      {object}  dto.ErrorResponse
func (c *emailVerificationController) Delete(ctx *gin.Context) {

	req := RequestJSON[dto.DeleteEmailVerificationRequest](ctx)
	err := c.emailVerificationService.DeleteByToken(ctx.Request.Context(), req.Token)
	ResponseJSON[any](ctx, req, gin.H{"status": "ok"}, err)
}