Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions scripts/data.cql
Original file line number Diff line number Diff line change
@@ -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()));
61 changes: 48 additions & 13 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
}
8 changes: 4 additions & 4 deletions src/users/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}