Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

files associated with objects

  • Loading branch information...
commit 0489c7cf790322c0ad61d21340167bc60ba931a1 1 parent 152b358
Karl Seguin authored
8 Parse.Tests/ObjectsTests/SaveTests.cs
@@ -52,5 +52,13 @@ public void SendsASaveRequestWithAGeoPoint()
52 52 new Driver().Objects.Save(new GeoPointClass {Location = new GeoPoint(23.3, -39.4)}, SetIfSuccess);
53 53 WaitOne();
54 54 }
  55 +
  56 + [Test]
  57 + public void SendsASaveRequestWithAFile()
  58 + {
  59 + Server.Stub(new ApiExpectation { Method = "POST", Url = "/1/classes/FileClass", Request = "{\"File\":{\"__type\":\"File\",\"name\":\"test.gif\"}}", Response = "{}" });
  60 + new Driver().Objects.Save(new FileClass { File = new ParseFile("test.gif") }, SetIfSuccess);
  61 + WaitOne();
  62 + }
55 63 }
56 64 }
5 Parse.Tests/ParseObjectClass.cs
@@ -36,6 +36,11 @@ public class GeoPointClass
36 36 public GeoPoint Location { get; set; }
37 37 }
38 38
  39 + public class FileClass
  40 + {
  41 + public ParseFile File { get; set; }
  42 + }
  43 +
39 44 public class SimpleClass
40 45 {
41 46 public int PowerLevel { get; set; }
29 Parse/Components/Json/ParseFileConverter.cs
... ... @@ -0,0 +1,29 @@
  1 +using System;
  2 +using System.Collections.Generic;
  3 +using Newtonsoft.Json;
  4 +
  5 +namespace Parse
  6 +{
  7 + public class ParseFileConverter : JsonConverter
  8 + {
  9 + public override bool CanConvert(Type type)
  10 + {
  11 + return (typeof(ParseFile).IsAssignableFrom(type));
  12 + }
  13 +
  14 + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  15 + {
  16 + var file = (ParseFile)value;
  17 + serializer.Serialize(writer, new Dictionary<string, object>
  18 + {
  19 + {"__type", "File"},
  20 + {"name", file.Name},
  21 + });
  22 + }
  23 +
  24 + public override object ReadJson(JsonReader reader, Type type, object existingValue, JsonSerializer serializer)
  25 + {
  26 + return serializer.Deserialize(reader, type);
  27 + }
  28 + }
  29 +}
2  Parse/Driver.cs
@@ -11,7 +11,7 @@ public interface IDriver
11 11
12 12 public class Driver : IDriver
13 13 {
14   - internal static readonly JsonConverter[] SerializationConverters = new[] { (JsonConverter)new DateConverter(), new ByteConverterr(), new GeoPointConverter() };
  14 + internal static readonly JsonConverter[] SerializationConverters = new[] { (JsonConverter)new DateConverter(), new ByteConverterr(), new GeoPointConverter(), new ParseFileConverter() };
15 15
16 16 private readonly IObjects _objects;
17 17 private readonly IUsers _users;
12 Parse/Models/ParseFile.cs
... ... @@ -0,0 +1,12 @@
  1 +namespace Parse
  2 +{
  3 + public class ParseFile
  4 + {
  5 + public string Name { get; private set; }
  6 +
  7 + public ParseFile(string name)
  8 + {
  9 + Name = name;
  10 + }
  11 + }
  12 +}
2  Parse/Parse.csproj
@@ -54,6 +54,7 @@
54 54 <Compile Include="Components\Communicator.cs" />
55 55 <Compile Include="Components\Json\DateConverter.cs" />
56 56 <Compile Include="Components\Json\GeoPointConverter.cs" />
  57 + <Compile Include="Components\Json\ParseFileConverter.cs" />
57 58 <Compile Include="Components\Json\ParseUserContract.cs" />
58 59 <Compile Include="Components\ParseConfiguration.cs" />
59 60 <Compile Include="Components\Response.cs" />
@@ -61,6 +62,7 @@
61 62 <Compile Include="Models\FileResponse.cs" />
62 63 <Compile Include="Models\GeoPoint.cs" />
63 64 <Compile Include="Models\IParseUser.cs" />
  65 + <Compile Include="Models\ParseFile.cs" />
64 66 <Compile Include="Models\ResultsResponse.cs" />
65 67 <Compile Include="Objects.cs" />
66 68 <Compile Include="Driver.cs" />
2  Parse/ParseFull.csproj
@@ -43,6 +43,7 @@
43 43 <Compile Include="Components\Json\ByteConverter.cs" />
44 44 <Compile Include="Components\Json\DateConverter.cs" />
45 45 <Compile Include="Components\Json\GeoPointConverter.cs" />
  46 + <Compile Include="Components\Json\ParseFileConverter.cs" />
46 47 <Compile Include="Components\Json\ParseUserContract.cs" />
47 48 <Compile Include="Components\ParseConfiguration.cs" />
48 49 <Compile Include="Components\Response.cs" />
@@ -51,6 +52,7 @@
51 52 <Compile Include="Models\FileResponse.cs" />
52 53 <Compile Include="Models\GeoPoint.cs" />
53 54 <Compile Include="Models\IParseUser.cs" />
  55 + <Compile Include="Models\ParseFile.cs" />
54 56 <Compile Include="Models\ParseObject.cs" />
55 57 <Compile Include="Models\ResultsResponse.cs" />
56 58 <Compile Include="Objects.cs" />
8 readme.md
Source Rendered
@@ -254,4 +254,10 @@ Do note that Parse will give each uploaded a file its own unique name. So, if we
254 254 ### Deleting A File
255 255 To delete a file you must supply the Parse name of the file (when you saved a file, Parse returned a unique name based on the name you supplied). Also, the Parse driver must have been configured with a MasterKey.
256 256
257   - parse.Files.Delete("12323-sand.png", null);
  257 + parse.Files.Delete("12323-sand.png", null);
  258 +
  259 +### Associating Files with Objects
  260 +Once you've created a file, you can associate it with an object by wrapping the file name in a `ParseFile` object. Note that this must be the parse-supplied file name:
  261 +
  262 + var user = new User{Name = "Goku", PowerLevel = 9001, Profile = new ParseFile("1233233-goku.png")};
  263 + parse.Objects.Save(user);

0 comments on commit 0489c7c

Please sign in to comment.
Something went wrong with that request. Please try again.