Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        // 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);
        }