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); }
protected override void Seed(Meseger context) { context.Users.Add(new User { Login = "******" }); base.Seed(context); }
public string GetLoginById(int id) { using (Meseger ctx = new Meseger()) { User user = ctx.Users.Find(id); return(user.Login); } }
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); }
public bool UserExists(Loger loger) { using (Meseger meseger = new Meseger()) { User user = meseger.Users.SingleOrDefault(a => a.Login == loger.Login); if (user != null) { return(true); } return(false); } }
public UserDTO GetUserProfile(Loger loger) { if (UserLogin(loger)) { UserDTO user; using (Meseger ctx = new Meseger()) { user = new UserDTO(ctx.Users.SingleOrDefault(a => a.Login == loger.Login)); } return(user); } return(null); }
public string GetPhone(Loger loger) { if (UserLogin(loger)) { string tmp = ""; using (Meseger ctx = new Meseger()) { User user = ctx.Users.SingleOrDefault <User>(a => a.Login == loger.Login); tmp = user.Phone; } return(tmp); } return(null); }
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); } }
public bool UserLogin(Loger loger) { if (loger != null) { using (Meseger meseger = new Meseger()) { User user = meseger.Users.SingleOrDefault(a => a.Login == loger.Login); if (user != null && user.PasswordHash == loger.PasswordHash) { return(true); } } } return(false); }
//public Model.File UploaderFile(Loger loger,FileDTO file) //{ // if (UserLogin(loger) && file.FileStream != null && file.ChatId != -1) // { // int realChatId = GetChat(loger, file.ChatId); // Chat chat = new Chat(); // string path = null; // using(Meseger ctx = new Meseger()) // { // chat = ctx.Chats.Find(realChatId); // path = $"./{chat.Guid}"; // if (!Directory.Exists(path)) // { // Directory.CreateDirectory(path); // } // //TransferService transfer = new TransferService(path); // //RemoteFileInfo fileInfo = new RemoteFileInfo(); // //fileInfo.FileByteStream = file.FileStream; // //fileInfo.FileName = file.FileName; // //fileInfo.Length = file.FileInfo.Length; // //transfer.UploadFile(fileInfo); // } // FileInfo info = new FileInfo(path + "/" + file.FileName); // if (info.Exists) // { // return new Model.File { Path = info.FullName }; // } // } // return null; //} public RemoteFileInfo DownloadFile(DownloadRequest request) { Message message = new Message(); Chat chat = new Chat(); using (Meseger ctx = new Meseger()) { message = ctx.Messages.Find(request.IdMessage); chat = message.Chat; } if (UserLogin(request.Loger) && message.Id == request.IdMessage && ChatUserExists(request.Loger, chat.Id)) { Model.File file = new Model.File(); using (Meseger ctx = new Meseger()) { file = ctx.Files.SingleOrDefault(a => a.Id == message.Id); } if (file.Id != message.Id) { return(null); } RemoteFileInfo result = new RemoteFileInfo(); try { //string filePath = Path.Combine(request.FileName, request.FileName); FileInfo fileInfo = new FileInfo(file.Path); // check if exists if (!fileInfo.Exists) { throw new FileNotFoundException("Файл не знайдений", file.Path); } // open stream FileStream stream = new FileStream(file.Path, System.IO.FileMode.Open, FileAccess.Read); // return result result.FileName = fileInfo.Name; result.Length = fileInfo.Length; result.FileByteStream = stream; } catch (Exception ex) { } return(result); } return(null); }
///////////////////////////////////////////////////////////////////////////// /// 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); }
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); }
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); }
public int GetChat(Loger Userloger, int id) { using (Meseger meseger = new Meseger()) { if (UserLogin(Userloger)) { User user = meseger.Users.SingleOrDefault <User>(a => a.Login == Userloger.Login); List <Chat> tmp = new List <Chat>(); var chats = user.Participant.Chats.ToList(); foreach (Chat item in chats) { tmp.Add(item); } return(tmp[id].Id); } return(-1); } }
public bool ChatUserExists(Loger loger, int Chatid) { using (Meseger meseger = new Meseger()) { User user = meseger.Users.SingleOrDefault(a => a.Login == loger.Login); Chat chat = meseger.Chats.SingleOrDefault(a => a.Id == Chatid); List <Participant> participants = chat.Participants.ToList(); foreach (Participant item in participants) { if (item == user.Participant) { return(true); } } if (chat.Administrator.User == user) { return(true); } return(false); } }
public List <string> GetChatList(Loger Userloger) { using (Meseger meseger = new Meseger()) { User user = meseger.Users.SingleOrDefault <User>(a => a.Login == Userloger.Login); if (UserLogin(Userloger)) { List <string> tmp = new List <string>(); //var chats = meseger.Chats.Select(a => a.Participants.Select(b => b.Users.Select(c => c.Login == user.Login))); //var chats = meseger.Chats.Where(a => a.Participants.Where(b => b.Users.Where(c => c.Login == user.Login) != null) != null).ToList<Chat>(); var chats = user.Participant.Chats.ToList(); foreach (Chat item in chats) { tmp.Add(item.Name); } return(tmp); } return(null); } }
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); }
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); }
public List <MessageDTO> GetMessages(Loger Userloger, int chatID) { if (chatID != -1 && Userloger != null) { bool Secure = false; List <Message> messages = new List <Message>(); using (Meseger meseger = new Meseger()) { int id = GetChat(Userloger, chatID); Chat chat = meseger.Chats.SingleOrDefault(a => a.Id == id); //bool admin = chat.Administrator.User == Userloger; //bool participant = chat.Participants.SingleOrDefault(a => Userloger == a.Users) != null; //bool all = admin || participant; if (UserLogin(Userloger) && ChatUserExists(Userloger, id)) { messages = chat.Messages.ToList <Message>(); Secure = true; } } List <MessageDTO> messageDTOs = new List <MessageDTO>(); if (Secure && messages.Count > 0) { foreach (Message item in messages) { messageDTOs.Add(new MessageDTO(item)); } return(messageDTOs); } else if (Secure && messages.Count == 0) { messageDTOs.Add(new MessageDTO { Text = "This chat is empty", SenderId = 1, FileName = String.Empty }); return(messageDTOs); } } return(null); }
public List <UserDTO> GetChatParticipant(Loger loger, int chatId) { if (UserLogin(loger) && chatId != -1) { List <UserDTO> userDTOs = new List <UserDTO>(); int realChatId = GetChat(loger, chatId); if (realChatId != -1) { HashSet <User> users = new HashSet <User>(); using (Meseger ctx = new Meseger()) { //Chat chat = ctx.Chats.Find(realChatId); List <Participant> participants = ctx.Chats.Find(realChatId).Participants.ToList(); participants.ForEach(a => users.UnionWith(a.Users)); } foreach (User item in users) { userDTOs.Add(new UserDTO(item)); } } return(userDTOs); } return(null); }
public List <UserDTO> GetUserListByFindMode(string findstring) { List <UserDTO> logers = new List <UserDTO>(); if (findstring != string.Empty) { HashSet <User> tmp = new HashSet <User>(); using (Meseger ctx = new Meseger()) { //tmp = ctx.Users.Where(t => t.Login.ToUpper().StartsWith(findstring)).ToList(); //foreach (User item in ctx.Users) //{ // if (item.Login.Contains(findstring) || item.Name.Contains(findstring) || item.SurName.Contains(findstring)) // { // tmp.Add(item); // } //} tmp.UnionWith(ctx.Users.Where(a => a.Login.Contains(findstring))); tmp.UnionWith(ctx.Users.Where(a => a.Name.Contains(findstring))); tmp.UnionWith(ctx.Users.Where(a => a.SurName.Contains(findstring))); } if (tmp.Count > 0) { foreach (User item in tmp) { if (item.Id != 1) { logers.Add(new UserDTO(item)); } } return(logers); } } //logers.Add(new LogerDTO { Login = "******" , Id = -1}); return(logers); }