public object getMessageId(int chatId) { var context = ConnectionService.GetContext(); var messId = (from allMessChat in context.Messages where allMessChat.ChatId == chatId select allMessChat).Select(m => m.Id); return(new { error = false, messageId = messId.ToList() }); }
public object DelitingFromChat(int chatId, int userId, string token) { var context = ConnectionService.GetContext(); var Tkn = context.Tokens.Where(t => t.UserId == userId).FirstOrDefault(); if (Tkn == null || Tkn.TokenId == token) { return(new { error = true, message = "неверный токен" }); } var findChat = context.Chats.Where(fch => fch.Id == chatId).FirstOrDefault(); if (findChat != null) { var findUser = context.UserChats.Where(fu => fu.UserId == userId && fu.ChatId == chatId).FirstOrDefault(); if (findUser != null) { UserChat userChat = new UserChat(); userChat.ChatId = chatId; userChat.UserId = userId; context.UserChats.Remove(userChat); context.SaveChanges(); } return(new { error = false, chat = chatId, user = userId }); } return(new { error = true, message = "ошибка удаления" }); }
public object DeleteUserFromChat(int chatId, int userId, int owenerId, string token) { var context = ConnectionService.GetContext(); var delUserChat = context.Tokens.Where(duc => duc.UserId == userId).FirstOrDefault(); UserChat userChat = context.UserChats.Where(du => du.ChatId == chatId && du.UserId == userId).FirstOrDefault(); if (userChat != null && delUserChat != null) { if (userId == owenerId && delUserChat.UserId == userId && delUserChat.TokenId == token) { context.UserChats.Remove(userChat); context.SaveChanges(); return(true); } else { var chat = context.Chats.Where(o => o.Id == chatId).FirstOrDefault(); if (chat.UserId == owenerId) { context.UserChats.Remove(userChat); context.SaveChanges(); return(new { error = false, chatId = chat.Id, userId = userId, user = owenerId }); } } } return(new { error = true, messageError = "Указанный Вами токен не подходит" }); }
public Token getToken(string login, string password) { var context = ConnectionService.GetContext(); //UserData user = new UserData(); Token token = new Token(); SHA512 shaM = new SHA512Managed(); byte[] pass = Encoding.UTF8.GetBytes(password); pass = shaM.ComputeHash(pass); //string hashString = Encoding.UTF8.GetString(pass); string hashString = Convert.ToBase64String(pass); var TokenForUser = context.Users.Where(u => u.Login == login && u.Password == hashString).FirstOrDefault(); if (TokenForUser != null) { token.UserId = TokenForUser.Id; Random rnd = new Random(); byte[] mass = new byte[256]; rnd.NextBytes(mass); var s = Convert.ToBase64String(mass); s = s.Replace('/', '0'); s = s.Replace('+', '1'); s = s.Replace('=', '2'); token.TokenId = s; context.Tokens.Add(token); context.SaveChanges(); } return(token); //context.Tokens.Add(); }
public object AddToExistChat(int chatId, int userId, string token) { var context = ConnectionService.GetContext(); var findUserInTokens = context.Tokens.Where(t => t.UserId == userId).FirstOrDefault(); if (findUserInTokens == null || findUserInTokens.TokenId != token) { return(new { error = true, message = "неверный токен" }); } var findChat = context.Chats.Where(fch => fch.Id == chatId).FirstOrDefault(); if (findChat != null) { var findUser = context.UserChats.Where(fu => fu.UserId == userId && fu.ChatId == chatId).FirstOrDefault(); var banList = context.Bans.Where(ban => ban.UserId == userId && ban.ChatId == chatId).FirstOrDefault(); if (findUser == null && findUser.ChatId != banList.ChatId && findUser.UserId != banList.UserId) { UserChat userChat = new UserChat(); userChat.UserId = userId; userChat.ChatId = chatId; context.UserChats.Add(userChat); context.SaveChanges(); } else { return(new { error = true, message = "Доступ закрыт" }); } return(new { error = false, chat = chatId, user = userId }); } return(new { error = true, message = "ошибка добавления" }); }
public object getCountMessages(int chatId) { var context = ConnectionService.GetContext(); Chat chat = context.Chats.Where(cm => cm.Id == chatId).FirstOrDefault(); if (chat != null) { int count = context.Messages.Where(c => c.ChatId == chatId).Count(); return(new { error = false, count = count }); } return(new { error = true, messageError = "Указанный Вами токен не подходит" }); }
public object Getkeys(int userId) { var context = ConnectionService.GetContext(); var getUserId = context.Users.Where(gui => gui.Id == userId).FirstOrDefault(); if (getUserId != null) { var allKeys = (from setKeys in context.Keys where setKeys.UserId == userId select setKeys); return(new { error = false, message = allKeys.ToString() }); } return(new { error = true, message = " Ошибка поиска ключей" }); }
public object removeToken(int id, int userId, string tokenId) { var context = ConnectionService.GetContext(); var token = context.Tokens.Where(t => t.Id == id && t.TokenId == tokenId && t.UserId == userId).FirstOrDefault(); if (token != null) { context.Tokens.Remove(token); context.SaveChanges(); return(new { error = false, token = token.Id }); } return(new { error = true, messageError = "Указанный Вами токен не подходит" }); }
public object DeleteMessage(int senderId, int messageId, string token) { var context = ConnectionService.GetContext(); var delmess = context.Tokens.Where(dm => dm.UserId == senderId).FirstOrDefault(); Message message = context.Messages.Where(del => del.Id == messageId).FirstOrDefault(); if (delmess != null && message?.SenderId == senderId && delmess.TokenId == token) { context.Messages.Remove(message); context.SaveChanges(); return(new { error = false, messageId = message.Id }); } return(new { error = true, messageError = "Указанный Вами токен не подходит" }); }
public object DeleteChat(int userId, int chatid, string token) { var context = ConnectionService.GetContext(); var delchat = context.Tokens.Where(dc => dc.UserId == userId).FirstOrDefault(); var chat = context.Chats.Where(c => c.Id == chatid).FirstOrDefault(); if (delchat != null && chat.UserId == delchat.UserId && delchat.TokenId == token) { context.Chats.Remove(chat); context.SaveChanges(); return(new { error = false, chatId = chat.Id }); } return(new { error = true, messageError = "Указанный Вами токен не подходит" }); }
public object getAllMessages(int numberChat, int recieverid, int startMessegeId, int count, string token) { var context = ConnectionService.GetContext(); var allmess = context.Tokens.Where(allm => allm.UserId == recieverid).FirstOrDefault(); if (allmess != null && allmess.TokenId == token) { var mess = (from allMessage in context.Messages where allMessage.ChatId == numberChat && allMessage.SenderId != recieverid && allMessage.Id >= startMessegeId select allMessage).Take(count); return(new { error = false, message = mess.ToString() }); } return(new { error = true, messageError = "Указанный Вами токен не подходит" }); }
public object EditMessage(int messageId, int senderId, string messegetext, string token) { var context = ConnectionService.GetContext(); var editmess = context.Tokens.Where(em => em.UserId == senderId).FirstOrDefault(); Message message = context.Messages.Where(e => e.Id == messageId).FirstOrDefault(); if (editmess != null && message?.SenderId == senderId && editmess.TokenId == token) { message.MessageText = messegetext; context.Messages.Update(message); context.SaveChanges(); return(new { error = false, messageId = message.Id }); } return(new { error = true, messageError = "Указанный Вами токен не подходит" }); }
public object getOneMessage(int userId, int messageId, string token) { var context = ConnectionService.GetContext(); var getone = context.Tokens.Where(go => go.UserId == userId).FirstOrDefault(); Message message = context.Messages.Where(mes => mes.Id == messageId).FirstOrDefault(); if (message != null && getone != null && getone.TokenId == token) { UserChat userChat = context.UserChats.Where(ch => ch.ChatId == message.ChatId && ch.UserId == userId).FirstOrDefault(); if (userChat != null) { return(new { error = false, message = message }); } } return(new { error = true, messageError = "Указанный Вами токен не подходит" }); }
public object CreateChat(int userid, string name, string token) { var context = ConnectionService.GetContext(); var tokenUser = context.Tokens.Where(tc => tc.UserId == userid).FirstOrDefault(); if (tokenUser != null && tokenUser.TokenId == token) { Chat chat = new Chat() { UserId = userid, Name = name }; context.Chats.Add(chat); context.SaveChanges(); return(new{ chatName = name, chatId = chat.Id, owener = userid, error = false }); } return(new { error = true, messageError = "Указанный Вами токен не подходит" }); }
public object CreateProfile(int id, string mail, string name, string surname, string token) { var context = ConnectionService.GetContext(); var uti = context.Tokens.Where(u => u.UserId == id).FirstOrDefault(); if (uti != null && uti.TokenId == token) { Profile profile = new Profile() { Mail = mail, Name = name, Surname = surname, UserId = id }; context.Profiles.Add(profile); context.SaveChanges(); return(profile.Id); } return(new { error = true, messageError = "Указанный Вами токен не подходит" }); }
public object RemoveKeys(int id, int userId, int tokenId, string token) { var context = ConnectionService.GetContext(); var check = CheckToken.Check(tokenId, userId, token); if (check == false) { return(new { error = true, messageError = "Неправильный токен" }); } var rem = context.Keys.Where(r => r.Id == id && r.UserId == userId).FirstOrDefault(); if (rem != null) { context.Keys.Remove(rem); context.SaveChanges(); return(new { error = false, userId = userId }); } return(new { error = true, messageError = "Ошибка" }); }
public object CreateNewUser(string login, string password) { SHA512 shaM = new SHA512Managed(); var context = ConnectionService.GetContext(); byte[] pass = Encoding.UTF8.GetBytes(password); pass = shaM.ComputeHash(pass); //string hashString = Encoding.UTF8.GetString(pass); string hashString = Convert.ToBase64String(pass); UserData user = new UserData() { Login = login.ToLower(), Password = hashString }; context.Users.Add(user); context.SaveChanges(); return(user.Id); }
public object RecoveryFromBanlist(int chatId, int idBanningUser, string token, int owner) { var context = ConnectionService.GetContext(); var Tkn = context.Tokens.Where(t => t.UserId == owner).FirstOrDefault(); if (Tkn == null || Tkn.TokenId != token) { return(new { error = true, message = "неверный токен" }); } var search = context.Bans.Where(s => s.ChatId == chatId && s.UserId == idBanningUser).FirstOrDefault(); var findChat = context.Chats.Where(fch => fch.Id == chatId).FirstOrDefault(); if (findChat != null) { if (findChat.UserId != owner) { return(new { error = true, message = "вы не являетесь владельцем чата, не достаточно прав" }); } var findUser = context.UserChats.Where(fu => fu.UserId == idBanningUser && fu.ChatId == chatId).FirstOrDefault(); if (findUser == null && search != null) { UserChat userChat = new UserChat(); userChat.ChatId = chatId; userChat.UserId = idBanningUser; //BanList banList = new BanList(); //banList.ChatId = chatId; //banList.UserId = idBanningUser; context.Bans.Remove(search); context.UserChats.Add(userChat); context.SaveChanges(); return(new { error = false, chat = chatId, user = idBanningUser }); } else { return(new { error = true, message = "пользователь присутствует в чате" }); } } return(new { error = true, message = "чат не найден" }); }
public object AddToBanList(int chatId, int idBanningUser, string token, int owner) { var context = ConnectionService.GetContext(); var Tkn = context.Tokens.Where(t => t.UserId == owner).FirstOrDefault(); if (Tkn == null || Tkn.TokenId != token) { return(new { error = true, message = "неверный токен" }); } var findChat = context.Chats.Where(fch => fch.Id == chatId).FirstOrDefault(); if (findChat != null) { if (findChat.UserId == idBanningUser) { return(new { error = true, message = "Не возможно добавить владельца чата в банлист" }); } if (findChat.UserId != owner) { return(new { error = true, message = "вы не являетесь владельцем чата, не достаточно прав" }); } var findUser = context.UserChats.Where(fu => fu.UserId == idBanningUser && fu.ChatId == chatId).FirstOrDefault(); if (findUser != null) { BanList banList = new BanList(); banList.ChatId = chatId; banList.UserId = idBanningUser; context.Bans.Add(banList); context.UserChats.Remove(findUser); context.SaveChanges(); } else { return(new { error = true, message = "пользователь отсутствует в чате" }); } return(new { error = false, chat = chatId, user = idBanningUser }); } return(new { error = true, message = "чат отсутствует" }); }
public object AddMessage(int chatId, int senderId, int recieverId, string messegetext, int?replayMessageId, string token) { var context = ConnectionService.GetContext(); var addmess = context.Tokens.Where(am => am.UserId == senderId).FirstOrDefault(); Message message = new Message() { ChatId = chatId, SenderId = senderId, RecieverId = recieverId, MessageText = messegetext, ReplayTo = replayMessageId }; if (addmess != null && addmess.TokenId == token) { context.Messages.Add(message); context.SaveChanges(); return(new { error = false, messageId = message.Id }); } return(new { error = true, messageError = "Указанный Вами токен не подходит" }); }
public object AddUserToChat(int userId, int chatId, int owenerId, string token) { var context = ConnectionService.GetContext(); var userChat = context.UserChats.Where(uc => uc.ChatId == chatId && uc.UserId == userId).FirstOrDefault(); var addUserChat = context.Tokens.Where(auc => auc.UserId == userId).FirstOrDefault(); if (userChat == null && addUserChat != null) { var chat = context.UserChats.Where(c => c.ChatId == chatId).FirstOrDefault(); if (chat?.UserId == owenerId && addUserChat.UserId == chat.UserId && addUserChat.TokenId == token) { userChat = new UserChat(); userChat.UserId = userId; userChat.ChatId = chatId; context.UserChats.Add(userChat); context.SaveChanges(); return(new { error = false, userId = userId, user = owenerId }); } } return(new { error = true, messageError = "Указанный Вами токен не подходит" }); }
public object AddKeys(int userId, string key, string token, int tokenId) { var context = ConnectionService.GetContext(); var check = CheckToken.Check(tokenId, userId, token); if (check == false) { return(new { error = true, messageError = "Неправильный токен" }); } var fKey = context.Keys.Where(fk => fk.UserId == userId && fk.Key == key).FirstOrDefault(); if (fKey == null) { KeyStorage keyStorage = new KeyStorage() { UserId = userId, Key = key }; context.Keys.Add(keyStorage); context.SaveChanges(); return(new { error = false, userId = userId, key = key }); } return(new { error = true, messageError = "Ошибка" }); }