github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/controllers/notifications/notifications.go (about) 1 package notifications 2 3 import ( 4 "fmt" 5 "time" 6 7 //"log" 8 "net/http" 9 10 "github.com/gin-gonic/gin" 11 12 "github.com/mdaxf/iac/logger" 13 14 "github.com/mdaxf/iac/controllers/common" 15 notif "github.com/mdaxf/iac/notifications" 16 ) 17 18 type NotificationController struct { 19 } 20 21 // CreateNotification handles the creation of a new notification. 22 // It retrieves the request body and user information from the context, 23 // creates a new notification using the provided data, and returns the created notification. 24 // If any error occurs during the process, it returns an error response. 25 26 func (n *NotificationController) CreateNotification(ctx *gin.Context) { 27 iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "notifications"} 28 29 startTime := time.Now() 30 defer func() { 31 elapsed := time.Since(startTime) 32 iLog.PerformanceWithDuration("notification.CreateNotification", elapsed) 33 }() 34 35 /* defer func() { 36 err := recover() 37 if err != nil { 38 iLog.Error(fmt.Sprintf("Error: %v", err)) 39 ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 40 } 41 }() 42 */ 43 requestobj, clientid, user, err := common.GetRequestBodyandUserbyJson(ctx) 44 if err != nil { 45 iLog.Error(fmt.Sprintf("Get request information Error: %v", err)) 46 ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 47 return 48 } 49 iLog.ClientID = clientid 50 iLog.User = user 51 ndata := make(map[string]interface{}) 52 ndata = requestobj["data"].(map[string]interface{}) 53 err = notif.CreateNewNotification(ndata, user) 54 55 if err != nil { 56 57 iLog.Error(fmt.Sprintf("failed to create the notification: %v", err)) 58 ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 59 return 60 } 61 iLog.Debug(fmt.Sprintf("Create notification to respository with data: %s", logger.ConvertJson(ndata))) 62 63 ctx.JSON(http.StatusOK, gin.H{"data": ndata}) 64 } 65 66 // GetNotificationsbyUser retrieves the notifications for a specific user. 67 // It takes a gin.Context as input and returns the notifications as JSON data. 68 // The function logs performance metrics and any errors encountered during execution. 69 func (n *NotificationController) GetNotificationsbyUser(ctx *gin.Context) { 70 iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "notifications"} 71 72 startTime := time.Now() 73 defer func() { 74 elapsed := time.Since(startTime) 75 iLog.PerformanceWithDuration("notification.GetNotificationsbyUser", elapsed) 76 }() 77 /* 78 defer func() { 79 err := recover() 80 if err != nil { 81 iLog.Error(fmt.Sprintf("Error: %v", err)) 82 ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 83 } 84 }() 85 */ 86 _, user, clientid, err := common.GetRequestUser(ctx) 87 if err != nil { 88 iLog.Error(fmt.Sprintf("Get user information Error: %v", err)) 89 ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 90 return 91 } 92 iLog.ClientID = clientid 93 iLog.User = user 94 95 items, err := notif.GetNotificationsbyUser(user) 96 97 if err != nil { 98 99 iLog.Error(fmt.Sprintf("failed to retrieve the list from notification: %v", err)) 100 ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 101 return 102 } 103 iLog.Debug(fmt.Sprintf("Get notification list from respository with data: %s", logger.ConvertJson(items))) 104 105 ctx.JSON(http.StatusOK, gin.H{"data": items}) 106 } 107 108 // ResponseNotification handles the HTTP request for updating a notification. 109 // It retrieves the request body and user information from the context, updates the notification, 110 // and returns the updated notification data in the response. 111 112 func (n *NotificationController) ResponseNotification(ctx *gin.Context) { 113 iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "notifications"} 114 115 startTime := time.Now() 116 defer func() { 117 elapsed := time.Since(startTime) 118 iLog.PerformanceWithDuration("notifications.ResponseNotification", elapsed) 119 }() 120 /* 121 defer func() { 122 err := recover() 123 if err != nil { 124 iLog.Error(fmt.Sprintf("Error: %v", err)) 125 ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 126 } 127 }() 128 */ 129 requestobj, clientid, user, err := common.GetRequestBodyandUserbyJson(ctx) 130 if err != nil { 131 iLog.Error(fmt.Sprintf("Get request information Error: %v", err)) 132 ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 133 return 134 } 135 iLog.ClientID = clientid 136 iLog.User = user 137 ndata := make(map[string]interface{}) 138 ndata = requestobj["data"].(map[string]interface{}) 139 comments := requestobj["comments"].(string) 140 status := int(requestobj["status"].(float64)) 141 err = notif.UpdateNotification(ndata, user, comments, status) 142 143 if err != nil { 144 145 iLog.Error(fmt.Sprintf("failed to update the notification: %v", err)) 146 ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 147 return 148 } 149 iLog.Debug(fmt.Sprintf("Update notification to respository with data: %s", logger.ConvertJson(ndata))) 150 151 ctx.JSON(http.StatusOK, gin.H{"data": ndata}) 152 153 }