Ejemplo n.º 1
0
        public bool AddChatToParticipants(Loger loger, int[] participants, int chatId)
        {
            if (UserLogin(loger) && participants != null && participants.Length > 0 && chatId != -1)
            {
                int realChatId = GetChat(loger, chatId);
                if (realChatId != -1)
                {
                    using (Meseger ctx = new Meseger())
                    {
                        User user = ctx.Users.SingleOrDefault(a => a.Login == loger.Login);
                        Chat chat = ctx.Chats.Find(realChatId);
                        if (user.Administrator == chat.Administrator)
                        {
                            //Participant participant = new Participant();
                            foreach (int item in participants)
                            {
                                chat.Participants.Add(ctx.Users.Find(item).Participant);

                                //participant.Users.Add(ctx.Users.Find(item));
                            }
                            //participants.ForEach(p => participant.Users.Add(ctx.Users.Find(p)));
                            ctx.SaveChanges();
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        public bool AddNewChat(Loger login, string name)
        {
            if (UserLogin(login) && name != null)
            {
                using (Meseger meseger = new Meseger())
                {
                    User user = meseger.Users.SingleOrDefault <User>(a => a.Login == login.Login);
                    meseger.Entry(user).State = System.Data.Entity.EntityState.Unchanged;
                    //if(UserLogin(login))
                    //{
                    //List<User> users = meseger.Users.Where(a => participants.Contains(a.Id)).ToList();
                    //users.Add(user);
                    Administrator administrator;
                    administrator = meseger.Administrators.SingleOrDefault(a => a.User.Login == user.Login);
                    if (administrator == null)
                    {
                        administrator = new Administrator {
                            User = user
                        };
                    }
                    Participant par;
                    par = user.Participant;
                    if (par == null)
                    {
                        par = new Participant {
                            Users = new List <User> {
                                user
                            }
                        };
                    }
                    Chat chat = new Chat
                    {
                        Name          = name,
                        Administrator = administrator,
                        Participants  = new List <Participant> {
                            par
                        }
                    };

                    //user.Chats.Add(chat);
                    meseger.Chats.Add(chat);
                    meseger.SaveChanges();
                    //AddChatToParticipants(participants, chat);
                    //users.ForEach(p => p.Chats.Add(chat));

                    //user.Chats.Add(chat);
                    // chat.Messages = new List<Message>();
                    //chat.Messages.Add(new Message { Chat = chat, Text = $"hello and welcome" });
                    //meseger.SaveChanges(); // Multiplicity constraint violated. The role 'User_Chats_Source' of the relationship 'Messeger.User_Chats' has multiplicity 1 or 0..1.
                    return(true);
                    //}
                    //return false;
                }
            }
            return(false);
        }
Ejemplo n.º 3
0
        public bool PushMessage(string text, Loger loger, int ChatId, string fileName)
        {
            //FileDTO file = new JavaScriptSerializer().Deserialize<FileDTO>(JSONfile);
            using (Meseger ctx = new Meseger())
            {
                User user = ctx.Users.SingleOrDefault(a => a.Login == loger.Login);
                int  id   = GetChat(loger, ChatId);
                Chat chat = ctx.Chats.SingleOrDefault(a => a.Id == id);
                if (ChatUserExists(loger, chat.Id) && UserLogin(loger))
                {
                    Sender sender = ctx.Senders.SingleOrDefault(a => a.Id == user.Id);
                    if (sender == null)
                    {
                        sender = new Sender {
                            User = user
                        };
                    }

                    Message msg = new Message {
                        Text = text, Sender = sender, DateTime = DateTime.Now, Chat = chat
                    };
                    if (fileName != String.Empty)
                    {
                        string domain       = System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);
                        string uploadFolder = domain + @"\upload\";
                        if (System.IO.File.Exists(uploadFolder + fileName))
                        {
                            string path = domain + $@"\{chat.Guid}\";
                            if (!Directory.Exists(path))
                            {
                                Directory.CreateDirectory(path);
                            }
                            System.IO.File.Move(uploadFolder + fileName, path + fileName);
                            if (System.IO.File.Exists(path + fileName))
                            {
                                Model.File file = new Model.File {
                                    Path = path + fileName
                                };
                                msg.File = file;
                            }
                        }
                    }
                    ctx.Messages.Add(msg);
                    ctx.SaveChanges();
                    return(true);
                }
                return(false);
            }
        }
Ejemplo n.º 4
0
        /////////////////////////////////////////////////////////////////////////////
        ///

        public bool AddNewUser(Loger login, string email, string phone)
        {
            if (login != null && Regex.IsMatch(login.PasswordHash, @"[0-9a-f]{32}"))
            {
                using (var ctx = new Meseger())
                {
                    User user = new User {
                        Login = login.Login, Email = email, Phone = phone, PasswordHash = login.PasswordHash, Name = "", SurName = ""
                    };
                    //ctx.Users.Add(user);
                    //ctx.SaveChanges();
                    Administrator admin = new Administrator {
                        User = user
                    };
                    Participant participant = new Participant {
                        Users = new List <User> {
                            user
                        }
                    };
                    Chat chat = new Chat {
                        Administrator = admin, Name = "Save", Participants = new List <Participant> {
                            participant
                        }
                    };
                    //ctx.SaveChanges();
                    //  Message message = new Message { Chat = chat, Text = $"Hello {user.Login}", Sender = user, Reciver = new List<User> { user } };
                    //  ctx.SaveChanges();
                    //  chat.Messages.Add(message);
                    //  ctx.SaveChanges();
                    //  user.Chats.Add(chat);
                    //  ctx.SaveChanges();
                    //  //user.PasswordHash = login.;
                    //  // meseger.Users.Add(user);
                    ////  ctx.Users.Add(user);
                    ctx.Chats.Add(chat);
                    // // ctx.Messages.Add(message);
                    ctx.SaveChanges();
                    //if(AddNewChat(user,"Save",new List<User> { user }))
                    //{
                    //
                    //}
                }
                return(true);
            }
            return(false);
        }
Ejemplo n.º 5
0
 public bool ReloadPhonelUser(Loger Userloger, string phone)
 {
     if (Userloger != null && phone != null)
     {
         using (Meseger meseger = new Meseger())
         {
             User user = meseger.Users.SingleOrDefault <User>(a => a.Login == Userloger.Login);
             if (UserLogin(Userloger))
             {
                 user.Phone = phone;
                 meseger.SaveChanges();
                 return(true);
             }
             return(false);
         }
     }
     return(false);
 }
Ejemplo n.º 6
0
 public bool ReloadEmailUser(Loger Userloger, string Email)
 {
     if (Userloger != null && Email != null)
     {
         using (Meseger meseger = new Meseger())
         {
             User user = meseger.Users.SingleOrDefault <User>(a => a.Login == Userloger.Login);
             if (UserLogin(Userloger))
             {
                 user.Email = Email;
                 meseger.SaveChanges();
                 return(true);
             }
             return(false);
         }
     }
     return(false);
 }
Ejemplo n.º 7
0
 public bool ReloadPassword(Loger loger, string NewPassHash)
 {
     if (UserLogin(loger) && NewPassHash != null)
     {
         using (Meseger ctx = new Meseger())
         {
             try
             {
                 User user = ctx.Users.SingleOrDefault <User>(a => a.Login == loger.Login);
                 user.PasswordHash = NewPassHash;
                 ctx.SaveChanges();
                 return(true);
             }
             catch (Exception)
             {
                 //throw;
             }
         }
     }
     return(false);
 }
Ejemplo n.º 8
0
 public bool RenameUser(Loger loger, string name, string surname)
 {
     if (UserLogin(loger))
     {
         using (Meseger ctx = new Meseger())
         {
             var user = ctx.Users.SingleOrDefault(a => a.Login == loger.Login);
             if (user.Name != name)
             {
                 user.Name = name;
             }
             if (user.SurName != surname)
             {
                 user.SurName = surname;
             }
             ctx.SaveChanges();
             return(true);
         }
     }
     return(false);
 }