Beispiel #1
0
        /// <summary>
        /// Register subscription
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResponseModel Register(SubscriptionManageModel model)
        {
            // Decode the parameter because maybe when sending to server it is in raw format
            model.Parameters = model.Parameters.Decode();

            var response      = new ResponseModel();
            var subscriptions = new List <SubscriptionManageModel>();
            var contacts      = _contactService.SearchContactsByEmail(model.Email).ToList();

            try
            {
                if (contacts.Any())
                {
                    foreach (var contact in contacts)
                    {
                        contact.SubscriptionType = model.SubscriptionType;

                        // Update contact
                        _contactService.SaveContact(new ContactManageModel(contact));

                        // Save contact to subscribers
                        subscriptions.Add(new SubscriptionManageModel
                        {
                            ContactId  = contact.Id,
                            Email      = model.Email,
                            Module     = model.Module,
                            Parameters = model.Parameters
                        });
                    }
                }
                else
                {
                    var contact = new ContactManageModel
                    {
                        Email            = model.Email,
                        SubscriptionType = model.SubscriptionType
                    };

                    response = _contactService.SaveContact(contact);

                    if (response.Success)
                    {
                        model.ContactId = response.Data.ToNullableInt();
                    }

                    subscriptions.Add(new SubscriptionManageModel
                    {
                        ContactId  = model.ContactId,
                        Email      = model.Email,
                        Module     = model.Module,
                        Parameters = model.Parameters
                    });
                }

                foreach (var subscription in subscriptions)
                {
                    var existedSubscription = GetSubscription(subscription.Module, subscription.Email,
                                                              subscription.Parameters);
                    if (existedSubscription == null)
                    {
                        Mapper.CreateMap <SubscriptionManageModel, Subscription>();
                        var newSubscription =
                            Mapper.Map <SubscriptionManageModel, Subscription>(subscription);
                        Insert(newSubscription);
                    }
                }

                WorkContext.CurrentContact.Email = model.Email;
                return(new ResponseModel
                {
                    Success = true,
                    Message = T("Subscription_Message_SubscribeSuccessfully")
                });
            }
            catch (Exception exception)
            {
                _logger.Error(exception);
                response.SetMessage(T("Subscription_Message_SubscribeFailure"));
            }

            return(response);
        }