func handleGetFavorites(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { token, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0]) if err != servers.STATUS_ERR_OK { servers.WriteResponse(w, nil, err) return } favorites, queryerr := models.LoadFavoritesFromDB(token.UsersId) if queryerr != nil { servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM) return } error := favorites.LoadDishesFromDB() if error != nil { servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM) return } usersDishes, queryerr2 := models.GetUsersDishes(token.UsersId, favorites.Dishes) if queryerr2 != nil { servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM) return } servers.WriteResponse(w, usersDishes, servers.STATUS_SUCCESS) }
func handleRemoveFavorites(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { token, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0]) if err != servers.STATUS_ERR_OK { servers.WriteResponse(w, nil, err) return } idStr := ps.ByName("dishId") dishId, convErr := strconv.ParseUint(idStr, 10, 64) if convErr != nil { servers.WriteResponse(w, nil, servers.STATUS_ERR_PARAMS_DECODE) return } favorites, queryerr := models.LoadFavoritesFromDB(token.UsersId) if queryerr != nil { servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM) return } favorites.RemoveDish(dishId) queryerr = favorites.LoadDishesFromDB() if queryerr != nil { servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM) return } queryerr = favorites.SaveToDB() if queryerr != nil { servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM) return } usersDishes, queryerr2 := models.GetUsersDishes(token.UsersId, favorites.Dishes) if queryerr2 != nil { servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM) return } servers.WriteResponse(w, usersDishes, servers.STATUS_SUCCESS) }
// params: // providerId // date func handleGetDishesByProviderAndDate(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { token, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0]) if err != servers.STATUS_ERR_OK { servers.WriteResponse(w, nil, err) return } q := r.URL.Query() idstr := q.Get("providerId") providerId, converr1 := strconv.ParseUint(idstr, 10, 64) if converr1 != nil { servers.WriteResponse(w, nil, servers.STATUS_ERR_PARAMS_DECODE) return } dateStr := q.Get("date") date, converr2 := strconv.ParseInt(dateStr, 10, 64) if converr2 != nil { servers.WriteResponse(w, nil, servers.STATUS_ERR_PARAMS_DECODE) return } dishes, queryerr1 := models.GetDishesByProviderAndDate(providerId, date) if queryerr1 != nil { servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM) return } usersDishes, queryerr2 := models.GetUsersDishes(token.UsersId, dishes) if queryerr2 != nil { servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM) return } res := GetDishesResponse{usersDishes} servers.WriteResponse(w, res, servers.STATUS_SUCCESS) }