list all users method

This commit is contained in:
2025-03-27 21:20:46 -04:00
parent 25ee1d3299
commit e8d8e8d70b
5 changed files with 44 additions and 2 deletions

View File

@@ -48,6 +48,7 @@ func Start() {
})
r.Route("/users", func(r chi.Router) {
r.Get("/", ListUsers)
r.Route("/{userID}", func(r chi.Router) {
r.Use(UserCtx) // Load user
r.Get("/", GetUser)

View File

@@ -27,6 +27,27 @@ func dbGetUser(id string) (*User, error) {
return nil, errors.New("User not found")
}
func dbGetAllUsers() ([]*User, error) {
data := db.ExecDB("users")
if data == nil {
return nil, errors.New("failed to load users database")
}
users := data["users"].([]interface{})
var result []*User
for _, u := range users {
user := u.(map[string]interface{})
result = append(result, &User{
ID: user["ID"].(string),
Name: user["Name"].(string),
})
}
if len(result) == 0 {
return nil, errors.New("no users found")
}
return result, nil
}
func dbGetMessage(id string) (*Message, error) {
data := db.ExecDB("messages")
if data == nil {

View File

@@ -30,6 +30,14 @@ func NewMessageListResponse(messages []*Message) []render.Renderer {
return list
}
func NewUserListResponse(users []*User) []render.Renderer {
list := []render.Renderer{}
for _, user := range users {
list = append(list, NewUserPayloadResponse(user))
}
return list
}
func NewUserPayloadResponse(user *User) *UserPayload {
return &UserPayload{User: user}
}

View File

@@ -43,6 +43,18 @@ func GetUser(w http.ResponseWriter, r *http.Request) {
}
}
func ListUsers(w http.ResponseWriter, r *http.Request) {
dbUsers, err := dbGetAllUsers()
if err != nil {
render.Render(w, r, ErrRender(err))
return
}
if err := render.RenderList(w, r, NewUserListResponse(dbUsers)); err != nil {
render.Render(w, r, ErrRender(err))
return
}
}
func newUserID() string {
return "user_" + uuid.New().String()
}

View File

@@ -77,8 +77,8 @@
"UserID": "user_8d7cd2ed-0aa2-4810-a172-42dd58563a54"
},
{
"Body": "I'd like to interject for a moment. What you're referring to as Linux, is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux. Linux is not an operating system unto itself, but rather another free component of a fully functioning GNU system made useful by the GNU corelibs, shell utilities and vital system components comprising a full OS as defined by POSIX. Many computer users run a modified version of the GNU system every day, without realizing it. Through a peculiar turn of events, the version of GNU which is widely used today is often called “Linux,” and many of its users are not aware that it is basically the GNU system, developed by the GNU Project. There really is a Linux, and these people are using it, but it is just a part of the system they use.\n\nLinux is the kernel: the program in the system that allocates the machine's resources to the other programs that you run. The kernel is an essential part of an operating system, but useless by itself; it can only function in the context of a complete operating system. Linux is normally used in combination with the GNU operating system: the whole system is basically GNU with Linux added, or GNU/Linux. All the so-called “Linux” distributions are really distributions of GNU/Linux.",
"Edited": "2025-03-27T18:59:12-04:00",
"Body": "I'd just like to interject for a moment. What you're referring to as Linux, is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux. Linux is not an operating system unto itself, but rather another free component of a fully functioning GNU system made useful by the GNU corelibs, shell utilities and vital system components comprising a full OS as defined by POSIX. Many computer users run a modified version of the GNU system every day, without realizing it. Through a peculiar turn of events, the version of GNU which is widely used today is often called “Linux,” and many of its users are not aware that it is basically the GNU system, developed by the GNU Project. There really is a Linux, and these people are using it, but it is just a part of the system they use.\n\nLinux is the kernel: the program in the system that allocates the machine's resources to the other programs that you run. The kernel is an essential part of an operating system, but useless by itself; it can only function in the context of a complete operating system. Linux is normally used in combination with the GNU operating system: the whole system is basically GNU with Linux added, or GNU/Linux. All the so-called “Linux” distributions are really distributions of GNU/Linux.",
"Edited": "2025-03-27T20:35:33-04:00",
"ID": "msg_d77f8e0f-5c23-4c10-984f-b07559e7c5ed",
"Timestamp": "2025-03-27T18:56:27-04:00",
"UserID": "user_8d7cd2ed-0aa2-4810-a172-42dd58563a54"