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) {
var loginRecords []models.LoginRecord
result := initializers.DB.Find(&loginRecords)
if result.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to fetch login records",
})
return
}
// Retrieve user details for each login record
for i := range loginRecords {
var users []models.User
initializers.DB.Find(&users, "id IN (?)", parseUserIDs(loginRecords[i].UserIDs))
loginRecords[i].Users = users
}
// Create JSON response
var response struct {
Records []models.LoginRecord `json:"records"`
}
response.Records = loginRecords
c.JSON(http.StatusOK, response)
var loginRecords []models.LoginRecord
result := initializers.DB.Find(&loginRecords)
if result.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"data": nil,
"message": "Failed to fetch login records",
"status": false,
"totalRecord": 0,
})
return
}
// Retrieve user details for each login record
for i := range loginRecords {
var users []models.User
initializers.DB.Find(&users, "id IN (?)", parseUserIDs(loginRecords[i].UserIDs))
loginRecords[i].Users = users
}
// Create JSON response
var response struct {
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