From c4f07d3b3e92aa88e5f6adf2842cea2b42b33e0c Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Sat, 24 Dec 2016 10:00:21 -0500 Subject: [PATCH] s/restaurants/suggestions/ --- attendees.go | 15 +- db.go | 2 +- errors.go | 2 +- ...taurantActions.js => SuggestionActions.js} | 24 +-- js/actions/UserActions.js | 4 +- js/constants/RestaurantConstants.js | 8 - js/constants/SuggestionConstants.js | 8 + js/models.js | 36 +++-- js/reducers/LunchReducer.js | 4 +- js/reducers/RestaurantReducer.js | 26 --- js/reducers/SuggestionReducer.js | 26 +++ main.go | 2 +- restaurants.go | 143 ---------------- suggestions.go | 152 ++++++++++++++++++ 14 files changed, 237 insertions(+), 215 deletions(-) rename js/actions/{RestaurantActions.js => SuggestionActions.js} (74%) delete mode 100644 js/constants/RestaurantConstants.js create mode 100644 js/constants/SuggestionConstants.js delete mode 100644 js/reducers/RestaurantReducer.js create mode 100644 js/reducers/SuggestionReducer.js delete mode 100644 restaurants.go create mode 100644 suggestions.go diff --git a/attendees.go b/attendees.go index 9e927a8..bc12edf 100644 --- a/attendees.go +++ b/attendees.go @@ -5,11 +5,14 @@ import ( "log" "net/http" "strings" + "time" ) type Attendee struct { AttendeeId int64 + UserId int64 `json:"-"` Name string + Date time.Time `json:"-"` } type AttendeeList struct { @@ -37,10 +40,10 @@ func (aeu AttendeeExistsError) Error() string { return "Attendee exists" } -func GetAttendees() (*[]*Attendee, error) { +func GetAttendees(userid int64, date time.Time) (*[]*Attendee, error) { var attendees []*Attendee - _, err := DB.Select(&attendees, "SELECT * from attendees") + _, err := DB.Select(&attendees, "SELECT * from attendees WHERE UserId=? AND Date=?", userid, date) if err != nil { return nil, err } @@ -79,12 +82,14 @@ func InsertAttendee(a *Attendee) error { } func AttendeeHandler(w http.ResponseWriter, r *http.Request) { - _, err := GetUserFromSession(r) + user, err := GetUserFromSession(r) if err != nil { WriteError(w, 1 /*Not Signed In*/) return } + today := time.Now().Truncate(time.Hour * 24) + if r.Method == "POST" { attendee_json := r.PostFormValue("attendee") if attendee_json == "" { @@ -99,6 +104,8 @@ func AttendeeHandler(w http.ResponseWriter, r *http.Request) { return } attendee.AttendeeId = -1 + attendee.UserId = user.UserId + attendee.Date = today err = InsertAttendee(&attendee) if err != nil { @@ -121,7 +128,7 @@ func AttendeeHandler(w http.ResponseWriter, r *http.Request) { } else if r.Method == "GET" { var al AttendeeList - attendees, err := GetAttendees() + attendees, err := GetAttendees(user.UserId, today) if err != nil { WriteError(w, 999 /*Internal Error*/) log.Print(err) diff --git a/db.go b/db.go index 98eb50a..4f1afa9 100644 --- a/db.go +++ b/db.go @@ -19,7 +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") + dbmap.AddTableWithName(Suggestion{}, "suggestions").SetKeys(true, "SuggestionId") err = dbmap.CreateTablesIfNotExists() if err != nil { diff --git a/errors.go b/errors.go index 6d8f34c..a677231 100644 --- a/errors.go +++ b/errors.go @@ -17,7 +17,7 @@ var error_codes = map[int]string{ 3: "Invalid Request", 4: "User Exists", 5: "Attendee Exists", - 6: "Restaurant Exists", + 6: "Suggestion Exists", // 5: "Connection Failed", //client-side error 999: "Internal Error", } diff --git a/js/actions/RestaurantActions.js b/js/actions/SuggestionActions.js similarity index 74% rename from js/actions/RestaurantActions.js rename to js/actions/SuggestionActions.js index fac8e3c..320f080 100644 --- a/js/actions/RestaurantActions.js +++ b/js/actions/SuggestionActions.js @@ -1,40 +1,40 @@ -var RestaurantConstants = require('../constants/RestaurantConstants'); +var SuggestionConstants = require('../constants/SuggestionConstants'); var ErrorActions = require('./ErrorActions'); var models = require('../models.js'); -var Restaurant = models.Restaurant; +var Suggestion = models.Suggestion; var Error = models.Error; -function fetchRestaurants() { +function fetchSuggestions() { return { - type: RestaurantConstants.FETCH_RESTAURANTS + type: SuggestionConstants.FETCH_SUGGESTIONS } } function restaurantsFetched(restaurants) { return { - type: RestaurantConstants.RESTAURANTS_FETCHED, + type: SuggestionConstants.SUGGESTIONS_FETCHED, restaurants: restaurants } } -function createRestaurant() { +function createSuggestion() { return { - type: RestaurantConstants.CREATE_RESTAURANT + type: SuggestionConstants.CREATE_SUGGESTION } } function restaurantCreated(restaurant) { return { - type: RestaurantConstants.RESTAURANT_CREATED, + type: SuggestionConstants.SUGGESTION_CREATED, restaurant: restaurant } } function fetchAll() { return function (dispatch) { - dispatch(fetchRestaurants()); + dispatch(fetchSuggestions()); $.ajax({ type: "GET", @@ -47,7 +47,7 @@ function fetchAll() { ErrorActions.serverError(e); } else { dispatch(restaurantsFetched(data.restaurants.map(function(json) { - var a = new Restaurant(); + var a = new Suggestion(); a.fromJSON(json); return a; }))); @@ -62,7 +62,7 @@ function fetchAll() { function create(restaurant) { return function (dispatch) { - dispatch(createRestaurant()); + dispatch(createSuggestion()); $.ajax({ type: "POST", @@ -75,7 +75,7 @@ function create(restaurant) { if (e.isError()) { ErrorActions.serverError(e); } else { - var a = new Restaurant(); + var a = new Suggestion(); a.fromJSON(data); dispatch(restaurantCreated(a)); } diff --git a/js/actions/UserActions.js b/js/actions/UserActions.js index c7929b0..65f6b2b 100644 --- a/js/actions/UserActions.js +++ b/js/actions/UserActions.js @@ -1,7 +1,7 @@ var UserConstants = require('../constants/UserConstants'); var AttendeeActions = require('./AttendeeActions'); -var RestaurantActions = require('./RestaurantActions'); +var SuggestionActions = require('./SuggestionActions'); var ErrorActions = require('./ErrorActions'); var models = require('../models.js'); @@ -92,7 +92,7 @@ function initializeSession(dispatch, session) { dispatch(userLoggedIn(session)); dispatch(fetch(session.UserId)); dispatch(AttendeeActions.fetchAll()); - dispatch(RestaurantActions.fetchAll()); + dispatch(SuggestionActions.fetchAll()); } function login(user) { diff --git a/js/constants/RestaurantConstants.js b/js/constants/RestaurantConstants.js deleted file mode 100644 index a755478..0000000 --- a/js/constants/RestaurantConstants.js +++ /dev/null @@ -1,8 +0,0 @@ -var keyMirror = require('keymirror'); - -module.exports = keyMirror({ - FETCH_RESTAURANTS: null, - RESTAURANTS_FETCHED: null, - CREATE_RESTAURANT: null, - RESTAURANT_CREATED: null -}); diff --git a/js/constants/SuggestionConstants.js b/js/constants/SuggestionConstants.js new file mode 100644 index 0000000..c3def0b --- /dev/null +++ b/js/constants/SuggestionConstants.js @@ -0,0 +1,8 @@ +var keyMirror = require('keymirror'); + +module.exports = keyMirror({ + FETCH_SUGGESTIONS: null, + SUGGESTIONS_FETCHED: null, + CREATE_SUGGESTION: null, + SUGGESTION_CREATED: null +}); diff --git a/js/models.js b/js/models.js index e21a67f..387a9e7 100644 --- a/js/models.js +++ b/js/models.js @@ -101,31 +101,37 @@ Attendee.prototype.isAttendee = function() { this.Name != empty_attendee.Name; } -function Restaurant() { - this.RestaurantId = -1; - this.Name = ""; +function Suggestion() { + this.SuggestionId = -1; + this.VetoingId = -1; + this.AttendeeId = -1; + this.RestaurantName = ""; } -Restaurant.prototype.toJSON = function() { +Suggestion.prototype.toJSON = function() { var json_obj = {}; - json_obj.RestaurantId = this.RestaurantId; - json_obj.Name = this.Name; + json_obj.SuggestionId = this.SuggestionId; + json_obj.AttendeeId = this.AttendeeId; + json_obj.RestaurantName = this.RestaurantName; return JSON.stringify(json_obj); } -Restaurant.prototype.fromJSON = function(json_input) { +Suggestion.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; + if (json_obj.hasOwnProperty("SuggestionId")) + this.SuggestionId = json_obj.SuggestionId; + if (json_obj.hasOwnProperty("AttendeeId")) + this.AttendeeId = json_obj.AttendeeId; + if (json_obj.hasOwnProperty("RestaurantName")) + this.RestaurantName = json_obj.RestaurantName; } -Restaurant.prototype.isRestaurant = function() { - var empty_attendee = new Restaurant(); - return this.RestaurantId != empty_attendee.RestaurantId || - this.Name != empty_attendee.Name; +Suggestion.prototype.isSuggestion = function() { + var empty_suggestion = new Suggestion(); + return this.SuggestionId != empty_suggestion.SuggestionId && + this.AttendeeId != empty_suggestion.AttendeeId && + this.RestaurantName != empty_suggestion.RestaurantName; } function Error() { diff --git a/js/reducers/LunchReducer.js b/js/reducers/LunchReducer.js index 9abcaff..303a40f 100644 --- a/js/reducers/LunchReducer.js +++ b/js/reducers/LunchReducer.js @@ -3,13 +3,13 @@ var Redux = require('redux'); var UserReducer = require('./UserReducer'); var SessionReducer = require('./SessionReducer'); var AttendeeReducer = require('./AttendeeReducer'); -var RestaurantReducer = require('./RestaurantReducer'); +var SuggestionReducer = require('./SuggestionReducer'); var ErrorReducer = require('./ErrorReducer'); module.exports = Redux.combineReducers({ user: UserReducer, session: SessionReducer, attendees: AttendeeReducer, - restaurants: RestaurantReducer, + suggestions: SuggestionReducer, error: ErrorReducer }); diff --git a/js/reducers/RestaurantReducer.js b/js/reducers/RestaurantReducer.js deleted file mode 100644 index b0e23b5..0000000 --- a/js/reducers/RestaurantReducer.js +++ /dev/null @@ -1,26 +0,0 @@ -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; - } -}; diff --git a/js/reducers/SuggestionReducer.js b/js/reducers/SuggestionReducer.js new file mode 100644 index 0000000..38731ed --- /dev/null +++ b/js/reducers/SuggestionReducer.js @@ -0,0 +1,26 @@ +var assign = require('object-assign'); + +var SuggestionConstants = require('../constants/SuggestionConstants'); +var UserConstants = require('../constants/UserConstants'); + +module.exports = function(state = {}, action) { + switch (action.type) { + case SuggestionConstants.SUGGESTIONS_FETCHED: + var suggestions = {}; + for (var i = 0; i < action.suggestions.length; i++) { + var suggestion = action.suggestions[i]; + suggestions[suggestion.SuggestionId] = suggestion; + } + return suggestions; + case SuggestionConstants.SUGGESTION_CREATED: + var suggestion = action.suggestion; + var suggestions = assign({}, state, { + [suggestion.SuggestionId]: suggestion + }); + return suggestions; + case UserConstants.USER_LOGGEDOUT: + return {}; + default: + return state; + } +}; diff --git a/main.go b/main.go index d48b7ce..c3ae1e4 100644 --- a/main.go +++ b/main.go @@ -70,7 +70,7 @@ func main() { servemux.HandleFunc("/session/", SessionHandler) servemux.HandleFunc("/user/", UserHandler) servemux.HandleFunc("/attendee/", AttendeeHandler) - servemux.HandleFunc("/restaurant/", RestaurantHandler) + servemux.HandleFunc("/suggestion/", SuggestionHandler) listener, err := net.Listen("tcp", ":"+strconv.Itoa(port)) if err != nil { diff --git a/restaurants.go b/restaurants.go deleted file mode 100644 index 7e4313c..0000000 --- a/restaurants.go +++ /dev/null @@ -1,143 +0,0 @@ -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 - } -} diff --git a/suggestions.go b/suggestions.go new file mode 100644 index 0000000..37581e3 --- /dev/null +++ b/suggestions.go @@ -0,0 +1,152 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" + "strings" + "time" +) + +type Suggestion struct { + SuggestionId int64 + VetoingId int64 // -1 for initial suggestion + AttendeeId int64 + UserId int64 `json:"-"` + RestaurantName string + Date time.Time `json:"-"` +} + +type SuggestionList struct { + Suggestions *[]*Suggestion `json:"suggestions"` +} + +func (s *Suggestion) Write(w http.ResponseWriter) error { + enc := json.NewEncoder(w) + return enc.Encode(s) +} + +func (s *Suggestion) Read(json_str string) error { + dec := json.NewDecoder(strings.NewReader(json_str)) + return dec.Decode(s) +} + +func (sl *SuggestionList) Write(w http.ResponseWriter) error { + enc := json.NewEncoder(w) + return enc.Encode(sl) +} + +type SuggestionExistsError struct{} + +func (aeu SuggestionExistsError) Error() string { + return "Suggestion exists" +} + +func GetSuggestions(userid int64, date time.Time) (*[]*Suggestion, error) { + var suggestions []*Suggestion + + _, err := DB.Select(&suggestions, "SELECT * from suggestions WHERE UserId=? AND Date=?", userid, date) + if err != nil { + return nil, err + } + return &suggestions, nil +} + +func InsertSuggestion(s *Suggestion) error { + transaction, err := DB.Begin() + if err != nil { + return err + } + + existing, err := transaction.SelectInt("SELECT count(*) from suggestions where RestaurantName=? AND UserId=? AND Date=?", s.RestaurantName, s.UserId, s.Date) + if err != nil { + transaction.Rollback() + return err + } + if existing > 0 { + transaction.Rollback() + return SuggestionExistsError{} + } + + err = transaction.Insert(s) + if err != nil { + transaction.Rollback() + return err + } + + err = transaction.Commit() + if err != nil { + transaction.Rollback() + return err + } + + return nil +} + +func SuggestionHandler(w http.ResponseWriter, r *http.Request) { + user, err := GetUserFromSession(r) + if err != nil { + WriteError(w, 1 /*Not Signed In*/) + return + } + + today := time.Now().Truncate(time.Hour * 24) + + if r.Method == "POST" { + suggestion_json := r.PostFormValue("suggestion") + if suggestion_json == "" { + WriteError(w, 3 /*Invalid Request*/) + return + } + + var suggestion Suggestion + err := suggestion.Read(suggestion_json) + if err != nil { + WriteError(w, 3 /*Invalid Request*/) + return + } + suggestion.SuggestionId = -1 + suggestion.UserId = user.UserId + suggestion.Date = today + + err = InsertSuggestion(&suggestion) + if err != nil { + if _, ok := err.(SuggestionExistsError); ok { + WriteError(w, 6 /*Suggestion Exists*/) + } else { + WriteError(w, 999 /*Internal Error*/) + log.Print(err) + } + return + } + + w.WriteHeader(201 /*Created*/) + err = suggestion.Write(w) + if err != nil { + WriteError(w, 999 /*Internal Error*/) + log.Print(err) + return + } + } else if r.Method == "GET" { + var sl SuggestionList + + suggestions, err := GetSuggestions(user.UserId, today) + if err != nil { + WriteError(w, 999 /*Internal Error*/) + log.Print(err) + return + } + + sl.Suggestions = suggestions + err = (&sl).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 + } +}