public virtual bool Delete() { BillomatResourceAttribute resource = GetResource(); if (resource.Flags.HasFlag(BillomatResourceFlags.NoDelete)) { throw new BillomatException(string.Format("Deleting an object is not allowed for {0}!", GetType().Name), new NotSupportedException()); } var req = new BillomatRequest { Verb = "DELETE", Resource = resource.ResourceName, Id = Id }; try { req.GetXmlResponse(); return(true); } catch (BillomatException) { return(false); } }
public virtual T Create() { BillomatResourceAttribute resource = GetResource(); if (resource.Flags.HasFlag(BillomatResourceFlags.NoCreate)) { throw new BillomatException(string.Format("Creating new objects is not allowed for {0}!", GetType().Name), new NotSupportedException()); } var xml = CreateXml(resource.XmlSingleName, this); var req = new BillomatRequest { Verb = "POST", Resource = resource.ResourceName, Body = xml.ToString(SaveOptions.DisableFormatting) }; T result = CreateFromXml(req.GetXmlResponse()); // save the returned values (especially id) ApplyFrom(result); return(result); }
/// <summary> /// Finds all Billomat objects with the specified parameters. Sends multiple requests if /// necessary (due to page size). /// </summary> /// <param name="parameters"></param> /// <returns></returns> protected static List <T> FindAll(NameValueCollection parameters = null) { BillomatResourceAttribute resource = GetResource(); var req = new BillomatRequest { Verb = "GET", Resource = resource.ResourceName }; if (parameters != null) { req.Params.Add(parameters); } req.Params["per_page"] = Billomat.PageSize.ToString(CultureInfo.InvariantCulture); int page = 0; int total = 0; T[] result = null; do { page++; req.Params["page"] = page.ToString(CultureInfo.InvariantCulture); XElement xml = req.GetXmlResponse(); if (result == null) { total = (int)xml.Attribute("total"); result = new T[total]; } int i = 0; foreach (var obj in xml.XPathSelectElements(string.Format("/{0}/{1}", resource.XmlMultiName, resource.XmlSingleName))) { result[(page - 1) * Billomat.PageSize + i] = CreateFromXml(obj); i++; } } while (page * Billomat.PageSize < total); return(result.ToList()); }
public virtual void Update() { BillomatResourceAttribute resource = GetResource(); if (resource.Flags.HasFlag(BillomatResourceFlags.NoUpdate)) { throw new BillomatException(string.Format("Updating an object is not allowed for {0}!", GetType().Name), new NotSupportedException()); } var xml = CreateXml(resource.XmlSingleName, this); var req = new BillomatRequest { Verb = "PUT", Resource = resource.ResourceName, Id = Id, Body = xml.ToString(SaveOptions.DisableFormatting) }; req.GetXmlResponse(); }