public void FromDto(AffiliateDTO dto) { if (dto == null) { return; } this.Id = dto.Id; this.StoreId = dto.StoreId; this.Enabled = dto.Enabled; this.ReferralId = dto.ReferralId; this.DisplayName = dto.DisplayName; this.Address.FromDto(dto.Address); this.CommissionAmount = dto.CommissionAmount; this.CommissionType = (AffiliateCommissionType)((int)dto.CommissionType); this.ReferralDays = dto.ReferralDays; this.TaxId = dto.TaxId; this.DriversLicenseNumber = dto.DriversLicenseNumber; this.WebSiteUrl = dto.WebSiteUrl; this.CustomThemeName = dto.CustomThemeName; this.Notes = dto.Notes; this.LastUpdatedUtc = dto.LastUpdatedUtc; this.Contacts.Clear(); foreach (AffiliateContactDTO contact in dto.Contacts) { AffiliateContact c = new AffiliateContact(); c.FromDto(contact); this.Contacts.Add(c); } }
/// <summary> /// Allows you to populate the current affiliate object using an AffiliateDTO instance /// </summary> /// <param name="dto">An instance of the affiliate from the REST API</param> public void FromDto(AffiliateDTO dto) { if (dto == null) { return; } Id = dto.Id; UserId = (int)dto.Id; // missing after the Affiliate updates StoreId = dto.StoreId; Enabled = dto.Enabled; AffiliateId = dto.ReferralId; Username = dto.DisplayName; Address.FromDto(dto.Address); CommissionAmount = dto.CommissionAmount; CommissionType = (AffiliateCommissionType)(int)dto.CommissionType; ReferralDays = dto.ReferralDays; TaxId = dto.TaxId; DriversLicenseNumber = dto.DriversLicenseNumber; WebSiteUrl = dto.WebSiteUrl; Notes = dto.Notes; LastUpdatedUtc = dto.LastUpdatedUtc; #pragma warning disable 0612, 0618 Contacts.Clear(); foreach (var contact in dto.Contacts) { var c = new AffiliateContact(); c.FromDto(contact); Contacts.Add(c); } #pragma warning restore 0612, 0618 }
//public static Collection<Affiliate> FindByUserId(string bvin) //{ // return Mapper.FindByUserId(bvin); //} //public static Collection<Affiliate> FindEnabledByUserId(string bvin) //{ // Collection<Affiliate> enabledAffiliates = new Collection<Affiliate>(); // foreach (Contacts.Affiliate item in Mapper.FindByUserId(bvin)) { // if (item.Enabled) { // enabledAffiliates.Add(item); // } // } // return enabledAffiliates; //} //DTO public AffiliateDTO ToDto() { AffiliateDTO dto = new AffiliateDTO(); dto.Id = this.Id; dto.StoreId = this.StoreId; dto.Enabled = this.Enabled; dto.ReferralId = this.ReferralId; dto.DisplayName = this.DisplayName; dto.Address = this.Address.ToDto(); dto.CommissionAmount = this.CommissionAmount; dto.CommissionType = (AffiliateCommissionTypeDTO)((int)this.CommissionType); dto.ReferralDays = this.ReferralDays; dto.TaxId = this.TaxId; dto.DriversLicenseNumber = this.DriversLicenseNumber; dto.WebSiteUrl = this.WebSiteUrl; dto.CustomThemeName = this.CustomThemeName; dto.Notes = this.Notes; dto.LastUpdatedUtc = this.LastUpdatedUtc; foreach (AffiliateContact contact in this.Contacts) { dto.Contacts.Add(contact.ToDto()); } return(dto); }
/// <summary> /// Allows you to convert the current affiliate object to the DTO equivalent for use with the REST API /// </summary> /// <returns>A new instance of AffiliateDTO</returns> public AffiliateDTO ToDto() { var dto = new AffiliateDTO(); dto.Id = Id; dto.StoreId = StoreId; dto.Enabled = Enabled; dto.ReferralId = AffiliateId; dto.DisplayName = Username; dto.Address = Address.ToDto(); dto.CommissionAmount = CommissionAmount; dto.CommissionType = (AffiliateCommissionTypeDTO)(int)CommissionType; dto.ReferralDays = ReferralDays; dto.TaxId = TaxId; dto.DriversLicenseNumber = DriversLicenseNumber; dto.WebSiteUrl = WebSiteUrl; dto.Notes = Notes; dto.LastUpdatedUtc = LastUpdatedUtc; #pragma warning disable 0612, 0618 foreach (var contact in Contacts) { dto.Contacts.Add(contact.ToDto()); } #pragma warning restore 0612, 0618 return(dto); }
/// <summary> /// Allows you to convert the current affiliate object to the DTO equivalent for use with the REST API /// </summary> /// <returns>A new instance of AffiliateDTO</returns> public AffiliateDTO ToDto() { var dto = new AffiliateDTO(); dto.Id = Id; dto.StoreId = StoreId; dto.Enabled = Enabled; dto.ReferralId = AffiliateId; dto.DisplayName = Username; dto.Address = Address.ToDto(); dto.CommissionAmount = CommissionAmount; dto.CommissionType = (AffiliateCommissionTypeDTO)(int)CommissionType; dto.ReferralDays = ReferralDays; dto.TaxId = TaxId; dto.DriversLicenseNumber = DriversLicenseNumber; dto.WebSiteUrl = WebSiteUrl; dto.Notes = Notes; dto.LastUpdatedUtc = LastUpdatedUtc; return(dto); }
/// <summary> /// Allows you to populate the current affiliate object using an AffiliateDTO instance /// </summary> /// <param name="dto">An instance of the affiliate from the REST API</param> public void FromDto(AffiliateDTO dto) { if (dto == null) { return; } Id = dto.Id; UserId = (int)dto.Id; // missing after the Affiliate updates StoreId = dto.StoreId; Enabled = dto.Enabled; AffiliateId = dto.ReferralId; Username = dto.DisplayName; Address.FromDto(dto.Address); CommissionAmount = dto.CommissionAmount; CommissionType = (AffiliateCommissionType)(int)dto.CommissionType; ReferralDays = dto.ReferralDays; TaxId = dto.TaxId; DriversLicenseNumber = dto.DriversLicenseNumber; WebSiteUrl = dto.WebSiteUrl; Notes = dto.Notes; LastUpdatedUtc = dto.LastUpdatedUtc; }
/// <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); }