Add restaurants
This commit is contained in:
parent
545c74f214
commit
4c7b9310c0
1
db.go
1
db.go
@ -19,6 +19,7 @@ func initDB() *gorp.DbMap {
|
||||
dbmap.AddTableWithName(User{}, "users").SetKeys(true, "UserId")
|
||||
dbmap.AddTableWithName(Session{}, "sessions").SetKeys(true, "SessionId")
|
||||
dbmap.AddTableWithName(Attendee{}, "attendees").SetKeys(true, "AttendeeId")
|
||||
dbmap.AddTableWithName(Restaurant{}, "restaurants").SetKeys(true, "RestaurantId")
|
||||
|
||||
err = dbmap.CreateTablesIfNotExists()
|
||||
if err != nil {
|
||||
|
@ -17,6 +17,7 @@ var error_codes = map[int]string{
|
||||
3: "Invalid Request",
|
||||
4: "User Exists",
|
||||
5: "Attendee Exists",
|
||||
6: "Restaurant Exists",
|
||||
// 5: "Connection Failed", //client-side error
|
||||
999: "Internal Error",
|
||||
}
|
||||
|
93
js/actions/AttendeeActions.js
Normal file
93
js/actions/AttendeeActions.js
Normal file
@ -0,0 +1,93 @@
|
||||
var AttendeeConstants = require('../constants/AttendeeConstants');
|
||||
|
||||
var ErrorActions = require('./ErrorActions');
|
||||
|
||||
var models = require('../models.js');
|
||||
var Attendee = models.Attendee;
|
||||
var Error = models.Error;
|
||||
|
||||
function fetchAttendees() {
|
||||
return {
|
||||
type: AttendeeConstants.FETCH_ATTENDEES
|
||||
}
|
||||
}
|
||||
|
||||
function attendeesFetched(attendees) {
|
||||
return {
|
||||
type: AttendeeConstants.ATTENDEES_FETCHED,
|
||||
attendees: attendees
|
||||
}
|
||||
}
|
||||
|
||||
function createAttendee() {
|
||||
return {
|
||||
type: AttendeeConstants.CREATE_ATTENDEE
|
||||
}
|
||||
}
|
||||
|
||||
function attendeeCreated(attendee) {
|
||||
return {
|
||||
type: AttendeeConstants.ATTENDEE_CREATED,
|
||||
attendee: attendee
|
||||
}
|
||||
}
|
||||
|
||||
function fetchAll() {
|
||||
return function (dispatch) {
|
||||
dispatch(fetchAttendees());
|
||||
|
||||
$.ajax({
|
||||
type: "GET",
|
||||
dataType: "json",
|
||||
url: "attendee/",
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
ErrorActions.serverError(e);
|
||||
} else {
|
||||
dispatch(attendeesFetched(data.attendees.map(function(json) {
|
||||
var a = new Attendee();
|
||||
a.fromJSON(json);
|
||||
return a;
|
||||
})));
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, status, error) {
|
||||
ErrorActions.ajaxError(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function create(attendee) {
|
||||
return function (dispatch) {
|
||||
dispatch(createAttendee());
|
||||
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
url: "attendee/",
|
||||
data: {attendee: attendee.toJSON()},
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
ErrorActions.serverError(e);
|
||||
} else {
|
||||
var a = new Attendee();
|
||||
a.fromJSON(data);
|
||||
dispatch(attendeeCreated(a));
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, status, error) {
|
||||
ErrorActions.ajaxError(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fetchAll: fetchAll,
|
||||
create: create
|
||||
};
|
93
js/actions/RestaurantActions.js
Normal file
93
js/actions/RestaurantActions.js
Normal file
@ -0,0 +1,93 @@
|
||||
var RestaurantConstants = require('../constants/RestaurantConstants');
|
||||
|
||||
var ErrorActions = require('./ErrorActions');
|
||||
|
||||
var models = require('../models.js');
|
||||
var Restaurant = models.Restaurant;
|
||||
var Error = models.Error;
|
||||
|
||||
function fetchRestaurants() {
|
||||
return {
|
||||
type: RestaurantConstants.FETCH_RESTAURANTS
|
||||
}
|
||||
}
|
||||
|
||||
function restaurantsFetched(restaurants) {
|
||||
return {
|
||||
type: RestaurantConstants.RESTAURANTS_FETCHED,
|
||||
restaurants: restaurants
|
||||
}
|
||||
}
|
||||
|
||||
function createRestaurant() {
|
||||
return {
|
||||
type: RestaurantConstants.CREATE_RESTAURANT
|
||||
}
|
||||
}
|
||||
|
||||
function restaurantCreated(restaurant) {
|
||||
return {
|
||||
type: RestaurantConstants.RESTAURANT_CREATED,
|
||||
restaurant: restaurant
|
||||
}
|
||||
}
|
||||
|
||||
function fetchAll() {
|
||||
return function (dispatch) {
|
||||
dispatch(fetchRestaurants());
|
||||
|
||||
$.ajax({
|
||||
type: "GET",
|
||||
dataType: "json",
|
||||
url: "restaurant/",
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
ErrorActions.serverError(e);
|
||||
} else {
|
||||
dispatch(restaurantsFetched(data.restaurants.map(function(json) {
|
||||
var a = new Restaurant();
|
||||
a.fromJSON(json);
|
||||
return a;
|
||||
})));
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, status, error) {
|
||||
ErrorActions.ajaxError(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function create(restaurant) {
|
||||
return function (dispatch) {
|
||||
dispatch(createRestaurant());
|
||||
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
url: "restaurant/",
|
||||
data: {restaurant: restaurant.toJSON()},
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
ErrorActions.serverError(e);
|
||||
} else {
|
||||
var a = new Restaurant();
|
||||
a.fromJSON(data);
|
||||
dispatch(restaurantCreated(a));
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, status, error) {
|
||||
ErrorActions.ajaxError(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fetchAll: fetchAll,
|
||||
create: create
|
||||
};
|
@ -1,5 +1,7 @@
|
||||
var UserConstants = require('../constants/UserConstants');
|
||||
|
||||
var AttendeeActions = require('./AttendeeActions');
|
||||
var RestaurantActions = require('./RestaurantActions');
|
||||
var ErrorActions = require('./ErrorActions');
|
||||
|
||||
var models = require('../models.js');
|
||||
@ -89,6 +91,8 @@ function fetch(userId) {
|
||||
function initializeSession(dispatch, session) {
|
||||
dispatch(userLoggedIn(session));
|
||||
dispatch(fetch(session.UserId));
|
||||
dispatch(AttendeeActions.fetchAll());
|
||||
dispatch(RestaurantActions.fetchAll());
|
||||
}
|
||||
|
||||
function login(user) {
|
||||
|
8
js/constants/AttendeeConstants.js
Normal file
8
js/constants/AttendeeConstants.js
Normal file
@ -0,0 +1,8 @@
|
||||
var keyMirror = require('keymirror');
|
||||
|
||||
module.exports = keyMirror({
|
||||
FETCH_ATTENDEES: null,
|
||||
ATTENDEES_FETCHED: null,
|
||||
CREATE_ATTENDEE: null,
|
||||
ATTENDEE_CREATED: null
|
||||
});
|
8
js/constants/RestaurantConstants.js
Normal file
8
js/constants/RestaurantConstants.js
Normal file
@ -0,0 +1,8 @@
|
||||
var keyMirror = require('keymirror');
|
||||
|
||||
module.exports = keyMirror({
|
||||
FETCH_RESTAURANTS: null,
|
||||
RESTAURANTS_FETCHED: null,
|
||||
CREATE_RESTAURANT: null,
|
||||
RESTAURANT_CREATED: null
|
||||
});
|
27
js/models.js
27
js/models.js
@ -101,6 +101,33 @@ Attendee.prototype.isAttendee = function() {
|
||||
this.Name != empty_attendee.Name;
|
||||
}
|
||||
|
||||
function Restaurant() {
|
||||
this.RestaurantId = -1;
|
||||
this.Name = "";
|
||||
}
|
||||
|
||||
Restaurant.prototype.toJSON = function() {
|
||||
var json_obj = {};
|
||||
json_obj.RestaurantId = this.RestaurantId;
|
||||
json_obj.Name = this.Name;
|
||||
return JSON.stringify(json_obj);
|
||||
}
|
||||
|
||||
Restaurant.prototype.fromJSON = function(json_input) {
|
||||
var json_obj = getJSONObj(json_input);
|
||||
|
||||
if (json_obj.hasOwnProperty("RestaurantId"))
|
||||
this.RestaurantId = json_obj.RestaurantId;
|
||||
if (json_obj.hasOwnProperty("Name"))
|
||||
this.Name = json_obj.Name;
|
||||
}
|
||||
|
||||
Restaurant.prototype.isRestaurant = function() {
|
||||
var empty_attendee = new Restaurant();
|
||||
return this.RestaurantId != empty_attendee.RestaurantId ||
|
||||
this.Name != empty_attendee.Name;
|
||||
}
|
||||
|
||||
function Error() {
|
||||
this.ErrorId = -1;
|
||||
this.ErrorString = "";
|
||||
|
26
js/reducers/AttendeeReducer.js
Normal file
26
js/reducers/AttendeeReducer.js
Normal file
@ -0,0 +1,26 @@
|
||||
var assign = require('object-assign');
|
||||
|
||||
var AttendeeConstants = require('../constants/AttendeeConstants');
|
||||
var UserConstants = require('../constants/UserConstants');
|
||||
|
||||
module.exports = function(state = {}, action) {
|
||||
switch (action.type) {
|
||||
case AttendeeConstants.ATTENDEES_FETCHED:
|
||||
var attendees = {};
|
||||
for (var i = 0; i < action.attendees.length; i++) {
|
||||
var attendee = action.attendees[i];
|
||||
attendees[attendee.AttendeeId] = attendee;
|
||||
}
|
||||
return attendees;
|
||||
case AttendeeConstants.ATTENDEE_CREATED:
|
||||
var attendee = action.attendee;
|
||||
var attendees = assign({}, state, {
|
||||
[attendee.AttendeeId]: attendee
|
||||
});
|
||||
return attendees;
|
||||
case UserConstants.USER_LOGGEDOUT:
|
||||
return {};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
@ -2,10 +2,14 @@ var Redux = require('redux');
|
||||
|
||||
var UserReducer = require('./UserReducer');
|
||||
var SessionReducer = require('./SessionReducer');
|
||||
var AttendeeReducer = require('./AttendeeReducer');
|
||||
var RestaurantReducer = require('./RestaurantReducer');
|
||||
var ErrorReducer = require('./ErrorReducer');
|
||||
|
||||
module.exports = Redux.combineReducers({
|
||||
user: UserReducer,
|
||||
session: SessionReducer,
|
||||
attendees: AttendeeReducer,
|
||||
restaurants: RestaurantReducer,
|
||||
error: ErrorReducer
|
||||
});
|
||||
|
26
js/reducers/RestaurantReducer.js
Normal file
26
js/reducers/RestaurantReducer.js
Normal file
@ -0,0 +1,26 @@
|
||||
var assign = require('object-assign');
|
||||
|
||||
var RestaurantConstants = require('../constants/RestaurantConstants');
|
||||
var UserConstants = require('../constants/UserConstants');
|
||||
|
||||
module.exports = function(state = {}, action) {
|
||||
switch (action.type) {
|
||||
case RestaurantConstants.RESTAURANTS_FETCHED:
|
||||
var restaurants = {};
|
||||
for (var i = 0; i < action.restaurants.length; i++) {
|
||||
var restaurant = action.restaurants[i];
|
||||
restaurants[restaurant.RestaurantId] = restaurant;
|
||||
}
|
||||
return restaurants;
|
||||
case RestaurantConstants.RESTAURANT_CREATED:
|
||||
var restaurant = action.restaurant;
|
||||
var restaurants = assign({}, state, {
|
||||
[restaurant.RestaurantId]: restaurant
|
||||
});
|
||||
return restaurants;
|
||||
case UserConstants.USER_LOGGEDOUT:
|
||||
return {};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
1
main.go
1
main.go
@ -70,6 +70,7 @@ func main() {
|
||||
servemux.HandleFunc("/session/", SessionHandler)
|
||||
servemux.HandleFunc("/user/", UserHandler)
|
||||
servemux.HandleFunc("/attendee/", AttendeeHandler)
|
||||
servemux.HandleFunc("/restaurant/", RestaurantHandler)
|
||||
|
||||
listener, err := net.Listen("tcp", ":"+strconv.Itoa(port))
|
||||
if err != nil {
|
||||
|
143
restaurants.go
Normal file
143
restaurants.go
Normal file
@ -0,0 +1,143 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Restaurant struct {
|
||||
RestaurantId int64
|
||||
Name string
|
||||
}
|
||||
|
||||
type RestaurantList struct {
|
||||
Restaurants *[]*Restaurant `json:"restaurants"`
|
||||
}
|
||||
|
||||
func (r *Restaurant) Write(w http.ResponseWriter) error {
|
||||
enc := json.NewEncoder(w)
|
||||
return enc.Encode(r)
|
||||
}
|
||||
|
||||
func (r *Restaurant) Read(json_str string) error {
|
||||
dec := json.NewDecoder(strings.NewReader(json_str))
|
||||
return dec.Decode(r)
|
||||
}
|
||||
|
||||
func (rl *RestaurantList) Write(w http.ResponseWriter) error {
|
||||
enc := json.NewEncoder(w)
|
||||
return enc.Encode(rl)
|
||||
}
|
||||
|
||||
type RestaurantExistsError struct{}
|
||||
|
||||
func (aeu RestaurantExistsError) Error() string {
|
||||
return "Restaurant exists"
|
||||
}
|
||||
|
||||
func GetRestaurants() (*[]*Restaurant, error) {
|
||||
var restaurants []*Restaurant
|
||||
|
||||
_, err := DB.Select(&restaurants, "SELECT * from restaurants")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &restaurants, nil
|
||||
}
|
||||
|
||||
func InsertRestaurant(r *Restaurant) error {
|
||||
transaction, err := DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
existing, err := transaction.SelectInt("SELECT count(*) from users where Name=?", r.Name)
|
||||
if err != nil {
|
||||
transaction.Rollback()
|
||||
return err
|
||||
}
|
||||
if existing > 0 {
|
||||
transaction.Rollback()
|
||||
return RestaurantExistsError{}
|
||||
}
|
||||
|
||||
err = transaction.Insert(r)
|
||||
if err != nil {
|
||||
transaction.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
err = transaction.Commit()
|
||||
if err != nil {
|
||||
transaction.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RestaurantHandler(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := GetUserFromSession(r)
|
||||
if err != nil {
|
||||
WriteError(w, 1 /*Not Signed In*/)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == "POST" {
|
||||
restaurant_json := r.PostFormValue("restaurant")
|
||||
if restaurant_json == "" {
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
|
||||
var restaurant Restaurant
|
||||
err := restaurant.Read(restaurant_json)
|
||||
if err != nil {
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
restaurant.RestaurantId = -1
|
||||
|
||||
err = InsertRestaurant(&restaurant)
|
||||
if err != nil {
|
||||
if _, ok := err.(RestaurantExistsError); ok {
|
||||
WriteError(w, 6 /*Restaurant Exists*/)
|
||||
} else {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(201 /*Created*/)
|
||||
err = restaurant.Write(w)
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
} else if r.Method == "GET" {
|
||||
var rl RestaurantList
|
||||
|
||||
restaurants, err := GetRestaurants()
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
|
||||
rl.Restaurants = restaurants
|
||||
err = (&rl).Write(w)
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
/* No PUT or DELETE */
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user