예제 #1
0
 /// <summary>
 /// Метод возвразщает UserDTO, по номеру телефона
 /// </summary>
 /// <param name="phone"></param>
 /// <returns></returns>
 public static UserDTO GetUserByPhone(string phone)
 {
     try
     {
         using (var ctx = new BeruaContext())
         {
             UserDTO user   = null;
             var     dbUser = ctx.Users.FirstOrDefault(u => u.Phone.Contains(phone) || u.Phone.Equals(phone));
             if (dbUser != null)
             {
                 user = new UserDTO()
                 {
                     Id        = dbUser.Id,
                     Domain    = dbUser.Domain,
                     Phone     = dbUser.Phone,
                     FirstName = dbUser.FirstName,
                     LastName  = dbUser.LastName,
                     ChatId    = dbUser.ChatId
                 };
             }
             return(user);
         }
     }
     catch (Exception)
     {
         return(null);
     }
 }
예제 #2
0
        /// <summary>
        /// Метод для проверки является ли данная пара пользователь - аккаунт подпиской
        /// </summary>
        /// <param name="userId">id пользователя</param>
        /// <param name="accountKey">id аккаунта</param>
        /// <param name="type">тип аккаунта</param>
        /// <returns></returns>
        ///
        //public static bool IsSubscriptionYet(int userId, string accountKey, SocialNetworkType type)
        //{
        //    try
        //    {
        //        using (var ctx = new BeruaContext())
        //        {
        //            var dbSubscriptions = ctx.Users.Find(userId).Subscriptions;

        //            if (dbSubscriptions.FirstOrDefault(x =>
        //                 x.AccountKey.SocialNetworkType == (byte)type &&
        //                 x.AccountKeyId == accountKey) is Subscription sub)
        //                return true;
        //            else
        //                return false;
        //        }
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}


        public static bool AddSubscription(long userId, int accId)
        {
            try
            {
                using (var ctx = new BeruaContext())
                {
                    if (ctx.Users.Find(userId) is User dbUser)
                    {
                        if (dbUser.Subscriptions
                            .FirstOrDefault(x => x.AccountId == accId) is Subscription dbSub)
                        {
                            return(false);
                        }
                        else
                        {
                            dbUser.Subscriptions.Add(new Subscription
                            {
                                AccountId = accId,
                                UserId    = userId
                            });
                        }
                    }
                    if (ctx.ChangeTracker.HasChanges())
                    {
                        ctx.SaveChanges();
                    }
                }
                return(true);
            }
            catch
            {
                return(false);
            }
        }
예제 #3
0
 /// <summary>
 /// Метод возвразщает телефон пользователя, по его Id
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static string GetPhoneUser(long id)
 {
     try
     {
         using (var ctx = new BeruaContext())
         {
             var dbUser = ctx.Users.Find(id);
             if (dbUser == null)
             {
                 throw new Exception("Пользователь не найден");
             }
             return(dbUser.Phone);
         }
     }
     catch (Exception ex)
     {
         return(ex.Message);
     }
 }
예제 #4
0
        public static int AddUpdateAccount(AccountDTO acc)
        {
            int resId = 0;

            try
            {
                using (var ctx = new BeruaContext())
                {
                    if (ctx.Accounts.Find(acc.Id) is Account dbAcc)
                    {
                        resId              = acc.Id;
                        dbAcc.AvatarUrl    = acc.AvatarUrl;
                        dbAcc.KeyFacebook  = string.IsNullOrEmpty(acc.KeyFacebook) ? dbAcc.KeyFacebook : acc.KeyFacebook;
                        dbAcc.KeyInstagram = string.IsNullOrEmpty(acc.KeyInstagram) ? dbAcc.KeyInstagram : acc.KeyInstagram;
                        dbAcc.KeyVK        = string.IsNullOrEmpty(acc.KeyVK) ? dbAcc.KeyVK : acc.KeyVK;
                        dbAcc.Fullname     = acc.Fullname;
                    }
                    else
                    {
                        ctx.Accounts.Add(new Account
                        {
                            Fullname     = acc.Fullname,
                            AvatarUrl    = acc.AvatarUrl,
                            KeyFacebook  = acc.KeyFacebook ?? string.Empty,
                            KeyInstagram = acc.KeyInstagram ?? string.Empty,
                            KeyVK        = acc.KeyVK ?? string.Empty,
                        });
                    }

                    if (ctx.ChangeTracker.HasChanges())
                    {
                        ctx.SaveChanges();
                        resId = ctx.Accounts.Last().Id;
                    }
                }
                return(resId);
            }
            catch
            {
                return(resId);
            }
        }
예제 #5
0
 /// <summary>
 /// Метод возвразщает ChatId пользователя, по его Id
 /// </summary>
 /// <param name="userId"></param>
 /// <returns></returns>
 public static long GetChatIdUser(long userId)
 {
     try
     {
         using (var ctx = new BeruaContext())
         {
             if (ctx.Users.Find(userId) is User dbUser)
             {
                 return(dbUser.ChatId);
             }
             else
             {
                 throw new Exception("Пользователь не найден");
             }
         }
     }
     catch
     {
         return(0);
     }
 }
예제 #6
0
        public static bool AddUpdateUser(UserDTO user)
        {
            try
            {
                using (var ctx = new BeruaContext())
                {
                    if (ctx.Users.Find(user.Id) is User dbUser)
                    {
                        dbUser.FirstName = user.FirstName;
                        dbUser.LastName  = user.LastName;
                        dbUser.Domain    = user.Domain;
                        dbUser.ChatId    = user.ChatId;
                    }
                    else
                    {
                        ctx.Add(new User
                        {
                            Id = user.Id,
                            DateRegistration = DateTime.Now,
                            Domain           = user.Domain,
                            FirstName        = user.FirstName,
                            LastName         = user.LastName,
                            ChatId           = user.ChatId
                        });
                    }

                    if (ctx.ChangeTracker.HasChanges())
                    {
                        ctx.SaveChanges();
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #7
0
 /// <summary>
 /// Метод добавляет/обновляет ChatId чата с Telegram
 /// </summary>
 /// <param name="userId"></param>
 /// <param name="phone">Телефон без кода страны (10 цифр)</param>
 /// <returns></returns>
 public static bool AddUpdateChatIdUser(string phone, long chatId)
 {
     try
     {
         using (var ctx = new BeruaContext())
         {
             if (ctx.Users.FirstOrDefault(x => x.Phone == phone) is User dbUser)
             {
                 dbUser.ChatId = chatId;
             }
             else
             {
                 throw new Exception("Пользователь с таким телеофном не найден");
             }
             ctx.SaveChanges();
         }
         return(true);
     }
     catch
     {
         return(false);
     }
 }
예제 #8
0
 /// <summary>
 /// Метод добавляет номер телефона для пользователя
 /// </summary>
 /// <param name="userId"></param>
 /// <param name="phone">Телефон без кода страны (10 цифр)</param>
 /// <returns></returns>
 public static bool AddUpdatePhoneUser(long userId, string phone)
 {
     try
     {
         using (var ctx = new BeruaContext())
         {
             if (ctx.Users.Find(userId) is User dbUser)
             {
                 dbUser.Phone = phone;
             }
             else
             {
                 throw new Exception("Пользователь не найден");
             }
             ctx.SaveChanges();
         }
         return(true);
     }
     catch
     {
         return(false);
     }
 }
예제 #9
0
 /// <summary>
 /// Метод возвразщает UserDTO, по номеру телефона
 /// </summary>
 /// <param name="phone"></param>
 /// <returns></returns>
 public static bool?UserAddedTelegram(long chatId)
 {
     try
     {
         using (var ctx = new BeruaContext())
         {
             UserDTO user   = null;
             var     dbUser = ctx.Users.FirstOrDefault(u => u.ChatId.Equals(chatId));
             if (dbUser != null)
             {
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
     }
     catch (Exception)
     {
         return(null);
     }
 }
예제 #10
0
        public static ICollection <AccountDTO> GetSubscriptionsByUser(long userId)
        {
            var listRes = new List <AccountDTO>();

            try
            {
                using (var ctx = new BeruaContext())
                {
                    if (ctx.Users.Find(userId) is User dbUser)
                    {
                        var col = dbUser.Subscriptions.ToList();
                        var acc = ctx.Accounts.ToList();

                        listRes = dbUser.Subscriptions
                                  .Join(ctx.Accounts,
                                        sb => sb.Account,
                                        ac => ac,
                                        (sb, ac) => new AccountDTO
                        {
                            Id           = ac.Id,
                            AvatarUrl    = ac.AvatarUrl,
                            Fullname     = ac.Fullname,
                            KeyVK        = ac.KeyVK,
                            KeyFacebook  = ac.KeyFacebook,
                            KeyInstagram = ac.KeyInstagram
                        })
                                  .ToList();
                    }
                }
                return(listRes);
            }
            catch
            {
                return(listRes);
            }
        }