From 69fd7937489da0f30aba894bcf16d986c16a886a Mon Sep 17 00:00:00 2001 From: MaxWallwey <127151268+MaxWallwey@users.noreply.github.com> Date: Sun, 27 Jul 2025 17:53:57 +0100 Subject: [PATCH 1/6] WIP --- scripts/data.cql | 18 ++++++++++++++++++ src/main.go | 26 ++++++++++++++++---------- 2 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 scripts/data.cql diff --git a/scripts/data.cql b/scripts/data.cql new file mode 100644 index 0000000..16046c5 --- /dev/null +++ b/scripts/data.cql @@ -0,0 +1,18 @@ +-- Create a keyspace +CREATE KEYSPACE IF NOT EXISTS store WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : '1' }; + +-- Create a table +CREATE TABLE IF NOT EXISTS store.users ( + id uuid PRIMARY KEY, + name text, + email_address text, + last_update_timestamp timestamp +); + +-- Insert some data +INSERT INTO store.users +(id, name, email_address, last_update_timestamp) +VALUES (uuid(), 'John', 'john@test.com',toTimeStamp(now())); +INSERT INTO store.users +(id, name, email_address, last_update_timestamp) +VALUES (uuid(), 'Paul', 'paul@test.com',toTimeStamp(now())); \ No newline at end of file diff --git a/src/main.go b/src/main.go index ca82671..04139ce 100644 --- a/src/main.go +++ b/src/main.go @@ -4,17 +4,11 @@ import ( "basic-api/cassandra" "basic-api/users" "encoding/json" - "github.com/gocql/gocql" "github.com/gorilla/mux" - "log" "net/http" - "time" ) func main() { - session := cassandra.SetupCassandra() - defer session.Close() - usersHandler := NewUsersHandler() // Create the router router := mux.NewRouter() @@ -49,11 +43,22 @@ func NewUsersHandler() *UsersHandler { return &UsersHandler{} } -func (h UsersHandler) CreateUser(w http.ResponseWriter, r *http.Request) {} +func (h UsersHandler) CreateUser(w http.ResponseWriter, r *http.Request) { + var user users.User + if err := json.NewDecoder(r.Body).Decode(&user); err != nil { + InternalServerErrorHandler(w, r) + return + } +} + func (h UsersHandler) ListUsers(w http.ResponseWriter, r *http.Request) { - log.Printf("get users") - var user = users.User{ID: gocql.UUIDFromTime(time.Now()), Name: "Max", EmailAddress: "test@test.com", Birthday: time.Now()} - jsonBytes, err := json.Marshal(user) + // users.User{ID: gocql.UUIDFromTime(time.Now()), Name: "Max", EmailAddress: "test@test.com", Birthday: time.Now()} + session := cassandra.SetupCassandra() + defer session.Close() + + allUsers := session.Query("SELECT * FROM store.users").Exec() + + jsonBytes, err := json.Marshal(allUsers) if err != nil { InternalServerErrorHandler(w, r) return @@ -64,6 +69,7 @@ func (h UsersHandler) ListUsers(w http.ResponseWriter, r *http.Request) { return } } + func (h UsersHandler) GetUser(w http.ResponseWriter, r *http.Request) {} func (h UsersHandler) UpdateUser(w http.ResponseWriter, r *http.Request) {} func (h UsersHandler) DeleteUser(w http.ResponseWriter, r *http.Request) {} From 4269b564ab0c4dd7ea6add065ad21ffa12c58d77 Mon Sep 17 00:00:00 2001 From: MaxWallwey <127151268+MaxWallwey@users.noreply.github.com> Date: Tue, 29 Jul 2025 09:51:48 +0100 Subject: [PATCH 2/6] Change query --- src/main.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main.go b/src/main.go index 04139ce..4140d3f 100644 --- a/src/main.go +++ b/src/main.go @@ -4,6 +4,7 @@ import ( "basic-api/cassandra" "basic-api/users" "encoding/json" + "github.com/gocql/gocql" "github.com/gorilla/mux" "net/http" ) @@ -51,26 +52,32 @@ func (h UsersHandler) CreateUser(w http.ResponseWriter, r *http.Request) { } } -func (h UsersHandler) ListUsers(w http.ResponseWriter, r *http.Request) { +func (h UsersHandler) ListUsers(w http.ResponseWriter, r *http.Request) {} +func (h UsersHandler) GetUser(w http.ResponseWriter, r *http.Request) { + id := mux.Vars(r)["id"] + // users.User{ID: gocql.UUIDFromTime(time.Now()), Name: "Max", EmailAddress: "test@test.com", Birthday: time.Now()} session := cassandra.SetupCassandra() defer session.Close() - allUsers := session.Query("SELECT * FROM store.users").Exec() + var user users.User + + err := session.Query("SELECT * FROM store.users WHERE id = ? LIMIT 1", id).Consistency(gocql.One).Scan(&user) + if err != nil { + InternalServerErrorHandler(w, r) + } - jsonBytes, err := json.Marshal(allUsers) + jsonBytes, err := json.Marshal(user) if err != nil { InternalServerErrorHandler(w, r) return } - w.WriteHeader(http.StatusOK) + _, err = w.Write(jsonBytes) if err != nil { return } } - -func (h UsersHandler) GetUser(w http.ResponseWriter, r *http.Request) {} func (h UsersHandler) UpdateUser(w http.ResponseWriter, r *http.Request) {} func (h UsersHandler) DeleteUser(w http.ResponseWriter, r *http.Request) {} From d8b6e001a00530dab7604503d6f5eb591770b7f6 Mon Sep 17 00:00:00 2001 From: MaxWallwey <127151268+MaxWallwey@users.noreply.github.com> Date: Tue, 29 Jul 2025 10:11:27 +0100 Subject: [PATCH 3/6] WIP --- src/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.go b/src/main.go index 4140d3f..3f8e1f8 100644 --- a/src/main.go +++ b/src/main.go @@ -65,6 +65,7 @@ func (h UsersHandler) GetUser(w http.ResponseWriter, r *http.Request) { err := session.Query("SELECT * FROM store.users WHERE id = ? LIMIT 1", id).Consistency(gocql.One).Scan(&user) if err != nil { InternalServerErrorHandler(w, r) + return } jsonBytes, err := json.Marshal(user) From 0737f237f5a0bcff89e75238ba02c320f764a976 Mon Sep 17 00:00:00 2001 From: MaxWallwey <127151268+MaxWallwey@users.noreply.github.com> Date: Tue, 29 Jul 2025 20:01:39 +0100 Subject: [PATCH 4/6] Context --- src/main.go | 36 +++++++++++++++++++++++++++++++----- src/users/user.go | 8 ++++---- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/main.go b/src/main.go index 3f8e1f8..57fdc84 100644 --- a/src/main.go +++ b/src/main.go @@ -6,6 +6,7 @@ import ( "encoding/json" "github.com/gocql/gocql" "github.com/gorilla/mux" + "log" "net/http" ) @@ -50,24 +51,43 @@ func (h UsersHandler) CreateUser(w http.ResponseWriter, r *http.Request) { InternalServerErrorHandler(w, r) return } + + ctx := r.Context() + + session := cassandra.SetupCassandra() + defer session.Close() + + err := session.Query("INSERT INTO store.users (id, name, email_address, last_updated_timestamp) VALUES (?,?,?,?)", user.ID, user.Name, user.EmailAddress, user.LastUpdatedTimestamp).WithContext(ctx).Exec() + if err != nil { + InternalServerErrorHandler(w, r) + return + } + + w.WriteHeader(http.StatusCreated) + json.NewEncoder(w).Encode(user) } func (h UsersHandler) ListUsers(w http.ResponseWriter, r *http.Request) {} func (h UsersHandler) GetUser(w http.ResponseWriter, r *http.Request) { id := mux.Vars(r)["id"] - // users.User{ID: gocql.UUIDFromTime(time.Now()), Name: "Max", EmailAddress: "test@test.com", Birthday: time.Now()} + log.Printf(id) + + ctx := r.Context() + session := cassandra.SetupCassandra() defer session.Close() var user users.User - err := session.Query("SELECT * FROM store.users WHERE id = ? LIMIT 1", id).Consistency(gocql.One).Scan(&user) + err := session.Query("SELECT * FROM store.users WHERE id = ? LIMIT 1", id).Consistency(gocql.One).WithContext(ctx).Scan(&user) if err != nil { - InternalServerErrorHandler(w, r) + NotFoundHandler(w, r) return } + log.Printf("%+v\n", user) + jsonBytes, err := json.Marshal(user) if err != nil { InternalServerErrorHandler(w, r) @@ -84,10 +104,16 @@ func (h UsersHandler) DeleteUser(w http.ResponseWriter, r *http.Request) {} func InternalServerErrorHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte("500 Internal Server Error")) + _, err := w.Write([]byte("500 Internal Server Error")) + if err != nil { + return + } } func NotFoundHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) - w.Write([]byte("404 Not Found")) + _, err := w.Write([]byte("404 Not Found")) + if err != nil { + return + } } diff --git a/src/users/user.go b/src/users/user.go index 96387a8..26d0213 100644 --- a/src/users/user.go +++ b/src/users/user.go @@ -7,8 +7,8 @@ import ( // User represents data about a record User. type User struct { - ID gocql.UUID `json:"id"` - Name string `json:"name"` - EmailAddress string `json:"emailAddress"` - Birthday time.Time `json:"birthday"` + ID gocql.UUID `json:"id"` + Name string `json:"name"` + EmailAddress string `json:"emailAddress"` + LastUpdatedTimestamp time.Time `json:"lastUpdatedTimestamp"` } From 7a5bef56f740941ec5fe13453d688de6adcfee5a Mon Sep 17 00:00:00 2001 From: MaxWallwey <127151268+MaxWallwey@users.noreply.github.com> Date: Tue, 29 Jul 2025 20:01:43 +0100 Subject: [PATCH 5/6] rename --- scripts/data.cql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/data.cql b/scripts/data.cql index 16046c5..2d46270 100644 --- a/scripts/data.cql +++ b/scripts/data.cql @@ -6,13 +6,13 @@ CREATE TABLE IF NOT EXISTS store.users ( id uuid PRIMARY KEY, name text, email_address text, - last_update_timestamp timestamp + last_updated_timestamp timestamp ); -- Insert some data INSERT INTO store.users -(id, name, email_address, last_update_timestamp) +(id, name, email_address, last_updated_timestamp) VALUES (uuid(), 'John', 'john@test.com',toTimeStamp(now())); INSERT INTO store.users -(id, name, email_address, last_update_timestamp) +(id, name, email_address, last_updated_timestamp) VALUES (uuid(), 'Paul', 'paul@test.com',toTimeStamp(now())); \ No newline at end of file From 95bca696e16e9f1977ad4200e2f2c1146cb47ff2 Mon Sep 17 00:00:00 2001 From: MaxWallwey <127151268+MaxWallwey@users.noreply.github.com> Date: Thu, 31 Jul 2025 09:08:34 +0100 Subject: [PATCH 6/6] Query works --- src/main.go | 9 ++------- src/users/user.go | 8 ++++---- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/main.go b/src/main.go index 57fdc84..351565d 100644 --- a/src/main.go +++ b/src/main.go @@ -6,7 +6,6 @@ import ( "encoding/json" "github.com/gocql/gocql" "github.com/gorilla/mux" - "log" "net/http" ) @@ -71,23 +70,19 @@ func (h UsersHandler) ListUsers(w http.ResponseWriter, r *http.Request) {} func (h UsersHandler) GetUser(w http.ResponseWriter, r *http.Request) { id := mux.Vars(r)["id"] - log.Printf(id) - ctx := r.Context() session := cassandra.SetupCassandra() defer session.Close() var user users.User - - err := session.Query("SELECT * FROM store.users WHERE id = ? LIMIT 1", id).Consistency(gocql.One).WithContext(ctx).Scan(&user) + queryString := "SELECT id, name, email_address, last_updated_timestamp FROM store.users WHERE id = ? LIMIT 1" + err := session.Query(queryString, id).Consistency(gocql.One).WithContext(ctx).Scan(&user.ID, &user.Name, &user.EmailAddress, &user.LastUpdatedTimestamp) if err != nil { NotFoundHandler(w, r) return } - log.Printf("%+v\n", user) - jsonBytes, err := json.Marshal(user) if err != nil { InternalServerErrorHandler(w, r) diff --git a/src/users/user.go b/src/users/user.go index 26d0213..9ad21da 100644 --- a/src/users/user.go +++ b/src/users/user.go @@ -7,8 +7,8 @@ import ( // User represents data about a record User. type User struct { - ID gocql.UUID `json:"id"` - Name string `json:"name"` - EmailAddress string `json:"emailAddress"` - LastUpdatedTimestamp time.Time `json:"lastUpdatedTimestamp"` + ID gocql.UUID `cql:"id" json:"id"` + Name string `cql:"name" json:"name"` + EmailAddress string `cql:"emailAddress" json:"emailAddress"` + LastUpdatedTimestamp time.Time `cql:"lastUpdatedTimestamp" json:"lastUpdatedTimestamp"` }