Esempio n. 1
0
        public async Task <ActionResult> Details(string id)
        {
            //check for null or empty id value
            if (id == null || id.Length == 0)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            JsonResponseObject imageResponse = await webAPIHelper.JsonRestGet <JsonResponseObject>(szBaseURL, (PublicWebApiUrl.API_RANDOM_BREED_IMAGE).Replace("{var-id}", id));

            // check response for error
            // the JsonRestGet could ALSO return an error collection if something went wrong in the API and we could poll that and return an Error view as well.
            if (!(string.IsNullOrEmpty(imageResponse.status)) && imageResponse.status.ToUpper() == "SUCCESS")
            {
                // Instantiate new Dog Object and Assign Image & Breed
                Dog dog = new Dog
                {
                    ImageUrl = imageResponse.message,
                    Breed    = id
                };
                //returning the Dog Object To The View
                return(View(dog));
            }
            return(View("Error"));
        }
Esempio n. 2
0
        [Route("cheditor/api/deleteclass/{cid}")] // Need this because passed variable was changed from id to cid
        public IHttpActionResult DeleteClass(int cid)
        {
            APIClass TargetClass = db.APIClasses.Find(cid);

            // Checking if target exists
            if (TargetClass == null)
            {
                JsonResponseObject faliureJsonResult = new JsonResponseObject()
                {
                    result  = false,
                    message = "Target class ID does not exist"
                };
                return(Json(faliureJsonResult));
            }

            // Checking if is not a parent
            if (che.IsAParent(cid))
            {
                JsonResponseObject faliureJsonResult = new JsonResponseObject()
                {
                    result  = false,
                    message = "Target class is a parent, please delete its children first"
                };
                return(Json(faliureJsonResult));
            }

            // Remove from context
            db.APIClasses.Remove(TargetClass);
            // Commit changes
            db.SaveChanges();

            return(Ok(TargetClass));
        }
Esempio n. 3
0
        [Route("cheditor/api/superclasses/{cid}")] // Need this because passed variable was changed from id to cid
        public IHttpActionResult Superclasses(int cid)
        //public string Superclasses(int cid)
        {
            //return che.GetSuperClasses(cid);

            // Creat list to store classes
            List <APIClass> list = new List <APIClass>();

            APIClass currentClass = db.APIClasses.Find(cid);

            int i = 1;

            while (i != 0)                // Condition is always true, but all paths lead to a 'return' statement eventually!
            {
                if (currentClass == null) // If passed cid does not exist
                {
                    if (list == null)     // if list is empty
                    {
                        // create an error response body to be srialized into json
                        JsonResponseObject faliureJsonResult = new JsonResponseObject()
                        {
                            result  = false,
                            message = "No parent classes found"
                        };
                        // Serialise and return error response body
                        return(Json(faliureJsonResult));
                    }
                    else
                    {
                        // Returning the list of classes
                        return(Json(list));
                    }
                }

                // Find parent class where currentClass.pid = cid
                // Find uses passed value to lookup in the primary key field in the DBContext
                APIClass parentClass = db.APIClasses.Find(currentClass.pid);

                // If no parent class found
                if (parentClass == null)//
                {
                    // Return the list of classess as a JSON object
                    return(Json(list));
                }
                // Add parent class to list
                list.Add(parentClass);

                // Set parent class as current, in preparation for hte next iteration
                currentClass = parentClass;
            }//END OF WHILE
            // Return the list of classess as a JSON object
            return(Json(list));
        }
Esempio n. 4
0
        [Route("cheditor/api/subclasses/{cid}")] // Need this cus passed variable was changed from id to cid
        public IHttpActionResult SubClasses(int?cid)
        {
            if (!db.APIClasses.Any(e => e.cid == cid))
            {
                JsonResponseObject faliureJsonResult = new JsonResponseObject()
                {
                    result  = false,
                    message = "Class ID does not exist"
                };
                return(Json(faliureJsonResult));
            }

            return(Json(che.GetSubClasses(cid)));

            /*
             * // Create a list
             * List<APIClass> list = new List<APIClass>();
             *
             * // find the
             *
             * APIClass currentClass = db.APIClasses.Find(cid);
             * if (currentClass != null)
             * {
             *  int? parentID = currentClass.pid;
             *
             *  APIClass parentClass = db.APIClasses.Find(cid);
             *
             *  if (parentClass == null)
             *  {
             *      if (list == null)
             *      {
             *          JsonResponseObject faliureJsonResult = new JsonResponseObject()
             *          {
             *              result = false,
             *              message = "No parent classes found"
             *          };
             *          return Json(faliureJsonResult);
             *      }
             *      else
             *      {
             *          return Json(list);
             *      }
             *  }
             * }
             * list.Add(parentClass);
             * SuperClasses((int)parentID);
             */
        }
Esempio n. 5
0
        public IHttpActionResult GetClass(int id)
        {
            APIClass foundClass = db.APIClasses.Find(id);

            if (foundClass == null)
            {
                JsonResponseObject faliureJsonResult = new JsonResponseObject()
                {
                    result  = false,
                    message = "Target class ID does not exist"
                };
                return(Json(faliureJsonResult));
            }

            return(Json(foundClass)); // ORIGINAL: works perfectly
        }
Esempio n. 6
0
        public ActionResult UpdateSettingCategory(SystemConfigCategory configCategory)
        {
            var response = new JsonResponseObject();

            try
            {
                response.Data = _systemConfigCategoryHandler.Update(ref configCategory);
            }
            catch (Exception ex)
            {
                response.Success   = false;
                response.Exception = ex;
                response.Message   = ex.Message;
            }

            return(new JsonNetResult {
                Data = response
            });
        }
        public static bool Validate(string mainresponse, string privatekey)
        {
            try {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/recaptcha/api/siteverify?secret=" + privatekey + "&response=" + mainresponse);

                WebResponse response = req.GetResponse();

                using (StreamReader readStream = new StreamReader(response.GetResponseStream())) {
                    string jsonResponse = readStream.ReadToEnd();

                    JsonResponseObject jobj = JsonConvert.DeserializeObject <JsonResponseObject>(jsonResponse);


                    return(jobj.success);
                }
            }
            catch (Exception) {
                return(false);
            }
        }
Esempio n. 8
0
        public ActionResult CreateSetting(SystemConfig config)
        {
            var response = new JsonResponseObject();

            try
            {
                response.Data = _systemConfigHandler.Create(ref config);
            }
            catch (Exception ex)
            {
                response.Success   = false;
                response.Exception = ex;
                response.Message   = ex.Message;
            }

            var jsonNetResult = new JsonNetResult {
                Data = response
            };

            return(jsonNetResult);
        }
Esempio n. 9
0
        [Route("cheditor/api/addclassesjson")] // Need this because passed variable was changed from id to cid
        public IHttpActionResult Addclassesjson(APIClass jsonReq)
        {
            /*
             * string jsonReq = "[{classes:[{ \"cid\": \"1\",\"name\": \"Vehicle\",\"abstract\": \"true\"}," +
             *               "{ \"cid\": \"2\",\"name\": \"Vehicle2\",\"pid\": \"1\",\"abstract\": \"true\"}" +
             *               "]}]";
             */

            JsonResponseObject jro = new JsonResponseObject
            {
                result  = false,
                message = "This service is still under construction!"
            };

            return(Json(jro));

            /*
             * for(int i = 0; i<classReq.Count(); i++)
             * {
             *  //ADD VALIDATION OF CID, NAME AND PID,
             *  // COPY THEM FROM ADDCLASS ABOVE
             *
             *
             *  db.APIClasses.Add(classReq[i]);
             * }
             *
             * try
             * {
             *  // Commiting changes to DB
             *  db.SaveChanges();
             * }
             * catch (DbUpdateException)
             * {
             *
             * }
             */
        }
Esempio n. 10
0
        public IHttpActionResult AddClass(int?cid, string name, bool Abstract, int?pid = null)
        // int? = nullable int, pid = null: makes it possible to not add pid in url
        {
            // setting as first level class
            if (pid == null)
            {
                pid = 0;
            }

            // Check CID != PID
            if (cid == pid)
            {
                JsonResponseObject jro = new JsonResponseObject
                {
                    result  = false,
                    message = "Class ID cannot be the same as Parent ID"
                };
                return(Json(jro));
            }

            // Checking if CID and Name are not Nulls
            if (cid == null | name == null)
            {
                JsonResponseObject jro = new JsonResponseObject
                {
                    result  = false,
                    message = "Class ID and/or Name cannot be null"
                };
                return(Json(jro));
            }

            // Checking if name is available
            if (!che.IsNameAvailable(name))
            {
                JsonResponseObject jro = new JsonResponseObject
                {
                    result  = false,
                    message = "Class name already exists"
                };
                return(Json(jro));
            }

            // Checking if pid exists
            if (!che.IsParentAvailable((int)pid))
            {
                JsonResponseObject jro = new JsonResponseObject
                {
                    result  = false,
                    message = "Parent class does not exist"
                };
                return(Json(jro));
            }

            // Adding request values to a class object
            APIClass AddClass = new APIClass
            {
                cid        = (int)cid,
                name       = name,
                isAbstract = Abstract,
                pid        = pid
            };

            // Adding created obj to DBContext
            db.APIClasses.Add(AddClass);

            try
            {
                // Commiting changes to DB
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                // Checking if cid is available
                // Dublicate CID will invoke DbUpdateException as CID is a primary key
                if (!che.IsCidAvailable((int)cid))
                {
                    JsonResponseObject jro = new JsonResponseObject
                    {
                        result  = false,
                        message = "Class ID already exists"
                    };
                    return(Json(jro));
                }
            }
            return(CreatedAtRoute("DefaultApi", new { id = AddClass.cid }, AddClass));
        }
Esempio n. 11
0
        [Route("cheditor/api/addclassjson")] // Need this because passed variable was changed from id to cid
        public IHttpActionResult AddClassJson(APIClass jsonReq)
        {
            //string jsonReq = "{ \"cid\": \"1\",\"name\": \"Vehicle\",\"abstract\": \"true\"}";

            // Deserialisong Json object into a class model object
            //APIClass classReq = JsonConvert.DeserializeObject<APIClass>(jsonReq);

            try
            {
                // Below statement returns throws an exception if invalid JSON obj was received

                // setting null pid to a first level class
                if (jsonReq.pid == null)
                {
                    jsonReq.pid = 0;
                }
            } catch (Exception)
            {
                JsonResponseObject jro = new JsonResponseObject
                {
                    result  = false,
                    message = "Sent JSON object not valid"
                };
                return(Json(jro));
            }

            // Check CID != PID
            if (jsonReq.cid == jsonReq.pid)
            {
                JsonResponseObject jro = new JsonResponseObject
                {
                    result  = false,
                    message = "Class ID cannot be the same as Parent ID"
                };
                return(Json(jro));
            }

            // Custom made Class model validity check
            // Validates manditory attributes
            // Validates CID and Name repototion and PID existance in DB
            string APIValidityCheck = che.APIClassValidityCheck(jsonReq.cid, jsonReq.name, jsonReq.pid);


            if (APIValidityCheck == "")
            {
                db.APIClasses.Add(jsonReq);
                try
                {
                    // Commiting changes to DB
                    db.SaveChanges();
                    return(Json("{\"ret\": \"true\"}"));
                }
                catch (DbUpdateException)
                {
                    JsonResponseObject jro = new JsonResponseObject
                    {
                        result  = false,
                        message = "An error occured while committing changes to database."
                    };
                    return(Json(jro));
                }
            }
            else
            {
                JsonResponseObject jro = new JsonResponseObject
                {
                    result  = false,
                    message = APIValidityCheck
                };
                return(Json(jro));
            }
        }