Skip to content

Commit

Permalink
feat(Request User) added user/me and user/delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Djangss committed Dec 11, 2024
1 parent cc565aa commit b877c32
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 16 deletions.
2 changes: 1 addition & 1 deletion server/.env.server.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ DB_PORT=3306
DB_NAME=area
DB_USER=root
DB_PASSWORD=change-me
RMQ_URL="amqp://root:root@localhost:5672/"
RMQ_URL="amqp://guest:guest@localhost:5000/"
45 changes: 45 additions & 0 deletions server/internal/controllers/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package controllers

import (
"AREA/internal/models"
"AREA/internal/pkg"
"github.com/gin-gonic/gin"
)

func UserMe(c *gin.Context) {
user, err := pkg.GetUserFromToken(c)
if err != nil {
c.JSON(500, gin.H{"error": "Failed to fetch user"})
return
}
pkg.DB.Preload("Tokens").First(&user)

var services []models.ServiceRequest
for _, token := range user.Tokens {
var service models.Service
pkg.DB.Where("id = ?", token.ServiceID).First(&service)
services = append(services, models.ServiceRequest{
Name: service.Name,
ID: service.ID,
})
}

var userData = models.UserRequest{
Username: user.Username,
Email: user.Email,
Services: services,
}
c.JSON(200, gin.H{"user": userData})
}
func UserDelete(c *gin.Context) {
user, err := pkg.GetUserFromToken(c)
if err != nil {
c.JSON(500, gin.H{"error": "Failed to fetch user"})
return
}
if err := pkg.DB.Unscoped().Delete(&user).Error; err != nil {
c.JSON(500, gin.H{"error": "Failed to delete user", "details": err.Error()})
return
}
c.JSON(200, gin.H{"success": "User deleted"})
}
24 changes: 20 additions & 4 deletions server/internal/controllers/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ func WorkflowCreate(c *gin.Context) {
c.JSON(400, gin.H{"error": err.Error()})
return
}
userID, err := pkg.GetUserFromToken(c)
user, err := pkg.GetUserFromToken(c)
if err != nil {
c.JSON(401, gin.H{"error": "Unauthorized"})
return
}

workflow := models.Workflow{
UserID: userID,
UserID: user.ID,
Name: request.Name,
Description: request.Description,
Status: models.WorkflowStatusPending,
Expand Down Expand Up @@ -125,12 +125,12 @@ func WorkflowCreate(c *gin.Context) {
// @Router /workflow/list [get]
func WorkflowList(c *gin.Context) {
var workflows []models.Workflow
userID, err := pkg.GetUserFromToken(c)
user, err := pkg.GetUserFromToken(c)
if err != nil {
return
}
err = pkg.DB.Preload("WorkflowEvents.ParametersValues").
Where("user_id = ?", userID).
Where("user_id = ?", user.ID).
Find(&workflows).Error

if err != nil {
Expand Down Expand Up @@ -173,3 +173,19 @@ func WorkflowDelete(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{"message": "Workflow deleted successfully"})
}

func WorkflowGet(c *gin.Context) {
idParam := c.Param("id")
workflowID, err := strconv.Atoi(idParam)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid workflow ID"})
return
}
var workflow models.Workflow
result := pkg.DB.Preload("WorkflowEvents.ParametersValues").First(&workflow, workflowID)
if result.Error != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Workflow not found"})
return
}
c.JSON(http.StatusOK, gin.H{"workflow": workflow})
}
29 changes: 29 additions & 0 deletions server/internal/models/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,32 @@ type WorkflowCreationRequest struct {
} `json:"parameters"`
} `json:"events"`
}

type ServiceRequest struct {
ID uint `json:"id"`
Name string `json:"name"`
}

type UserRequest struct {
Username string `json:"username"`
Email string `json:"email"`
Services []ServiceRequest `json:"services"`
}

type WorkflowPutRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Services []interface{} `json:"services"`
Events []struct {
Id int `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Description string `json:"description"`
Parameters []struct {
Name string `json:"name"`
Type string `json:"type"`
Value string `json:"value"`
} `json:"parameters"`
} `json:"events"`
IsActive bool `json:"is_active"`
}
18 changes: 9 additions & 9 deletions server/internal/pkg/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import (
"github.com/gin-gonic/gin"
)

func GetUserFromToken(c *gin.Context) (uint, error) {
func GetUserFromToken(c *gin.Context) (models.User, error) {
email, err := utils.VerifyToken(c)
if err != nil {
return 0, err
return models.User{}, err
}
user := models.User{}
DB.Where("email = ?", email).First(&user)
if user.ID == 0 {
return 0, errors.New("User not found")
return models.User{}, errors.New("User not found")
}
return user.ID, nil
return user, nil
}

func GetServiceFromName(serviceName string) (uint, error) {
var service models.Service
if err := DB.Where("name = ?", serviceName).First(&service).Error; err != nil {
return 0, errors.New("Service not found")
}
return service.ID, nil
var service models.Service
if err := DB.Where("name = ?", serviceName).First(&service).Error; err != nil {
return 0, errors.New("service not found")
}
return service.ID, nil
}
15 changes: 13 additions & 2 deletions server/internal/routers/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

func setUpOauthGroup(router *gin.Engine) {
router.POST("/oauth/:service", controllers.OAuth)
//router.POST("/oauth/bind/:service", controllers.OAuthBind)
router.POST("/oauth/:service", controllers.OAuth)
//router.POST("/oauth/bind/:service", controllers.OAuthBind)
}

func setUpAuthGroup(router *gin.Engine) {
Expand All @@ -31,6 +31,16 @@ func setUpWorkflowGroup(router *gin.Engine) {
workflow.POST("/create", controllers.WorkflowCreate)
workflow.GET("/list", controllers.WorkflowList)
workflow.DELETE("/delete/:id", controllers.WorkflowDelete)
workflow.GET("/:id", controllers.WorkflowGet)
}
}

func setUpUserGroup(router *gin.Engine) {
user := router.Group("/user")
user.Use(middleware.AuthMiddleware())
{
user.GET("/me", controllers.UserMe)
user.DELETE("/delete", controllers.UserDelete)
}
}

Expand All @@ -50,6 +60,7 @@ func SetupRouter() *gin.Engine {
}
setUpAuthGroup(router)
setUpOauthGroup(router)
setUpUserGroup(router)
setUpWorkflowGroup(router)
protected := router.Group("/")
protected.Use(middleware.AuthMiddleware())
Expand Down

0 comments on commit b877c32

Please sign in to comment.