コード例 #1
0
        //update
        public void Put(int id, [FromBody] TopSpot updatedTopSpot)
        {
            // get file name
            string fileName = HttpContext.Current.Server.MapPath("../topspots.json");

            //read json from file system
            string json = File.ReadAllText(fileName);

            //deserialize
            List <TopSpot> topSpots = JsonConvert.DeserializeObject <List <TopSpot> >(json);

            //find top spot to be udpated
            foreach (var topSpot in topSpots)
            {
                if (topSpot.Id == updatedTopSpot.Id)
                {
                    topSpot.Name        = updatedTopSpot.Name;
                    topSpot.Description = updatedTopSpot.Description;
                    topSpot.Location    = updatedTopSpot.Location;
                }
            }

            //modify the properties


            //turn list back into json
            json = JsonConvert.SerializeObject(topSpots);

            //save it to .json file
            File.WriteAllText(fileName, json);
        }
コード例 #2
0
        // PUT: api/TopSpots/5
        public void Put(int id, [FromBody] TopSpot updatedTopSpot)
        {
            // 1. Get the filename
            string fileName = HttpContext.Current.Server.MapPath("../App_Data/topspots.json");

            // 2. Read the JSON from the file system
            string json = File.ReadAllText(fileName);

            // 3. Deserialize
            List <TopSpot> topSpots = JsonConvert.DeserializeObject <List <TopSpot> >(json);

            // 4. Find the top spot to be updated
            foreach (var topSpot in topSpots)
            {
                if (topSpot.Id == updatedTopSpot.Id)
                {
                    // 5. Modify the properties
                    topSpot.Name        = updatedTopSpot.Name;
                    topSpot.Description = updatedTopSpot.Description;
                    topSpot.Location    = updatedTopSpot.Location;
                }
            }

            // 6. Turn the list back into JSON
            json = JsonConvert.SerializeObject(topSpots);

            // 7. Write the JSON back to the file system.
            File.WriteAllText(fileName, json);
        }
コード例 #3
0
        // POST: api/TopSpots
        // POST will involve reading the JSON from the file system, adding a TopSpot object to the end of the array and then saving the array back to the file system.
        public void Post([FromBody] TopSpot value)
        {
            // Take text object array and save it back to the file system.
            // File.WriteAllText(@"Y:\Github\13-TopSpotsAPI\App_Data\topspots.json", text);
            // Read the XML posted content from the file system and converting it into a JSON format and assign it to the string text variable object.

            // 1. Get file Name
            string fileName = HttpContext.Current.Server.MapPath("../App_Data/topspots.json");

            // 2. Read the JSON from the file System
            string json = File.ReadAllText(fileName);

            // 3. Deserialize - takes JSON and converts to C# objects.
            // Use List type Class that inherits from ICollections
            List <TopSpot> topSpots = JsonConvert.DeserializeObject <List <TopSpot> >(json);

            // 4. Add the top spot to the list
            // Value in this case is the topSpots being passed in.
            topSpots.Add(value);

            // 5. Serialize - Turn the list from C# objects back into JSON using SerializeObject
            json = JsonConvert.SerializeObject(topSpots);

            // 6. Write the JSON back to the file system, which replaces the file on the file system.
            // The second argument is the json file to write it to.
            File.WriteAllText(fileName, json);
        }
コード例 #4
0
        // DELETE: api/TopSpots/5
        public TopSpot Delete(int id)
        {
            //Read in json from file and store in array, creat new array one index smaller and store deleted topspot data
            TopSpot[] TopSpots      = JsonConvert.DeserializeObject <TopSpot[]>(File.ReadAllText(@"c:\dev\11-TopSpotsAPI\topspots.json"));
            TopSpot[] DeleteTopSpot = new TopSpot[TopSpots.Length - 1];
            TopSpot   DelTopSpot    = TopSpots[id];

            //Copying in TopSpot data from TopSpots array omitting the value and index id
            int i = 0;
            int j = 0;

            while (i < TopSpots.Length)
            {
                if (i != id)
                {
                    DeleteTopSpot[j] = TopSpots[i];
                    j++;
                }

                i++;
            }
            TopSpots = DeleteTopSpot;

            //Writing new TopSpot array to File and returning deleted term
            string TopSpotsString = JsonConvert.SerializeObject(TopSpots, Formatting.Indented);

            File.WriteAllText(@"c:\dev\11-TopSpotsAPI\topspots.json", TopSpotsString);
            return(DelTopSpot);
        }
コード例 #5
0
        // PUT: api/TopSpots/5
        public void Put(spotIndex index, [FromBody] TopSpot topSpot)
        {
            var list = JsonConvert.DeserializeObject <List <TopSpot> >(File.ReadAllText("C:/dev/TopSpotsAPI/topspots.json"));

            list[index.index].Name        = topSpot.Name;
            list[index.index].Description = topSpot.Description;
            list[index.index].Location[0] = topSpot.Location[0];
            list[index.index].Location[1] = topSpot.Location[1];

            var convertedJson = JsonConvert.SerializeObject(list, Formatting.Indented);

            Console.Write(convertedJson);
            File.WriteAllText("C:/dev/TopSpotsAPI/topspots.json", convertedJson);
        }
コード例 #6
0
        // POST: api/TopSpots
        public TopSpot Post(TopSpot TopSpot)
        {
            //Read in json from file and store in array, create new array one index larger and append parameter TopSpot to end
            TopSpot[] TopSpots    = JsonConvert.DeserializeObject <TopSpot[]>(File.ReadAllText(@"c:\dev\11-TopSpotsAPI\topspots.json"));
            TopSpot[] addTopSpots = new TopSpot[TopSpots.Length + 1];
            TopSpots.CopyTo(addTopSpots, 0);
            addTopSpots.SetValue(TopSpot, TopSpots.Length);
            TopSpots = addTopSpots;

            //Serialize TopSpots and write to file as json before returning new TopSpots with added term
            string TopSpotsString = JsonConvert.SerializeObject(TopSpots, Formatting.Indented);

            File.WriteAllText(@"c:\dev\11-TopSpotsAPI\topspots.json", TopSpotsString);
            return(TopSpot);
        }
コード例 #7
0
        // GET: api/TopSpots/5
        //public TopSpot Get(int id)
        //{
        //var userdata = new topspot { name = "test", description = "this is a test" };
        //topspotjson topspot = jsonconvert.deserializeobject<topspotjson>(file.readalltext(@"c:/dev/topspotsapi/topspots.json"));

        //topspot[] arr = new topspot[] { userdata };
        //var ts = topspot;
        //var array = arr;
        //string json_data = jsonconvert.serializeobject(arr);

        //list<> parts = new list<ts.topspots[id]>();

        //parts.add(userdata);

        //    //Console.WriteLine(parts);


        //    return Ts.topspots[id];
        //}

        // post: api/topspots
        public HttpResponseMessage Post([FromBody] TopSpot topSpot)
        {
            var list = JsonConvert.DeserializeObject <List <TopSpot> >(File.ReadAllText("C:/dev/TopSpotsAPI/topspots.json"));

            list.Add(topSpot);
            var convertedJson = JsonConvert.SerializeObject(list, Formatting.Indented);

            File.WriteAllText("C:/dev/TopSpotsAPI/topspots.json", convertedJson);

            Console.Write(list);

            return(Request.CreateResponse(HttpStatusCode.OK, new
            {
                topSpot = topSpot
            }));
        }
コード例 #8
0
        //create
        public void Post([FromBody] TopSpot value)
        {
            // get file name
            string fileName = HttpContext.Current.Server.MapPath("../topspots.json");

            //read json from file system
            string json = File.ReadAllText(fileName);

            //deserialize
            List <TopSpot> topSpots = JsonConvert.DeserializeObject <List <TopSpot> >(json);

            //add to the list
            topSpots.Add(value);

            //turn list back into json
            json = JsonConvert.SerializeObject(topSpots);

            //save it to .json file
            File.WriteAllText(fileName, json);
        }
コード例 #9
0
        // POST: api/TopSpots
        public void Post([FromBody] TopSpot value)
        {
            // 1. Get the filename
            string fileName = HttpContext.Current.Server.MapPath("../App_Data/topspots.json");

            // 2. Read the JSON from the file system
            string json = File.ReadAllText(fileName);

            // 3. Deserialize
            List <TopSpot> topSpots = JsonConvert.DeserializeObject <List <TopSpot> >(json);

            // 4. Add the top spot to the list
            topSpots.Add(value);

            // 5. Turn the list back into JSON
            json = JsonConvert.SerializeObject(topSpots);

            // 6. Write the JSON back to the file system.
            File.WriteAllText(fileName, json);
        }
コード例 #10
0
        // POST: api/TopSpots
        public HttpResponseMessage Post([FromBody] TopSpot topspot)
        {
            // reads the topspots.json into a local deserialized array
            var jsonArray = JsonConvert.DeserializeObject <List <TopSpot> >(File.ReadAllText("C:/dev/github/reuvenkishon/TopSpotsAPI/TopSpotsAPI/TopSpotsAPI/topspots.json"));

            // adds the object written within the body of the POST message, back into the local deserialized array
            jsonArray.Add(topspot);

            // serializes the jsonArray and stores it into another local variable
            var convertedJson = JsonConvert.SerializeObject(jsonArray, Formatting.Indented);

            File.WriteAllText(@"C:/dev/github/reuvenkishon/TopSpotsAPI/TopSpotsAPI/TopSpotsAPI/topspots.json", convertedJson);

            // Sends a Success Http Response message
            return(Request.CreateResponse(HttpStatusCode.OK,
                                          new
            {
                value = topspot,
                message = "success!"
            }));
        }
コード例 #11
0
        // PUT: api/TopSpots/5
        public HttpResponseMessage Put(int id, [FromBody] TopSpot topspot)
        {
            // reads the topspots.json into a local deserialized array
            var jsonArray = JsonConvert.DeserializeObject <List <TopSpot> >(File.ReadAllText("C:/dev/github/reuvenkishon/TopSpotsAPI/TopSpotsAPI/TopSpotsAPI/topspots.json"));

            //check to see if the name property within the body of the PUT message has contents, if yes then grab that value and store it within the appropriate element inside of the Json array
            if (topspot.name != null)
            {
                jsonArray[id].name = topspot.name;
            }

            //check to see if the description property within the body of the PUT message has contents, if yes then grab that value and store it within the appropriate element inside of the Json array

            if (topspot.description != null)
            {
                jsonArray[id].description = topspot.description;
            }

            //check to see if the location property within the body of the PUT message has contents, if yes then grab that value and store it within the appropriate element inside of the Json array

            if (topspot.location != null)
            {
                jsonArray[id].location[0] = topspot.location[0];
                jsonArray[id].location[1] = topspot.location[1];
            }

            // serializes the jsonArray and stores it into another local variable
            var convertedJson = JsonConvert.SerializeObject(jsonArray, Formatting.Indented);

            //writes the serialized JSON back into the external JSON file
            File.WriteAllText(@"C:/dev/github/reuvenkishon/TopSpotsAPI/TopSpotsAPI/TopSpotsAPI/topspots.json", convertedJson);

            // Sends an Success Http Response message
            return(Request.CreateResponse(HttpStatusCode.OK,
                                          new
            {
                value = topspot,
                message = "success!"
            }));
        }
コード例 #12
0
        // PUT: api/TopSpots
        // PUT (EDIT) will involve reading the JSON from the file system, finding the TopSpot to be updated, modifying those properties, then saving the array back to the file system.
        public void Put([FromBody] TopSpot updatedTopSpot)
        {
            // 1. Get file Name
            string fileName = HttpContext.Current.Server.MapPath("../App_Data/topspots.json");

            // 2. Read the JSON from the file System
            string json = File.ReadAllText(fileName);

            // 3. Deserialize - takes JSON and converts to C# objects.
            // Use List type Class that inherits from ICollections
            List <TopSpot> topSpots = JsonConvert.DeserializeObject <List <TopSpot> >(json);

            // 4. Find the top spot to be updated
            // foreach loop goes through every topspot location in the topSpots file and look for the top spot with matching laditude and longitude.
            foreach (var topSpot in topSpots)
            {
                // If there is a top spot location that matches the updated top spot location
                if (topSpot.Location[0] == updatedTopSpot.Location[0] &&
                    topSpot.Location[1] == updatedTopSpot.Location[1])
                {
                    // 5. Modify the properties - Take the topSpot value and overriding the values of the object.
                    // Update the name of the top spot
                    topSpot.Name = updatedTopSpot.Name;
                    // And update the top spot description
                    topSpot.Description = updatedTopSpot.Description;
                    // And update the top spot location because it was edited by user.
                    topSpot.Location = updatedTopSpot.Location;
                }
            }

            // 6. Turn the list back into JSON
            json = JsonConvert.SerializeObject(topSpots);

            // 7. Write the JSON back to the file system
            File.WriteAllText(fileName, json);

            //*************ONLY WAY TO TEST THIS IS TO UPDATE JSON FILE WITH ID'S ********************//
        }
コード例 #13
0
 // DELETE: api/TopSpots/5
 public void Delete(int id, TopSpot spot)
 {
 }
コード例 #14
0
 // PUT: api/TopSpots/5
 public void Put(int id, TopSpot spot)
 {
 }
コード例 #15
0
 // POST: api/TopSpots
 public TopSpot Post(TopSpot spot)
 {
 }
コード例 #16
0
 // POST: api/TopSpots
 public TopSpot Post(TopSpot topspot)
 {
     return(topspot);
 }