Exemplo n.º 1
0
        public void CreateCampaign()
        {
            var campaignCommand = new CampaignCreated()
            {
                RelatedDataEntityName = "CreateTestEntityName",
                RelatedDataEntityID   = 1,
                Name             = "CreateTestName",
                ShortDescription = "CreateTestSDescription",
                LongDescription  = "CreateTestLDescription",
                DiscountType     = DiscountType.Percentage,
                DiscountAmount   = 30,
                Status           = Framework.Models.EntityStatus.Active
            };

            var response = campaingService.CreateCampaign(campaignCommand);

            Assert.AreEqual(response.Type, Common.ServiceResponseTypes.Success);
        }
Exemplo n.º 2
0
        public CampaignServiceResponse <Campaign> CreateCampaign(CampaignCreated ev, List <ServiceLogRecord> logRecords = null)
        {
            // Create the watch
            var sw = new Stopwatch();

            sw.Start();

            // Create a log record collection if necessary
            if (logRecords == null)
            {
                logRecords = new List <ServiceLogRecord>();
            }

            // Add log
            logRecords.Add(new ServiceLogRecord()
            {
                Type      = "DEBUG",
                TimeStamp = DateTime.Now,
                Body      = "Create Campaign request received."
            });

            var response = new CampaignServiceResponse <Campaign>();

            #region [ Validate Request ]

            logRecords.Add(new ServiceLogRecord()
            {
                Type      = "DEBUG",
                TimeStamp = DateTime.Now,
                Body      = "User has the required permissions. Now validating the incoming event data."
            });

            // Check required data
            List <string> dataErrors = new List <string>();

            //if (ev.ResellerId == default(int) || String.IsNullOrEmpty(ev.ResellerName))
            //{
            //    dataErrors.Add("No reseller!");
            //}

            //if (ev.Items == null || ev.Items.Count == 0)
            //{
            //    dataErrors.Add("No Campaign item(s)!");
            //}

            //if (String.IsNullOrEmpty(ev.Name))
            //{
            //    dataErrors.Add("No product name!");
            //}
            //if (ev.Items.Count == 0)
            //{
            //    dataErrors.Add("No Campaign item");
            //}

            if (dataErrors.Count > 0)
            {
                // Add log
                logRecords.Add(new ServiceLogRecord()
                {
                    Type      = "DEBUG",
                    TimeStamp = DateTime.Now,
                    Body      = dataErrors.Count + " error(s) found within the event data! Terminating the process. Errors:" + String.Join(";", dataErrors)
                });

                // Stop the sw
                sw.Stop();

                response.Type = ServiceResponseTypes.Error;
                response.Code = ((short)HeadstoneServiceResponseCodes.Invalid_Request).ToString();
                response.PreProcessingTook = sw.ElapsedMilliseconds;
                response.Message           = "There are some errors with the incoming event data!";
                response.Errors.AddRange(dataErrors);
                response.LogRecords = logRecords;

                return(response);
            }

            #endregion

            #region [ Data manuplation ]

            // Stop the timer
            sw.Stop();

            // Set the pre-processing time and start the time
            response.PreProcessingTook = sw.ElapsedMilliseconds;
            sw.Start();
            #endregion

            #region [ Create Object ]
            logRecords.Add(new ServiceLogRecord()
            {
                Type      = "DEBUG",
                TimeStamp = DateTime.Now,
                Body      = "Creating Campaign."
            });

            // Create the reseller application
            var command = new Campaign()
            {
                RelatedDataEntityName = ev.RelatedDataEntityName,
                RelatedDataEntityID   = ev.RelatedDataEntityID,
                Name               = ev.Name,
                ShortDescription   = ev.ShortDescription,
                LongDescription    = ev.LongDescription,
                DiscountType       = ev.DiscountType,
                DiscountAmount     = ev.DiscountAmount,
                Status             = ev.Status,
                Created            = DateTime.Now,
                CampaignProperties = ev.CampaignProperties
            };

            // Add log
            logRecords.Add(new ServiceLogRecord()
            {
                Type      = "DEBUG",
                TimeStamp = DateTime.Now,
                Body      = string.Format("Campaign created", ev.UserToken, ev.SessionId)
            });
            #endregion

            #region [ Save Campaign ]

            var baseServiceResponse = _CampaignServiceBase.Create(command);
            if (baseServiceResponse.Type != Headstone.Framework.Models.ServiceResponseTypes.Success)
            {
                // Add log
                logRecords.Add(new ServiceLogRecord()
                {
                    Type      = "ERROR",
                    TimeStamp = DateTime.Now,
                    Body      = "There was an error while saving Campaign!"
                });

                // Stop the sw
                sw.Stop();

                response.Type        = ServiceResponseTypes.Error;
                response.Code        = ((short)HeadstoneServiceResponseCodes.General_Exception).ToString();
                response.ServiceTook = sw.ElapsedMilliseconds;
                response.Message     = "There was an error while saving the request!";
                response.Errors.AddRange(baseServiceResponse.Errors);
                response.LogRecords = logRecords;

                return(response);
            }
            else
            {
                // Add log
                logRecords.Add(new ServiceLogRecord()
                {
                    Type      = "DEBUG",
                    TimeStamp = DateTime.Now,
                    Body      = string.Format("Campaign successfuly created. CampaignID:{0}",
                                              command.CampaignID)
                });

                // Add the new object to the result
                response.Result.Add(command);

                // Set the object id
                response.CampaignID = command.CampaignID;
            }
            // Stop the sw
            sw.Stop();

            response.Type        = ServiceResponseTypes.Success;
            response.Code        = ((short)HeadstoneServiceResponseCodes.Request_Successfuly_Completed).ToString();
            response.ServiceTook = sw.ElapsedMilliseconds;
            response.Message     = string.Format("Campaign successfuly created. CampaignID:{0}",
                                                 command.CampaignID);
            response.LogRecords = logRecords;

            #endregion

            return(response);
        }