Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aait.backend.g11.controller testing #2321

Open
wants to merge 2 commits into
base: aait.backend.g11.main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions backend/AAiT-backend-group-11/delivery/controller/ai_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"backend-starter-project/domain/dto"
"backend-starter-project/service"
"net/http"

Expand All @@ -22,20 +23,29 @@ func (acc *AIContentController) GenerateContentSuggestions(c *gin.Context) {
Keywords []string `json:"keywords"`
}

var response dto.Response


if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
response.Success = false
response.Error = "Invalid request payload"
c.JSON(http.StatusBadRequest, response)
return
}

contentSuggestion, err := acc.aiContentService.GenerateContentSuggestions( req.Keywords)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
response.Success = false
response.Error = "Error generating content suggestions"
c.JSON(http.StatusInternalServerError, response)
return
}

c.JSON(http.StatusOK, gin.H{
response.Success = true
response.Data = gin.H{
"content": contentSuggestion,
})
}
c.JSON(http.StatusOK, response)
}

func (acc *AIContentController) SuggestContentImprovements(c *gin.Context) {
Expand All @@ -44,16 +54,27 @@ func (acc *AIContentController) SuggestContentImprovements(c *gin.Context) {
Instruction string `json:"instruction"`
}

var response dto.Response

if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
response.Success = false
response.Error = "Invalid request payload"
c.JSON(http.StatusBadRequest, response)
return
}

contentSuggestion, err := acc.aiContentService.SuggestContentImprovements(req.BlogPostID, req.Instruction)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
response.Success = false
response.Error = "Error suggesting content improvements"
c.JSON(http.StatusBadRequest, response)
return
}

c.JSON(http.StatusOK, contentSuggestion)

response.Success = true
response.Data = gin.H{
"suggestion result": contentSuggestion,
}
c.JSON(http.StatusOK, response)
}
152 changes: 110 additions & 42 deletions backend/AAiT-backend-group-11/delivery/controller/auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"backend-starter-project/domain/dto"
"backend-starter-project/domain/entities"
"backend-starter-project/domain/interfaces"
"log"
"net/http"
"time"

Expand All @@ -25,9 +26,11 @@ func NewAuthController(authService interfaces.AuthenticationService, passwordRes
func (controller *AuthController) RegisterUser(c *gin.Context) {

var userRequest dto.UserCreateRequestDTO

var response dto.Response
if err := c.ShouldBindJSON(&userRequest); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
response.Success = false
response.Error = "Invalid request payload"
c.JSON(http.StatusBadRequest, response)
return
}

Expand All @@ -45,7 +48,9 @@ func (controller *AuthController) RegisterUser(c *gin.Context) {

createdUser, err := controller.authService.RegisterUser(&user)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
response.Success = false
response.Error = err.Error()
c.JSON(http.StatusInternalServerError,response)
return
}

Expand All @@ -55,8 +60,8 @@ func (controller *AuthController) RegisterUser(c *gin.Context) {
Role: createdUser.Role,
}


c.JSON(http.StatusCreated, gin.H{"data": userResponse})
response.Data = userResponse
c.JSON(http.StatusCreated, response)
}

func (controller *AuthController) Login(c *gin.Context) {
Expand All @@ -65,107 +70,170 @@ func (controller *AuthController) Login(c *gin.Context) {
c.JSON(400, gin.H{"error": err.Error()})
return
}

var response dto.Response
refreshToken, accessToken, err := controller.authService.Login(user.Email, user.Password)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
response.Success = false
response.Error = err.Error()
c.JSON(400, response)
return
}

c.Header("Authorization", "Bearer "+accessToken)
c.JSON(200, gin.H{"refresh_token": refreshToken.Token})
c.JSON(200, gin.H{"message": "login successful"})
response.Data = gin.H{ "access_token": accessToken}
response.Message = "Logged in successfully"
c.Set("userId", refreshToken.UserID)
c.SetCookie("refresh_token", refreshToken.Token, int(refreshToken.ExpiresAt.Unix()), "/", "localhost", false, true)
c.JSON(http.StatusOK, response)
}

func (controller *AuthController) Logout(c *gin.Context) {
userId := c.GetString("userId")

controller.authService.Logout(userId)
c.JSON(200, gin.H{"message": "succesfully logged out"})
var response dto.Response
err := controller.authService.Logout(userId)
log.Println("logout" + userId)
if err != nil {
response.Success = false
response.Error = err.Error()
c.JSON(400, response)
return
}
response.Success = true
response.Message = "succesfully logged out"
c.JSON(200, response)
}

func (controller *AuthController) RefreshAccessToken(c *gin.Context) {
var token entities.RefreshToken
var response dto.Response
err := c.ShouldBindJSON(&token)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
response.Success = true
response.Error = err.Error()
c.JSON(400, response)
return
}

newToken,err := controller.authService.RefreshAccessToken(&token)
if err != nil {
response.Success = false
response.Error = err.Error()
c.JSON(400, response)
return
}
controller.authService.RefreshAccessToken(&token)
response.Success = true
response.Data = gin.H{"access_token": newToken}
c.JSON(200, response)
}

func (controller *AuthController) VerifyEmail(c *gin.Context) {

var emailVerification entities.EmailVerificationRequest

if err := c.ShouldBindJSON(&emailVerification); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, dto.Response{
Success: false,
Message: "Invalid request",
Error: err.Error(),
})
return
}

err := controller.authService.VerifyEmail(emailVerification.Email, emailVerification.Code)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, dto.Response{
Success: false,
Message: "Email verification failed",
Error: err.Error(),
})
return
}

c.JSON(http.StatusOK, gin.H{"message": "Email verified successfully"})
c.JSON(http.StatusOK, dto.Response{
Success: true,
Message: "Email verified successfully",
})
}


func (controller *AuthController) RequestPasswordReset(c *gin.Context) {

var forgetPasswordRequest entities.ForgetPasswordRequest

if err := c.ShouldBindJSON(&forgetPasswordRequest); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, dto.Response{
Success: false,
Message: "Invalid request",
Error: err.Error(),
})
return
}

err := controller.passwordResetService.RequestPasswordReset(forgetPasswordRequest.Email)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, dto.Response{
Success: false,
Message: "Failed to send password reset link",
Error: err.Error(),
})
return
}

c.JSON(http.StatusOK, gin.H{"message": "Password reset link sent to your email"})
c.JSON(http.StatusOK, dto.Response{
Success: true,
Message: "Password reset link sent to your email",
})
}

func (controller *AuthController) ResetPassword(c *gin.Context) {

var passwordReset entities.PasswordReset

if err := c.ShouldBindJSON(&passwordReset); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, dto.Response{
Success: false,
Message: "Invalid request",
Error: err.Error(),
})
return
}

err := controller.passwordResetService.ResetPassword(passwordReset.Token, passwordReset.NewPassword)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, dto.Response{
Success: false,
Message: "Password reset failed",
Error: err.Error(),
})
return
}

c.JSON(http.StatusOK, gin.H{"message": "Password reset successfully"})
c.JSON(http.StatusOK, dto.Response{
Success: true,
Message: "Password reset successfully",
})
}

func (controller *AuthController) ResendOtp(c *gin.Context) {
var request entities.ResendOTPRequest

var request entities.ResendOTPRequest

err := c.ShouldBind(&request)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
err := c.ShouldBind(&request)
if err != nil {
c.JSON(http.StatusBadRequest, dto.Response{
Success: false,
Message: "Invalid request",
Error: err.Error(),
})
return
}

err = controller.authService.ResendOtp(request)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
err = controller.authService.ResendOtp(request)
if err != nil {
c.JSON(http.StatusInternalServerError, dto.Response{
Success: false,
Message: "Failed to resend OTP",
Error: err.Error(),
})
return
}

c.JSON(http.StatusOK, gin.H{"message": "OTP sent successfully"})
}
c.JSON(http.StatusOK, dto.Response{
Success: true,
Message: "OTP sent successfully",
})
}
Loading