Пример #1
0
        public APIResult<List<Message>> Messages(GetMessagesModel 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");
                }

                if (String.IsNullOrEmpty(model.Ids))
                    throw new Exception("Please pass Ids parameter");

                var ids = model.Ids.Split(',')
                    .Select(x =>
                    {
                        int i;
                        return Int32.TryParse(x, out i) ? (int?)i : null;
                    })
                    .Where(x => x.HasValue)
                    .ToList();

                if (ids.Count == 0)
                    return new APIResult<List<Message>>(new List<Message>());

                using (var bo = new MessageBusinessObject())
                {
                    var result = bo.GetList(x => ids.Contains(x.Id));

                    return new APIResult<List<Message>>(result);
                }
            }
            catch (Exception ex)
            {
                return new APIResult<List<Message>>(ex.Message, ex.ToString());
            }
        }
Пример #2
0
        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);
        }