Browse Source

Menambahkan login JWT dan API CRUD

widyarhn-dev
widyarhn 1 year ago
parent
commit
98c42f1dce
  1. 63
      controllers/usersController.go
  2. BIN
      go-crud.exe
  3. BIN
      go-crud.exe~

63
controllers/usersController.go

@ -215,29 +215,42 @@ func parseUserIDs(idsStr string) []uint {
} }
func GetLoginRecords(c *gin.Context) { func GetLoginRecords(c *gin.Context) {
var loginRecords []models.LoginRecord var loginRecords []models.LoginRecord
result := initializers.DB.Find(&loginRecords) result := initializers.DB.Find(&loginRecords)
if result.Error != nil { if result.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{ c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to fetch login records", "code": http.StatusInternalServerError,
}) "data": nil,
return "message": "Failed to fetch login records",
} "status": false,
"totalRecord": 0,
// Retrieve user details for each login record })
for i := range loginRecords { return
var users []models.User }
initializers.DB.Find(&users, "id IN (?)", parseUserIDs(loginRecords[i].UserIDs))
// Retrieve user details for each login record
loginRecords[i].Users = users for i := range loginRecords {
} var users []models.User
initializers.DB.Find(&users, "id IN (?)", parseUserIDs(loginRecords[i].UserIDs))
// Create JSON response
var response struct { loginRecords[i].Users = users
Records []models.LoginRecord `json:"records"` }
}
response.Records = loginRecords // Create JSON response
var response struct {
c.JSON(http.StatusOK, response) Code int `json:"code"`
Data []models.LoginRecord `json:"data"`
Message string `json:"message"`
Status bool `json:"status"`
TotalRecord int `json:"totalRecord"`
}
response.Code = http.StatusOK
response.Data = loginRecords
response.Message = "OK"
response.Status = true
response.TotalRecord = len(loginRecords)
c.JSON(http.StatusOK, response)
} }

BIN
go-crud.exe

Binary file not shown.

BIN
go-crud.exe~

Binary file not shown.
Loading…
Cancel
Save