Skip to content

Commit

Permalink
Finishes #33
Browse files Browse the repository at this point in the history
Released in 1.1.6085.27777
  • Loading branch information
kfrancis committed Aug 29, 2016
1 parent ed54b4c commit 482183b
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 49 deletions.
154 changes: 138 additions & 16 deletions Source/Chargify.NET/ChargifyConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5262,20 +5262,21 @@ public INote CreateNote(INote note)
return CreateNote(note.SubscriptionID, note.Body, note.Sticky);
}

private INote CreateNote(int subscriptionId, string body, bool sticky = false)
public INote CreateNote(int subscriptionId, string body, bool sticky = false)
{
// make sure data is valid
if (subscriptionId > 0) throw new ArgumentNullException(nameof(subscriptionId));
// make sure that the system ID is unique
if (LoadSubscription(subscriptionId) != null) throw new ArgumentException("Not valid", "subscriptionId");
// create XML for creation of customer
if (subscriptionId < 0) throw new ArgumentNullException(nameof(subscriptionId));

// create XML for creation of note
var noteXml = new StringBuilder(GetXmlStringIfApplicable());
noteXml.Append("<note>");
noteXml.AppendFormat("<body>{0}</body>", body);
noteXml.AppendFormat("<sticky>{0}</sticky>", sticky);
noteXml.Append("</note>");

// now make the request
string response = DoRequest(string.Format("subscriptions/{0}/notes.{1}", subscriptionId, GetMethodExtension()), HttpRequestMethod.Post, noteXml.ToString());

// change the response to the object
return response.ConvertResponseTo<Note>("note");
}
Expand All @@ -5287,8 +5288,67 @@ private INote CreateNote(int subscriptionId, string body, bool sticky = false)
/// <returns></returns>
public IDictionary<int, INote> GetNotesForSubscription(int subscriptionId)
{
// TODO
throw new NotImplementedException();
// make sure data is valid
if (subscriptionId == int.MinValue) throw new ArgumentNullException(nameof(subscriptionId));

// now make the request
string response = DoRequest(string.Format("subscriptions/{0}/notes.{1}", subscriptionId, GetMethodExtension()));
var retValue = new Dictionary<int, INote>();
if (response.IsXml())
{
// now build a product list based on response XML
XmlDocument doc = new XmlDocument();
doc.LoadXml(response); // get the XML into an XML document
if (doc.ChildNodes.Count == 0) throw new InvalidOperationException("Returned XML not valid");
// loop through the child nodes of this node

foreach (XmlNode elementNode in doc.ChildNodes)
{
if (elementNode.Name == "notes")
{
foreach (XmlNode noteNode in elementNode.ChildNodes)
{
if (noteNode.Name == "note")
{
INote loadedNote = new Note(noteNode);
if (!retValue.ContainsKey(loadedNote.ID))
{
retValue.Add(loadedNote.ID, loadedNote);
}
else
{
throw new InvalidOperationException("Duplicate Note ID values detected");
}
}
}
}
}
}
else if (response.IsJSON())
{
// should be expecting an array
int position = 0;
JsonArray array = JsonArray.Parse(response, ref position);
for (int i = 0; i <= array.Length - 1; i++)
{
var jsonObject = array.Items[i] as JsonObject;
if (jsonObject != null && jsonObject.ContainsKey("note"))
{
JsonObject componentObj = (array.Items[i] as JsonObject)["note"] as JsonObject;
INote loadedComponent = new Note(componentObj);
if (!retValue.ContainsKey(loadedComponent.ID))
{
retValue.Add(loadedComponent.ID, loadedComponent);
}
else
{
throw new InvalidOperationException("Duplicate Note ID values detected");
}
}
}
}
// return the dictionary
return retValue;
}

/// <summary>
Expand All @@ -5299,8 +5359,20 @@ public IDictionary<int, INote> GetNotesForSubscription(int subscriptionId)
/// <returns></returns>
public INote LoadNote(int subscriptionId, int noteId)
{
// TODO
throw new NotImplementedException();
try
{
// make sure data is valid
if (subscriptionId == int.MinValue) throw new ArgumentNullException(nameof(subscriptionId));
// now make the request
string response = DoRequest(string.Format("subscriptions/{0}/notes/{1}.{2}", subscriptionId, noteId, GetMethodExtension()));
// change the response to the object
return response.ConvertResponseTo<Note>("note");
}
catch (ChargifyException cex)
{
if (cex.StatusCode == HttpStatusCode.NotFound) return null;
throw;
}
}

/// <summary>
Expand All @@ -5311,20 +5383,70 @@ public INote LoadNote(int subscriptionId, int noteId)
/// <returns></returns>
public bool DeleteNote(int subscriptionId, int noteId)
{
// TODO
throw new NotImplementedException();
try
{
// make sure data is valid
if (subscriptionId < 0) throw new ArgumentNullException(nameof(subscriptionId));

// now make the request
DoRequest(string.Format("subscriptions/{0}/notes/{1}.{2}", subscriptionId, noteId, GetMethodExtension()), HttpRequestMethod.Delete, string.Empty);
return true;
}
catch (ChargifyException cex)
{
switch (cex.StatusCode)
{
case HttpStatusCode.Forbidden:
case HttpStatusCode.NotFound:
return false;
default:
throw;
}
}
}

/// <summary>
/// Updates a note
/// </summary>
/// <param name="subscriptionId">The id of the subscription</param>
/// <param name="updatedNote">The updated note</param>
/// <returns></returns>
public INote UpdateNote(int subscriptionId, INote updatedNote)
/// <param name="note">The updated note</param>
/// <returns>Returns the updated note, or the same note if unsuccessful</returns>
public INote UpdateNote(int subscriptionId, INote note)
{
// TODO
throw new NotImplementedException();
// make sure data is OK
if (note == null) throw new ArgumentNullException(nameof(note));
if (note.ID == int.MinValue) throw new ArgumentException("Invalid chargify ID detected", nameof(note));
INote oldNote = LoadNote(note.SubscriptionID, note.ID);

bool isUpdateRequired = false;

// create XML for updating of note
var customerXml = new StringBuilder(GetXmlStringIfApplicable());
customerXml.Append("<note>");
if (oldNote != null)
{
if (oldNote.SubscriptionID != note.SubscriptionID) throw new ArgumentException("Subscription IDs do not match", nameof(note));
if (oldNote.Body != note.Body) { customerXml.AppendFormat("<body>{0}</body>", HttpUtility.HtmlEncode(note.Body)); isUpdateRequired = true; }
if (oldNote.Sticky != note.Sticky) { customerXml.AppendFormat("<sticky>{0}</sticky>", note.Sticky ? 1 : 0); isUpdateRequired = true; }
}
customerXml.Append("</note>");

if (isUpdateRequired)
{
try
{
// now make the request
string response = DoRequest(string.Format("subscriptions/{0}/notes/{1}.{2}", subscriptionId, note.ID, GetMethodExtension()), HttpRequestMethod.Put, customerXml.ToString());
// change the response to the object
return response.ConvertResponseTo<Note>("note");
}
catch (ChargifyException cex)
{
if (cex.StatusCode == HttpStatusCode.NotFound) throw new InvalidOperationException("Customer not found");
throw;
}
}
return note;
}
#endregion

Expand Down
8 changes: 8 additions & 0 deletions Source/Chargify.NET/Interfaces/IChargifyConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ public interface IChargifyConnect
/// <returns>The note (with id) if successful, null otherwise.</returns>
INote CreateNote(INote NewNote);
/// <summary>
/// Create note
/// </summary>
/// <param name="subscriptionId">The id of the subscription</param>
/// <param name="body">The note body</param>
/// <param name="sticky">Is the note sticky?</param>
/// <returns>The note, if successful. Null otherwise</returns>
INote CreateNote(int subscriptionId, string body, bool sticky = false);
/// <summary>
/// Gets the notes for a particular subscription
/// </summary>
/// <param name="SubscriptionID">The id of the subscription</param>
Expand Down
6 changes: 3 additions & 3 deletions Source/Chargify.NET/Interfaces/INote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public interface INote : IComparable<INote>
/// <summary>
/// The main text context of the note
/// </summary>
string Body { get; }
string Body { get; set; }

/// <summary>
/// The ID of the related subscription
/// </summary>
int SubscriptionID { get; }
int SubscriptionID { get; set; }

/// <summary>
/// The date and time the note was created
Expand All @@ -68,6 +68,6 @@ public interface INote : IComparable<INote>
/// <summary>
/// Whether or not it is pinned to the top of the list of notes
/// </summary>
bool Sticky { get; }
bool Sticky { get; set; }
}
}
39 changes: 9 additions & 30 deletions Source/Chargify.NET/Note.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ private void LoadFromJson(JsonObject obj)
_id = obj.GetJSONContentAsInt(key);
break;
case BodyKey:
_body = obj.GetJSONContentAsString(key);
Body = obj.GetJSONContentAsString(key);
break;
case SubscriptionIdKey:
_subscriptionId = obj.GetJSONContentAsInt(key);
SubscriptionID = obj.GetJSONContentAsInt(key);
break;
case CreatedAtKey:
_createdAt = obj.GetJSONContentAsDateTime(key);
Expand All @@ -125,7 +125,7 @@ private void LoadFromJson(JsonObject obj)
_updatedAt = obj.GetJSONContentAsDateTime(key);
break;
case StickyKey:
_sticky = obj.GetJSONContentAsBoolean(key);
Sticky = obj.GetJSONContentAsBoolean(key);
break;
}
}
Expand All @@ -145,10 +145,10 @@ private void LoadFromNode(XmlNode noteNode)
_id = dataNode.GetNodeContentAsInt();
break;
case BodyKey:
_body = dataNode.GetNodeContentAsString();
Body = dataNode.GetNodeContentAsString();
break;
case SubscriptionIdKey:
_subscriptionId = dataNode.GetNodeContentAsInt();
SubscriptionID = dataNode.GetNodeContentAsInt();
break;
case CreatedAtKey:
_createdAt = dataNode.GetNodeContentAsDateTime();
Expand All @@ -157,7 +157,7 @@ private void LoadFromNode(XmlNode noteNode)
_updatedAt = dataNode.GetNodeContentAsDateTime();
break;
case StickyKey:
_sticky = dataNode.GetNodeContentAsBoolean();
Sticky = dataNode.GetNodeContentAsBoolean();
break;
}
}
Expand All @@ -168,14 +168,7 @@ private void LoadFromNode(XmlNode noteNode)
/// <summary>
/// The main text content of the note
/// </summary>
public string Body
{
get
{
return _body;
}
}
private string _body;
public string Body { get; set; }

/// <summary>
/// Date and time the note was created
Expand Down Expand Up @@ -204,26 +197,12 @@ public int ID
/// <summary>
/// Whether or not it is pinned to the top of the list of notes
/// </summary>
public bool Sticky
{
get
{
return _sticky;
}
}
private bool _sticky;
public bool Sticky { get; set; }

/// <summary>
/// The id of the related subscription
/// </summary>
public int SubscriptionID
{
get
{
return _subscriptionId;
}
}
private int _subscriptionId = int.MinValue;
public int SubscriptionID { get; set; }

/// <summary>
/// Last update timestamp
Expand Down
1 change: 1 addition & 0 deletions Source/ChargifyDotNetTests/ChargifyDotNetTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<Compile Include="PaymentProfileTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ProductTests.cs" />
<Compile Include="NoteTests.cs" />
<Compile Include="SubscriptionTests.cs" />
<Compile Include="CouponTests.cs" />
<Compile Include="ComponentTests.cs" />
Expand Down
Loading

0 comments on commit 482183b

Please sign in to comment.