diff --git a/scripts/data.cql b/scripts/data.cql new file mode 100644 index 0000000..2d46270 --- /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_updated_timestamp timestamp +); + +-- Insert some data +INSERT INTO store.users +(id, name, email_address, last_updated_timestamp) +VALUES (uuid(), 'John', 'john@test.com',toTimeStamp(now())); +INSERT INTO store.users +(id, name, email_address, last_updated_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..351565d 100644 --- a/src/main.go +++ b/src/main.go @@ -6,15 +6,10 @@ import ( "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,31 +44,71 @@ func NewUsersHandler() *UsersHandler { return &UsersHandler{} } -func (h UsersHandler) CreateUser(w http.ResponseWriter, r *http.Request) {} -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()} +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 + } + + 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"] + + ctx := r.Context() + + session := cassandra.SetupCassandra() + defer session.Close() + + var user users.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 + } + 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) {} 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..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"` - Birthday time.Time `json:"birthday"` + 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"` }