public JsonResult Index()
        {
            ConvertibleHashtable h  = ConvertibleHashtable.fromRequest();
            ConvertibleHashtable ud = ConvertibleHashtable.fromJObject((JObject)h["user_data"]);
            ApiKey k = ApiKey.getApiKey();

            if (k.isAdmin())
            {
                if (h["type"].ToString() == "supplier")
                {
                    ud.merge(((JObject)h["supplier_data"]).ToObject <ConvertibleHashtable>());
                    if (ud.ContainsKey("user_id"))
                    {
                        ud.Remove("user_id");
                    }
                    ud.toObject <Supplier>().insert();
                }
                else if (h["type"].ToString() == "admin")
                {
                    ud.toObject <Admin>().insert();
                }
                else
                {
                    return(Json(Costants.WRONG_USER_TYPE, JsonRequestBehavior.AllowGet));
                }
                return(Json(Costants.OK, JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json(Costants.UNAUTHORIZED, JsonRequestBehavior.AllowGet));
            }
        }
        /** Aggiorna una categoria
         *
         *  API Reference: POST /api/categories/update/<id>/
         *
         *  Requisiti: admin api_key negli header http
         *
         *  JSON Data: {name: "name"} */

        public JsonResult Update(int id)
        {
            if (!ApiKey.getApiKey().isAdmin())
            {
                return(Json(Costants.UNAUTHORIZED, JsonRequestBehavior.AllowGet));
            }
            else
            {
                ConvertibleHashtable newData     = ConvertibleHashtable.fromRequest();
                ConvertibleHashtable currentData = Model.getHashtableById <ProductCategory>(id);
                currentData.update(newData);
                currentData.toObject <ProductCategory>().update();
                return(Json(Costants.OK, JsonRequestBehavior.AllowGet));
            }
        }
Example #3
0
        public JsonResult Update(int id)
        {
            ApiKey k = ApiKey.getApiKey();
            ConvertibleHashtable newData     = ConvertibleHashtable.fromRequest();
            ConvertibleHashtable currentData = Model.getHashtableById <Product>(id);

            if (k.isAdmin() || Supplier.checkUserId(k.user_id, int.Parse(currentData["supplier_id"].ToString())))
            {
                currentData.update(newData);
                currentData.toObject <Product>().update();
                return(Json(Costants.OK, JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json(Costants.UNAUTHORIZED, JsonRequestBehavior.AllowGet));
            }
        }
        /** Ritorna i dettagli di un utente
         *
         *  Nota: ritorna anche le info sui supplier
         *
         *  API Reference: GET /api/users/detail/<id>/
         *
         *  Requisiti: api_key negli headers http */
        public JsonResult Detail(int id)
        {
            ApiKey k = ApiKey.getApiKey();

            if (k.isAdmin() || k.checkUser(id))
            {
                ConvertibleHashtable h = Model.getHashtableById <User>(id);
                if (h.toObject <User>().type == userType.supplier)
                {
                    h = Supplier.getHashtableByUserId(id);
                }
                return(Json(h.filterPassword(), JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json(Costants.UNAUTHORIZED, JsonRequestBehavior.AllowGet));
            }
        }
Example #5
0
        /** Aggiorna uno stock
         *
         *  Api Reference: POST /api/stocks/update/<id>/
         *
         *  Requisiti: admin api_key negli header http
         *
         *  JSON Data: {product_id:<id>, price:<pr>, min:<min>, max:<max>, availability:<av>} */

        public JsonResult Update(int id)
        {
            ApiKey k = ApiKey.getApiKey();
            ConvertibleHashtable newData     = ConvertibleHashtable.fromRequest();
            ConvertibleHashtable currentData = Model.getHashtableById <ProductStock>(id);

            currentData.update(newData);
            ProductStock s = currentData.toObject <ProductStock>();

            if (k.isAdmin() || s.checkUserId(k.user_id))
            {
                s.update();
                return(Json(Costants.OK, JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json(Costants.UNAUTHORIZED, JsonRequestBehavior.AllowGet));
            }
        }
        public JsonResult Update(int id)
        {
            ConvertibleHashtable h = ConvertibleHashtable.fromRequest();
            ApiKey k = ApiKey.getApiKey();

            if (k.isAdmin() || k.checkUser(id))
            {
                // controlla se si deve modificare le informazioni specifiche di un supplier
                ConvertibleHashtable supplierCurrentData = Supplier.getHashtableByUserId(id);
                if (h.ContainsKey("supplier_data") && supplierCurrentData["user_id"].ToString() == id.ToString())
                {
                    ConvertibleHashtable newData = ConvertibleHashtable.fromJObject((JObject)h["supplier_data"]);
                    if (h.ContainsKey("user_data"))
                    {
                        newData.merge(ConvertibleHashtable.fromJObject((JObject)h["user_data"]));
                    }
                    supplierCurrentData.update(newData);
                    if (!supplierCurrentData.ContainsKey("user_id"))
                    {
                        supplierCurrentData.Add("user_id", id);
                    }
                    supplierCurrentData.toObject <Supplier>().update();
                }
                // modifica le info di base dell'utente, che sia admin o supplier
                else
                {
                    ConvertibleHashtable currentData = Model.getHashtableById <User>(id);
                    ConvertibleHashtable newData     = ConvertibleHashtable.fromJObject((JObject)h["user_data"]);
                    currentData.update(newData);
                    currentData.toObject <User>().update();
                }
                // aggiorna le api key per riflettere evenutali modifiche alla password o alla mail
                ApiKey.fromUserId(id).update();
                return(Json(Costants.OK, JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json(Costants.UNAUTHORIZED, JsonRequestBehavior.AllowGet));
            }
        }