1
0
Fork 0

Add attendees and suggestions (except for UI)

This commit is contained in:
Aaron Lindsay 2016-12-26 08:29:18 -05:00
parent c4f07d3b3e
commit c8232050da
11 changed files with 283 additions and 14 deletions

View File

@ -15,10 +15,19 @@ type Attendee struct {
Date time.Time `json:"-"`
}
type PopularAttendee struct {
Name string
Popularity int64
}
type AttendeeList struct {
Attendees *[]*Attendee `json:"attendees"`
}
type PopularAttendeeList struct {
PopularAttendees *[]*PopularAttendee `json:"popularattendees"`
}
func (a *Attendee) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(a)
@ -34,6 +43,16 @@ func (al *AttendeeList) Write(w http.ResponseWriter) error {
return enc.Encode(al)
}
func (pa *PopularAttendee) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(pa)
}
func (pal *PopularAttendeeList) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(pal)
}
type AttendeeExistsError struct{}
func (aeu AttendeeExistsError) Error() string {
@ -50,6 +69,29 @@ func GetAttendees(userid int64, date time.Time) (*[]*Attendee, error) {
return &attendees, nil
}
func GetPopularAttendees() (*[]*PopularAttendee, error) {
var attendees []*Attendee
var attendeeMap map[string]int64
popularAttendees := make([]*PopularAttendee, 0)
_, err := DB.Select(&attendees, "SELECT * from attendees")
if err != nil {
return nil, err
}
for i := range attendees {
attendeeMap[attendees[i].Name] += 1
}
for name, count := range attendeeMap {
var popularAttendee PopularAttendee
popularAttendee.Name = name
popularAttendee.Popularity = count
popularAttendees = append(popularAttendees, &popularAttendee)
}
return &popularAttendees, nil
}
func InsertAttendee(a *Attendee) error {
transaction, err := DB.Begin()
if err != nil {
@ -148,3 +190,28 @@ func AttendeeHandler(w http.ResponseWriter, r *http.Request) {
return
}
}
func PopularAttendeeHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
var pal PopularAttendeeList
attendees, err := GetPopularAttendees()
if err != nil {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
return
}
pal.PopularAttendees = attendees
err = (&pal).Write(w)
if err != nil {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
return
}
} else {
/* No POST, PUT, or DELETE */
WriteError(w, 3 /*Invalid Request*/)
return
}
}

View File

@ -32,6 +32,19 @@ function attendeeCreated(attendee) {
}
}
function fetchPopularAttendees() {
return {
type: AttendeeConstants.FETCH_POPULAR_ATTENDEES
}
}
function popularAttendeesFetched(attendees) {
return {
type: AttendeeConstants.POPULAR_ATTENDEES_FETCHED,
attendees: attendees
}
}
function fetchAll() {
return function (dispatch) {
dispatch(fetchAttendees());
@ -87,7 +100,36 @@ function create(attendee) {
};
}
function fetchPopular() {
return function (dispatch) {
dispatch(fetchPopularAttendees());
$.ajax({
type: "GET",
dataType: "json",
url: "popularattendees/",
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
ErrorActions.serverError(e);
} else {
dispatch(popularAttendeesFetched(data.attendees.map(function(json) {
var a = new Attendee();
a.fromJSON(json);
return a;
})));
}
},
error: function(jqXHR, status, error) {
ErrorActions.ajaxError(e);
}
});
};
}
module.exports = {
fetchAll: fetchAll,
create: create
create: create,
fetchPopular: fetchPopular
};

View File

@ -12,10 +12,10 @@ function fetchSuggestions() {
}
}
function restaurantsFetched(restaurants) {
function suggestionsFetched(suggestions) {
return {
type: SuggestionConstants.SUGGESTIONS_FETCHED,
restaurants: restaurants
suggestions: suggestions
}
}
@ -25,10 +25,23 @@ function createSuggestion() {
}
}
function restaurantCreated(restaurant) {
function suggestionCreated(suggestion) {
return {
type: SuggestionConstants.SUGGESTION_CREATED,
restaurant: restaurant
suggestion: suggestion
}
}
function fetchPopularSuggestions() {
return {
type: SuggestionConstants.FETCH_POPULAR_SUGGESTIONS
}
}
function popularSuggestionsFetched(suggestions) {
return {
type: SuggestionConstants.POPULAR_SUGGESTIONS_FETCHED,
suggestions: suggestions
}
}
@ -39,14 +52,14 @@ function fetchAll() {
$.ajax({
type: "GET",
dataType: "json",
url: "restaurant/",
url: "suggestion/",
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) {
dispatch(suggestionsFetched(data.suggestions.map(function(json) {
var a = new Suggestion();
a.fromJSON(json);
return a;
@ -60,15 +73,15 @@ function fetchAll() {
};
}
function create(restaurant) {
function create(suggestion) {
return function (dispatch) {
dispatch(createSuggestion());
$.ajax({
type: "POST",
dataType: "json",
url: "restaurant/",
data: {restaurant: restaurant.toJSON()},
url: "suggestion/",
data: {suggestion: suggestion.toJSON()},
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
@ -77,7 +90,35 @@ function create(restaurant) {
} else {
var a = new Suggestion();
a.fromJSON(data);
dispatch(restaurantCreated(a));
dispatch(suggestionCreated(a));
}
},
error: function(jqXHR, status, error) {
ErrorActions.ajaxError(e);
}
});
};
}
function fetchPopular() {
return function (dispatch) {
dispatch(fetchPopularSuggestions());
$.ajax({
type: "GET",
dataType: "json",
url: "popularsuggestions/",
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
ErrorActions.serverError(e);
} else {
dispatch(popularSuggestionsFetched(data.suggestions.map(function(json) {
var a = new Suggestion();
a.fromJSON(json);
return a;
})));
}
},
error: function(jqXHR, status, error) {
@ -89,5 +130,6 @@ function create(restaurant) {
module.exports = {
fetchAll: fetchAll,
create: create
create: create,
fetchPopular: fetchPopular
};

View File

@ -93,6 +93,8 @@ function initializeSession(dispatch, session) {
dispatch(fetch(session.UserId));
dispatch(AttendeeActions.fetchAll());
dispatch(SuggestionActions.fetchAll());
dispatch(AttendeeActions.fetchPopular());
dispatch(SuggestionActions.fetchPopular());
}
function login(user) {

View File

@ -4,5 +4,7 @@ module.exports = keyMirror({
FETCH_ATTENDEES: null,
ATTENDEES_FETCHED: null,
CREATE_ATTENDEE: null,
ATTENDEE_CREATED: null
ATTENDEE_CREATED: null,
FETCH_POPULAR_ATTENDEES: null,
POPULAR_ATTENDEES_FETCHED: null
});

View File

@ -4,5 +4,7 @@ module.exports = keyMirror({
FETCH_SUGGESTIONS: null,
SUGGESTIONS_FETCHED: null,
CREATE_SUGGESTION: null,
SUGGESTION_CREATED: null
SUGGESTION_CREATED: null,
FETCH_POPULAR_SUGGESTIONS: null,
POPULAR_SUGGESTIONS_FETCHED: null,
});

View File

@ -3,13 +3,17 @@ var Redux = require('redux');
var UserReducer = require('./UserReducer');
var SessionReducer = require('./SessionReducer');
var AttendeeReducer = require('./AttendeeReducer');
var PopularAttendeeReducer = require('./PopularAttendeeReducer');
var SuggestionReducer = require('./SuggestionReducer');
var PopularSuggestionReducer = require('./PopularSuggestionReducer');
var ErrorReducer = require('./ErrorReducer');
module.exports = Redux.combineReducers({
user: UserReducer,
session: SessionReducer,
attendees: AttendeeReducer,
popularAttendees: PopularAttendeeReducer,
suggestions: SuggestionReducer,
popularSuggestions: PopularSuggestionReducer,
error: ErrorReducer
});

View File

@ -0,0 +1,20 @@
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.POPULAR_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 UserConstants.USER_LOGGEDOUT:
return {};
default:
return state;
}
};

View File

@ -0,0 +1,20 @@
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.POPULAR_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 UserConstants.USER_LOGGEDOUT:
return {};
default:
return state;
}
};

View File

@ -70,7 +70,9 @@ func main() {
servemux.HandleFunc("/session/", SessionHandler)
servemux.HandleFunc("/user/", UserHandler)
servemux.HandleFunc("/attendee/", AttendeeHandler)
servemux.HandleFunc("/popularattendees/", PopularAttendeeHandler)
servemux.HandleFunc("/suggestion/", SuggestionHandler)
servemux.HandleFunc("/popularsuggestions/", PopularSuggestionHandler)
listener, err := net.Listen("tcp", ":"+strconv.Itoa(port))
if err != nil {

View File

@ -17,10 +17,19 @@ type Suggestion struct {
Date time.Time `json:"-"`
}
type PopularSuggestion struct {
RestaurantName string
Popularity int64
}
type SuggestionList struct {
Suggestions *[]*Suggestion `json:"suggestions"`
}
type PopularSuggestionList struct {
PopularSuggestions *[]*PopularSuggestion `json:"popularsuggestions"`
}
func (s *Suggestion) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(s)
@ -42,6 +51,16 @@ func (aeu SuggestionExistsError) Error() string {
return "Suggestion exists"
}
func (s *PopularSuggestion) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(s)
}
func (sl *PopularSuggestionList) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(sl)
}
func GetSuggestions(userid int64, date time.Time) (*[]*Suggestion, error) {
var suggestions []*Suggestion
@ -52,6 +71,28 @@ func GetSuggestions(userid int64, date time.Time) (*[]*Suggestion, error) {
return &suggestions, nil
}
func GetPopularSuggestions() (*[]*PopularSuggestion, error) {
var suggestions []*Suggestion
var suggestionMap map[string]int64
popularSuggestions := make([]*PopularSuggestion, 0)
_, err := DB.Select(&suggestions, "SELECT * from suggestions")
if err != nil {
return nil, err
}
for i := range suggestions {
suggestionMap[suggestions[i].RestaurantName] += 1
}
for name, count := range suggestionMap {
var popularSuggestion PopularSuggestion
popularSuggestion.RestaurantName = name
popularSuggestion.Popularity = count
popularSuggestions = append(popularSuggestions, &popularSuggestion)
}
return &popularSuggestions, nil
}
func InsertSuggestion(s *Suggestion) error {
transaction, err := DB.Begin()
if err != nil {
@ -150,3 +191,28 @@ func SuggestionHandler(w http.ResponseWriter, r *http.Request) {
return
}
}
func PopularSuggestionHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
var sl PopularSuggestionList
suggestions, err := GetPopularSuggestions()
if err != nil {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
return
}
sl.PopularSuggestions = suggestions
err = (&sl).Write(w)
if err != nil {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
return
}
} else {
/* No POST, PUT, or DELETE */
WriteError(w, 3 /*Invalid Request*/)
return
}
}