Ejemplo n.º 1
0
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 handleGetCategoreis(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	_, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0])
	if err != servers.STATUS_ERR_OK {
		servers.WriteResponse(w, nil, err)
		return
	}

	cat := GetCategoriesResponse{[]string{"First", "Second", "Garnishes", "Salads", "Desserts"}}

	servers.WriteResponse(w, cat, servers.STATUS_SUCCESS)
}
Ejemplo n.º 3
0
func handleGetUser(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
	}

	userResult := GetUserResponse{token.User.Name, token.User.Email, token.Role}

	servers.WriteResponse(w, userResult, servers.STATUS_SUCCESS)
}
Ejemplo n.º 4
0
func handleGetOffices(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	_, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0])
	if err != servers.STATUS_ERR_OK {
		servers.WriteResponse(w, nil, err)
		return
	}

	off := GetOfficesResponse{[]string{"Paris", "Not Paris"}}

	servers.WriteResponse(w, off, servers.STATUS_SUCCESS)
}
Ejemplo n.º 5
0
func handleGetProviders(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	_, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0])
	if err != servers.STATUS_ERR_OK {
		servers.WriteResponse(w, nil, err)
		return
	}

	providers, queryerr := models.GetAllProviders()
	if queryerr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	res := GetProvidersResponse{providers}
	servers.WriteResponse(w, res, servers.STATUS_SUCCESS)
}
Ejemplo n.º 6
0
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)
}
Ejemplo n.º 7
0
// 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)
}
Ejemplo n.º 8
0
func handleGetOrders(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
	}

	var params GetOrdersParams
	decodeerr := json.NewDecoder(r.Body).Decode(&params)
	if decodeerr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_PARAMS_DECODE)
		return
	}

	menus, queryerr := models.GetOrdersForUserBetween(token.UsersId, params.StartDate, params.EndDate)
	if queryerr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	res := GetOrdersResponse{menus}
	servers.WriteResponse(w, res, servers.STATUS_SUCCESS)
}
Ejemplo n.º 9
0
func handleGetMenus(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	_, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0])
	if err != servers.STATUS_ERR_OK {
		servers.WriteResponse(w, nil, err)
		return
	}

	var params MenuParams
	decodeErr := json.NewDecoder(r.Body).Decode(&params)
	if decodeErr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_PARAMS_DECODE)
		return
	}

	menus, queryErr := models.GetMenuByProvider(params.ProviderId, params.Date)
	if queryErr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	res := GetMenusResponse{menus}
	servers.WriteResponse(w, res, servers.STATUS_SUCCESS)
}
Ejemplo n.º 10
0
func handleGetOrder(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()
	var res interface{}

	orderIdStr := q.Get("order_id")

	orderFromDateStr := q.Get("from_date")
	orderToDateStr := q.Get("to_date")

	orderDateStr := q.Get("order_date")

	if len(orderIdStr) > 0 {
		orderId, converr := strconv.ParseInt(orderIdStr, 10, 64)
		if converr != nil {
			servers.WriteResponse(w, converr.Error(), servers.STATUS_ERR_PARAMS_DECODE)
			return
		}
		order, queryerr := models.GetOrderListForUserAndId(token.UsersId, uint64(orderId))
		if queryerr != nil {
			servers.WriteResponse(w, queryerr.Error(), servers.STATUS_ERR_QUERY_PROBLEM)
			return
		}
		res = GetOrderListResponse{*order}
	} else if len(orderFromDateStr) > 0 && len(orderToDateStr) > 0 {
		orderFromDate, converr := strconv.ParseInt(orderFromDateStr, 10, 64)
		if converr != nil {
			servers.WriteResponse(w, converr.Error(), servers.STATUS_ERR_PARAMS_DECODE)
			return
		}
		orderToDate, converr := strconv.ParseInt(orderToDateStr, 10, 64)
		if converr != nil {
			servers.WriteResponse(w, converr.Error(), servers.STATUS_ERR_PARAMS_DECODE)
			return
		}
		orders, queryerr := models.GetOrderListsForUserBetween(token.UsersId, orderFromDate, orderToDate)
		if queryerr != nil {
			servers.WriteResponse(w, queryerr.Error(), servers.STATUS_ERR_QUERY_PROBLEM)
			return
		}
		res = GetOrderListsResponse{orders}
	} else if len(orderDateStr) > 0 {
		orderDate, converr := strconv.ParseInt(orderDateStr, 10, 64)
		if converr != nil {
			servers.WriteResponse(w, converr.Error(), servers.STATUS_ERR_PARAMS_DECODE)
			return
		}
		order, queryerr := models.GetOrderListForUserAndDate(token.UsersId, orderDate)
		if queryerr != nil {
			servers.WriteResponse(w, queryerr.Error(), servers.STATUS_ERR_QUERY_PROBLEM)
			return
		}
		res = GetOrderListResponse{*order}
	}

	servers.WriteResponse(w, res, servers.STATUS_SUCCESS)
}