From e8d8e8d70b2d84f431606c0d97b812bc0c0268b4 Mon Sep 17 00:00:00 2001 From: William P Date: Thu, 27 Mar 2025 21:20:46 -0400 Subject: [PATCH] list all users method --- api/api.go | 1 + api/db.go | 21 +++++++++++++++++++++ api/response.go | 8 ++++++++ api/user.go | 12 ++++++++++++ test_data/messages.json | 4 ++-- 5 files changed, 44 insertions(+), 2 deletions(-) diff --git a/api/api.go b/api/api.go index 2114e1c..07b7328 100644 --- a/api/api.go +++ b/api/api.go @@ -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) diff --git a/api/db.go b/api/db.go index 06e3af5..7cad316 100644 --- a/api/db.go +++ b/api/db.go @@ -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 { diff --git a/api/response.go b/api/response.go index dfae7ff..b48c715 100644 --- a/api/response.go +++ b/api/response.go @@ -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} } diff --git a/api/user.go b/api/user.go index 3009a60..1bac250 100644 --- a/api/user.go +++ b/api/user.go @@ -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() } diff --git a/test_data/messages.json b/test_data/messages.json index a1d7da7..c1e4f81 100644 --- a/test_data/messages.json +++ b/test_data/messages.json @@ -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"