private void ImportAffiliateReferrals(long oldId, long newId) { wl(" - Migrating Referrals..."); Api oldProxy = GetOldStoreBV6Proxy(); ApiResponse <List <AffiliateReferralDTO> > refs = oldProxy.AffiliateReferralsFindForAffiliate(oldId); if (refs == null) { return; } if (refs.Content == null) { return; } foreach (AffiliateReferralDTO r in refs.Content) { AffiliateReferralDTO rnew = new AffiliateReferralDTO(); rnew.AffiliateId = newId; rnew.TimeOfReferralUtc = r.TimeOfReferralUtc; rnew.ReferrerUrl = r.ReferrerUrl; Api bv6proxy = GetBV6Proxy(); var res = bv6proxy.AffiliateReferralsCreate(rnew); } }
//DTO public AffiliateReferralDTO ToDto() { AffiliateReferralDTO dto = new AffiliateReferralDTO(); dto.Id = this.Id; dto.StoreId = this.StoreId; dto.TimeOfReferralUtc = this.TimeOfReferralUtc; dto.AffiliateId = this.AffiliateId; dto.ReferrerUrl = this.ReferrerUrl; return(dto); }
/// <summary> /// Allows you to convert the current AffiliateReferral object to the DTO equivalent for use with the REST API /// </summary> /// <returns>A new instance of AffiliateReferralDTO</returns> public AffiliateReferralDTO ToDto() { var dto = new AffiliateReferralDTO(); dto.Id = Id; dto.StoreId = StoreId; dto.TimeOfReferralUtc = TimeOfReferralUtc; dto.AffiliateId = AffiliateId; dto.ReferrerUrl = ReferrerUrl; return(dto); }
public void FromDto(AffiliateReferralDTO dto) { if (dto == null) { return; } this.Id = dto.Id; this.StoreId = dto.StoreId; this.TimeOfReferralUtc = dto.TimeOfReferralUtc; this.AffiliateId = dto.AffiliateId; this.ReferrerUrl = dto.ReferrerUrl; }
/// <summary> /// Allows the REST API to create or update an affiliate /// </summary> /// <param name="parameters"> /// Parameters passed in the URL of the REST API call. If there is a first parameter found in the /// URL, the method will assume it is the affiliate ID and that this is an update, otherwise it assumes to create an /// affiliate. /// </param> /// <param name="querystring">Name/value pairs from the REST API call querystring. This is not used in this method.</param> /// <param name="postdata">Serialized (JSON) version of the AffiliateDTO object</param> /// <returns>AffiliateDTO - Serialized (JSON) version of the affiliate</returns> public override string PostAction(string parameters, NameValueCollection querystring, string postdata) { var data = string.Empty; var ids = FirstParameter(parameters); long id = 0; long.TryParse(ids, out id); var isReferrals = GetParameterByIndex(1, parameters); if (isReferrals.Trim().ToLowerInvariant() == "referrals") { // Create Referral var responseA = new ApiResponse <AffiliateReferralDTO>(); AffiliateReferralDTO postedItemA = null; try { postedItemA = Json.ObjectFromJson <AffiliateReferralDTO>(postdata); } catch (Exception ex) { responseA.Errors.Add(new ApiError("EXCEPTION", ex.Message)); return(responseA.ObjectToJson()); } var itemA = new AffiliateReferral(); itemA.FromDto(postedItemA); HccApp.ContactServices.AffiliateReferrals.Create2(itemA, true); responseA.Content = itemA.ToDto(); data = responseA.ObjectToJson(); } else { // Create/Update Affiliate var responseB = new ApiResponse <AffiliateDTO>(); AffiliateDTO postedItem = null; try { postedItem = Json.ObjectFromJson <AffiliateDTO>(postdata); } catch (Exception ex) { responseB.Errors.Add(new ApiError("EXCEPTION", ex.Message)); return(responseB.ObjectToJson()); } var item = new Affiliate(); item.FromDto(postedItem); if (id < 1) { var existing = HccApp.ContactServices.Affiliates.FindByAffiliateId(item.AffiliateId); if (existing == null || existing.Id < 1) { // 20140408 - Updated to call the current repository method, instead of allowing the error to be thrown var result = CreateUserStatus.None; try { // set a temporary password item.Password = PasswordGenerator.GeneratePassword(8); // create the affiliate HccApp.ContactServices.Affiliates.Create(item, ref result); } catch (Exception createException) { // return the exception to the calling method responseB.Errors.Add(new ApiError { Code = "EXCEPTION", Description = createException.Message }); } if (result == CreateUserStatus.Success) { // send the affiliate responseB.Content = item.ToDto(); } else { // send a response why the affiliate wasn't created responseB.Errors.Add(new ApiError { Code = "USERNOTCREATED", Description = result.ToString() }); } id = item.Id; } else { try { responseB.Content = existing.ToDto(); id = existing.Id; } catch (Exception exUpdate) { responseB.Errors.Add(new ApiError { Code = "EXCEPTION", Description = exUpdate.Message }); } } } else { try { HccApp.ContactServices.Affiliates.Update(item); } catch (Exception ex) { responseB.Errors.Add(new ApiError { Code = "EXCEPTION", Description = ex.Message }); } id = item.Id; responseB.Content = item.ToDto(); } data = responseB.ObjectToJson(); } return(data); }
// Create or Update public override string PostAction(string parameters, System.Collections.Specialized.NameValueCollection querystring, string postdata) { string data = string.Empty; string ids = FirstParameter(parameters); long id = 0; long.TryParse(ids, out id); string isReferrals = GetParameterByIndex(1, parameters); if (isReferrals.Trim().ToLowerInvariant() == "referrals") { // Create Referral ApiResponse <AffiliateReferralDTO> responseA = new ApiResponse <AffiliateReferralDTO>(); AffiliateReferralDTO postedItemA = null; try { postedItemA = MerchantTribe.Web.Json.ObjectFromJson <AffiliateReferralDTO>(postdata); } catch (Exception ex) { responseA.Errors.Add(new ApiError("EXCEPTION", ex.Message)); return(MerchantTribe.Web.Json.ObjectToJson(responseA)); } AffiliateReferral itemA = new AffiliateReferral(); itemA.FromDto(postedItemA); MTApp.ContactServices.AffiliateReferrals.Create2(itemA, true); responseA.Content = itemA.ToDto(); data = MerchantTribe.Web.Json.ObjectToJson(responseA); } else { // Create/Update Affiliate ApiResponse <AffiliateDTO> responseB = new ApiResponse <AffiliateDTO>(); AffiliateDTO postedItem = null; try { postedItem = MerchantTribe.Web.Json.ObjectFromJson <AffiliateDTO>(postdata); } catch (Exception ex) { responseB.Errors.Add(new ApiError("EXCEPTION", ex.Message)); return(MerchantTribe.Web.Json.ObjectToJson(responseB)); } Affiliate item = new Affiliate(); item.FromDto(postedItem); if (id < 1) { Affiliate existing = MTApp.ContactServices.Affiliates.FindByReferralId(item.ReferralId); if (existing == null || existing.Id < 1) { // Create bool result = MTApp.ContactServices.Affiliates.Create(item); responseB.Content = item.ToDto(); id = item.Id; } else { responseB.Content = existing.ToDto(); id = existing.Id; } } else { MTApp.ContactServices.Affiliates.Update(item); id = item.Id; responseB.Content = item.ToDto(); } data = MerchantTribe.Web.Json.ObjectToJson(responseB); } return(data); }
public void Affiliates_TestCreateAndFindAndDelete() { //Create API proxy var proxy = CreateApiProxy(); //Create Affiliate var affiliate = new AffiliateDTO { StoreId = 1, DisplayName = "TestUserName" + DateTime.Now.ToString("ss"), Enabled = true, CommissionAmount = 50, ReferralId = "TestReferall" + DateTime.Now.ToString("ss") }; var createResponse = proxy.AffiliatesCreate(affiliate); CheckErrors(createResponse); Assert.IsFalse(createResponse.Content.Id == 0); //Find Affiliate var findResponse = proxy.AffiliatesFind(createResponse.Content.Id); CheckErrors(findResponse); Assert.AreEqual(createResponse.Content.DisplayName, findResponse.Content.DisplayName); Assert.AreEqual(createResponse.Content.CommissionAmount, findResponse.Content.CommissionAmount); Assert.AreEqual(createResponse.Content.Enabled, findResponse.Content.Enabled); Assert.AreEqual(createResponse.Content.ReferralId, findResponse.Content.ReferralId); //Create Affiliate Referral var affiliateReferral = new AffiliateReferralDTO { AffiliateId = createResponse.Content.Id, ReferrerUrl = string.Empty, StoreId = 1, TimeOfReferralUtc = DateTime.UtcNow }; var affiliateRefferalCreateResponse = proxy.AffiliateReferralsCreate(affiliateReferral); CheckErrors(affiliateRefferalCreateResponse); Assert.IsFalse(affiliateRefferalCreateResponse.Content.Id == 0); //Find Affiliate Referral var findAffiliateReferralResponse = proxy.AffiliateReferralsFindForAffiliate(createResponse.Content.Id); CheckErrors(findAffiliateReferralResponse); Assert.AreEqual(affiliateRefferalCreateResponse.Content.AffiliateId, findAffiliateReferralResponse.Content.First().AffiliateId); Assert.AreEqual(affiliateRefferalCreateResponse.Content.StoreId, findAffiliateReferralResponse.Content.First().StoreId); //Update Affiliate createResponse.Content.CommissionAmount = 55; var updateResponse = proxy.AffiliatesUpdate(createResponse.Content); CheckErrors(updateResponse); Assert.AreEqual(createResponse.Content.CommissionAmount, updateResponse.Content.CommissionAmount); //Delete Affiliate var deleteResponse = proxy.AffiliatesDelete(createResponse.Content.Id); CheckErrors(deleteResponse); Assert.IsTrue(deleteResponse.Content); }