コード例 #1
0
ファイル: APIController.cs プロジェクト: sal0/notifications
        public APIResult UnsubscribeEvent(SubscribeUserModel model)
        {
            try
            {
                if (model == null)
                    throw new Exception("Please pass model");

                using (var bo = new ApplicationBusinessObject())
                {
                    if (!bo.Check(model.ApplicationId, model.ApplicationSecretKey))
                        throw new Exception("Invalid application credentials");
                }

                int eventId;
                using (var bo = new EventBusinessObject())
                {
                    var ev = bo.GetByKey(model.ApplicationId, model.EventKey);
                    if (ev == null)
                        throw new Exception("Event not found");

                    eventId = ev.Id;
                }

                int userId;
                using (var bo = new UserBusinessObject())
                {
                    var user = bo.GetByExternalUserId(model.ApplicationId, model.ExternalUserId);
                    if (user == null)
                        throw new Exception("User not found");

                    userId = user.Id;
                }

                Provider provider;
                using (var bo = new ProviderBusinessObject())
                {
                    provider = bo.GetByKey(model.ProviderKey);
                    if (provider == null)
                        throw new Exception("Provider not found");
                }

                var subscription = new Subscription
                {
                    EventId = eventId,
                    UserId = userId,
                    ProviderId = provider.Id
                };

                using (var bo = new SubscriptionBusinessObject())
                {
                    bo.Unsubscribe(subscription);
                }

            }
            catch (Exception ex)
            {
                return new APIResult(ex.Message, ex.ToString());
            }

            return new APIResult();
        }
コード例 #2
0
ファイル: APIController.cs プロジェクト: sal0/notifications
        public APIResult<List<int>> Send(SendModel model)
        {
            var messageIds = new List<int>();

            try
            {
                if (model == null)
                    throw new Exception("Please pass model");

                using (var bo = new ApplicationBusinessObject())
                {
                    if (!bo.Check(model.ApplicationId, model.ApplicationSecretKey))
                        throw new Exception("Invalid application credentials");
                }

                if (String.IsNullOrEmpty(model.Content))
                    throw new Exception("Content can't be empty");

                Provider provider;
                using (var bo = new ProviderBusinessObject())
                {
                    provider = bo.GetByKey(model.ProviderKey);
                    if (provider == null)
                        throw new Exception("Provider not found");
                }

                if (model.ExternalUserIds == null)
                    model.ExternalUserIds = new List<string>();

                if (!String.IsNullOrEmpty(model.ExternalUserId))
                    model.ExternalUserIds.Add(model.ExternalUserId);

                var userInfos = GetUserInfos(model.ApplicationId, model.ExternalUserIds, provider.Id);

                if (!String.IsNullOrEmpty(model.To))
                    userInfos.Add(new Tuple<int?, string>(null, model.To));

                if (userInfos.Count == 0)
                    throw new Exception("Please set: ExternalUserId, ExternalUserIds, or To");

                var groupId = Guid.NewGuid();

                foreach (var info in userInfos)
                {
                    var message = new Message
                    {
                        UserId = info.Item1,
                        To = info.Item2,
                        ProviderId = provider.Id,
                        ApplicationId = model.ApplicationId,
                        Subject = model.Subject,
                        Content = model.Content,
                        ProcessDate = model.ProcessDate,
                        GroupId = groupId,
                        Priority = model.Priority ?? MessagePriority.Normal
                    };

                    using (var bo = new MessageBusinessObject())
                    {
                        var id = bo.Create(message);
                        messageIds.Add(id);
                    }
                }
            }
            catch (Exception ex)
            {
                return new APIResult<List<int>>(ex.Message, ex.ToString());
            }

            return new APIResult<List<int>>(messageIds);
        }