Example #1
0
        public IHttpActionResult SaveCampaignRecorderListings([FromBody] List <ListingCampaignUrl> listingCampaignUrls)
        {
            try
            {
                bool saveSuccess  = true;
                bool addNewRecord = true;

                foreach (ListingCampaignUrl listing in listingCampaignUrls)
                {
                    DataWriter dw = new DataWriter();

                    SocialMediaAccount sma = new SocialMediaAccount()
                    {
                        Password          = listing.Password,
                        SocialMediaSiteId = listing.SocialMediaSiteId,
                        AccountId         = listing.AccountId,
                        Url      = listing.Url,
                        Username = listing.UserName,
                        Id       = listing.SocialMediaAccountId
                    };

                    try
                    {
                        // Update first. If our SocialMediaAccountId is a number greater than zero, that means we have an existing record which we need to update.
                        if (listing.SocialMediaAccountId > 0)
                        {
                            addNewRecord = false;
                            List <SocialMediaAccount> singleMediaAccount = new List <SocialMediaAccount>();
                            singleMediaAccount.Add(sma);
                            dw.SaveListingCampaignUrl(listing);
                            dw.UpdateSocialMediaAccounts(singleMediaAccount);
                        }
                    }
                    catch (Exception)
                    {
                        saveSuccess = false;
                    }

                    // If we did not 'return' prior to here, it means we default this to an 'add'.
                    // First we insert a record into the SocialMediaAccount table and get its id which is needed for the ListingCampaignUrl table.
                    if (addNewRecord)
                    {
                        listing.SocialMediaAccountId = dw.AddSocialMediaAccount(sma);

                        if (listing.SocialMediaAccountId > 0)
                        {
                            int newId = dw.SaveListingCampaignUrl(listing);
                            listing.ListingCampaignUrlId = newId;
                            if (newId < 1)
                            {
                                saveSuccess = false;
                            }
                        }
                    }
                }

                if (saveSuccess)
                {
                    return(Ok(listingCampaignUrls));
                }

                return(StatusCode(HttpStatusCode.BadRequest));
            }
            catch (Exception e)
            {
                if (e.InnerException != null)
                {
                    return(InternalServerError(e.InnerException));
                }
                return(InternalServerError(e));
            }
        }