-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Request User) added user/me and user/delete
- Loading branch information
Showing
6 changed files
with
117 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters