github.com/ars0915/glossika-exercise@v0.0.0-20240904180321-a5617a00d892/router/user.go (about)

     1  package router
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/gin-gonic/gin"
     7  
     8  	"github.com/ars0915/glossika-exercise/usecase"
     9  	"github.com/ars0915/glossika-exercise/util/cGin"
    10  )
    11  
    12  type registerBody struct {
    13  	Email    string `json:"email" binding:"required,email"`
    14  	Password string `json:"password" binding:"required,password"`
    15  }
    16  
    17  func (rH *HttpHandler) registerHandler(c *gin.Context) {
    18  	ctx := cGin.NewContext(c)
    19  
    20  	var body registerBody
    21  	if err := ctx.ShouldBindJSON(&body); err != nil {
    22  		ctx.WithError(err).Response(http.StatusBadRequest, "Invalid Json")
    23  		return
    24  	}
    25  
    26  	if err := rH.h.Register(ctx, usecase.RegisterParam{
    27  		Email:    body.Email,
    28  		Password: body.Password,
    29  	}); err != nil {
    30  		ctx.WithError(err).Response(http.StatusInternalServerError, "Register Failed")
    31  		return
    32  	}
    33  
    34  	ctx.Response(http.StatusOK, "")
    35  }
    36  
    37  type verifyUserBody struct {
    38  	Email            string `json:"email" binding:"required,email"`
    39  	VerificationCode string `json:"verificationCode" binding:"required,len=6"`
    40  }
    41  
    42  func (rH *HttpHandler) verifyUserHandler(c *gin.Context) {
    43  	ctx := cGin.NewContext(c)
    44  
    45  	var body verifyUserBody
    46  	if err := ctx.ShouldBindJSON(&body); err != nil {
    47  		ctx.WithError(err).Response(http.StatusBadRequest, "Invalid Json")
    48  		return
    49  	}
    50  
    51  	if err := rH.h.Verify(ctx, usecase.VerifyUserParam{
    52  		Email:            body.Email,
    53  		VerificationCode: body.VerificationCode,
    54  	}); err != nil {
    55  		ctx.WithError(err).Response(http.StatusInternalServerError, "Verify Failed")
    56  		return
    57  	}
    58  
    59  	ctx.Response(http.StatusOK, "")
    60  }
    61  
    62  type loginBody struct {
    63  	Email    string `json:"email" binding:"required,email"`
    64  	Password string `json:"password" binding:"required,password"`
    65  }
    66  
    67  func (rH *HttpHandler) loginHandler(c *gin.Context) {
    68  	ctx := cGin.NewContext(c)
    69  
    70  	var body loginBody
    71  	if err := ctx.ShouldBindJSON(&body); err != nil {
    72  		ctx.WithError(err).Response(http.StatusBadRequest, "Invalid Json")
    73  		return
    74  	}
    75  
    76  	token, err := rH.h.Login(ctx, usecase.LoginParam{
    77  		Email:    body.Email,
    78  		Password: body.Password,
    79  	})
    80  	if err != nil {
    81  		ctx.WithError(err).Response(http.StatusInternalServerError, "Login Failed")
    82  		return
    83  	}
    84  	ctx.Response(http.StatusOK, token)
    85  }
    86  
    87  func (rH *HttpHandler) getRecommendHandler(c *gin.Context) {
    88  	ctx := cGin.NewContext(c)
    89  
    90  	userID := ctx.GetUint("userID")
    91  	if userID == uint(0) {
    92  		ctx.Response(http.StatusUnauthorized, "Unauthorized")
    93  		return
    94  	}
    95  
    96  	products, err := rH.h.GetRecommendProducts(ctx, userID)
    97  	if err != nil {
    98  		ctx.WithError(err).Response(http.StatusInternalServerError, "GetRecommendProducts Failed")
    99  		return
   100  	}
   101  	ctx.WithData(products).Response(http.StatusOK, "")
   102  }