/// <summary> /// Delete notifications for user from db. /// </summary> /// <param name="userId">user id.</param> /// <returns>true if operations success; otherwise, false.</returns> public static bool RemoveNotifications(int userId) { using (var DBcontext = new LinqToSqlDataContext()) { // get notifications var data = from notifications in DBcontext.Notifications where notifications.user_one.Equals(userId) select notifications; foreach (var notification in data) { DBcontext.Notifications.DeleteOnSubmit(notification); } try { DBcontext.SubmitChanges(); return(true); } catch (Exception e) { log.Error(e); return(false); } } }
/// <summary> /// Remove friend. /// </summary> /// <param name="userOneId">user one id.</param> /// <param name="userTwoId">user two id.</param> /// <returns>true if operations success; otherwise, false.</returns> public static bool RemoveFriend(int userOneId, int userTwoId) { using (var DBcontext = new LinqToSqlDataContext()) { // get friendship records var data = from friendship in DBcontext.Friends where (friendship.friend_one.Equals(userOneId) & friendship.friend_two.Equals(userTwoId) & friendship.accepted) | (friendship.friend_one.Equals(userTwoId) & friendship.friend_two.Equals(userOneId) & friendship.accepted) select friendship; foreach (var friendship in data) { DBcontext.Friends.DeleteOnSubmit(friendship); } try { DBcontext.SubmitChanges(); return(true); } catch (Exception e) { log.Error(e); return(false); } } }
/// <summary> /// Add or update friendship in database. /// </summary> /// <param name="userOneId">user one id.</param> /// <param name="userTwoId">user two id.</param> /// <param name="accepted">is friendship request accepted.</param> /// <returns>true if operations success; otherwise, false.</returns> public static bool SetFriendship(int userOneId, int userTwoId, bool accepted) { using (var DBcontext = new LinqToSqlDataContext()) { // get friendship records var data = from friendship in DBcontext.Friends where (friendship.friend_one.Equals(userOneId) & friendship.friend_two.Equals(userTwoId)) | (friendship.friend_two.Equals(userOneId) & friendship.friend_one.Equals(userTwoId)) select friendship; // already friends if (data.Any() && data.First().accepted) { return(false); } else { // accept friendship request if (data.Any() && !data.First().accepted) { data.First().accepted = accepted; } // add friendship request else { Friendship f = new Friendship { friend_one = userOneId, friend_two = userTwoId, accepted = accepted }; DBcontext.Friends.InsertOnSubmit(f); } try { DBcontext.SubmitChanges(); return(true); } catch (Exception e) { log.Error(e); return(false); } } } }
/// <summary> /// Do registration. /// </summary> /// <param name="login">user's login.</param> /// <param name="password">user's password.</param> /// <returns>true if operation had success; otherwise, false.</returns> public static bool Register(string login, string password) { using (var DBcontext = new LinqToSqlDataContext()) { // get user var user = from users in DBcontext.Users where users.login.Equals(login) select users; if (user.Any()) { // account already exist return(false); } else { // new user User newUser = new User { login = login, password = PasswordHash.PasswordHash.CreateHash(password) }; DBcontext.Users.InsertOnSubmit(newUser); try { DBcontext.SubmitChanges(); return(true); } catch (Exception e) { log.Error(e); return(false); } } } }
/// <summary> /// Add notification about action to db. /// </summary> /// <param name="userOneId">user one id.</param> /// <param name="userTwoId">user two id.</param> /// <param name="action">action.</param> /// <param name="actionTime">time of action.</param> /// <returns>true if operations success; otherwise, false.</returns> public static bool AddNotification(int userOneId, int userTwoId, UserActions action, DateTime actionTime) { using (var DBcontext = new LinqToSqlDataContext()) { // new notification Notification newNotification; switch (action) { case UserActions.AcceptFriendship: newNotification = new Notification { user_one = userOneId, user_two = userTwoId, time = actionTime, accept_friendship = true }; break; case UserActions.RejectFriendship: newNotification = new Notification { user_one = userOneId, user_two = userTwoId, time = actionTime, reject_friendship = true }; break; case UserActions.SendFriendshipRequest: newNotification = new Notification { user_one = userOneId, user_two = userTwoId, time = actionTime, send_friendship = true }; break; case UserActions.CancelFriendshipRequest: newNotification = new Notification { user_one = userOneId, user_two = userTwoId, time = actionTime, cancel_friendship = true }; break; case UserActions.RemoveFromFriends: newNotification = new Notification { user_one = userOneId, user_two = userTwoId, time = actionTime, remove_friend = true }; break; case UserActions.MessageSended: newNotification = new Notification { user_one = userOneId, user_two = userTwoId, time = actionTime }; break; default: return(false); } DBcontext.Notifications.InsertOnSubmit(newNotification); try { DBcontext.SubmitChanges(); return(true); } catch (Exception e) { log.Error(e); return(false); } } }
/// <summary> /// Add new reply (conversation) to database. /// </summary> /// <param name="senderId">sender's id.</param> /// <param name="receiverId">receiver's id.</param> /// <param name="text">text of reply.</param> /// <param name="time">time.</param> /// <returns>true if operations success; otherwise, false.</returns> public static bool AddNewReply(int senderId, int receiverId, string text, DateTime time) { using (var DBcontext = new LinqToSqlDataContext()) { // get conversation id var data = from conversation in DBcontext.Conversations where (conversation.user_one.Equals(senderId) & conversation.user_two.Equals(receiverId)) | (conversation.user_one.Equals(receiverId) & conversation.user_two.Equals(senderId)) select conversation.conversation_id; int conversationId; // conversation exist if (data.Any()) { conversationId = data.First(); } // conversation dont exist else { // new conversation Conversation conv = new Conversation { user_one = senderId, user_two = receiverId }; DBcontext.Conversations.InsertOnSubmit(conv); try { DBcontext.SubmitChanges(); conversationId = conv.conversation_id; } catch (Exception e) { log.Error(e); return(false); } } // new reply ConversationReply reply = new ConversationReply { reply = text, conversation_id = conversationId, user_id = senderId, time = time }; DBcontext.Conversation_replies.InsertOnSubmit(reply); try { DBcontext.SubmitChanges(); return(true); } catch (Exception e) { log.Error(e); return(false); } } }
/// <summary> /// Add or update friendship in database. /// </summary> /// <param name="userOneId">user one id.</param> /// <param name="userTwoId">user two id.</param> /// <param name="accepted">is friendship request accepted.</param> /// <returns>true if operations success; otherwise, false.</returns> public static bool SetFriendship(int userOneId, int userTwoId, bool accepted) { using (var DBcontext = new LinqToSqlDataContext()) { // get friendship records var data = from friendship in DBcontext.Friends where (friendship.friend_one.Equals(userOneId) & friendship.friend_two.Equals(userTwoId)) | (friendship.friend_two.Equals(userOneId) & friendship.friend_one.Equals(userTwoId)) select friendship; // already friends if (data.Any() && data.First().accepted) { return false; } else { // accept friendship request if (data.Any() && !data.First().accepted) { data.First().accepted = accepted; } // add friendship request else { Friendship f = new Friendship { friend_one = userOneId, friend_two = userTwoId, accepted = accepted }; DBcontext.Friends.InsertOnSubmit(f); } try { DBcontext.SubmitChanges(); return true; } catch (Exception e) { log.Error(e); return false; } } } }
/// <summary> /// Delete notifications for user from db. /// </summary> /// <param name="userId">user id.</param> /// <returns>true if operations success; otherwise, false.</returns> public static bool RemoveNotifications(int userId) { using (var DBcontext = new LinqToSqlDataContext()) { // get notifications var data = from notifications in DBcontext.Notifications where notifications.user_one.Equals(userId) select notifications; foreach (var notification in data) { DBcontext.Notifications.DeleteOnSubmit(notification); } try { DBcontext.SubmitChanges(); return true; } catch (Exception e) { log.Error(e); return false; } } }
/// <summary> /// Remove friendship request. /// </summary> /// <param name="userOneId">user one id.</param> /// <param name="userTwoId">user two id.</param> /// <returns>true if operations success; otherwise, false.</returns> public static bool RemoveFriendshipRequest(int userOneId, int userTwoId) { using (var DBcontext = new LinqToSqlDataContext()) { // get friendsip requests var data = from friendship in DBcontext.Friends where friendship.friend_one.Equals(userOneId) & friendship.friend_two.Equals(userTwoId) & !friendship.accepted select friendship; foreach (var friendship in data) { DBcontext.Friends.DeleteOnSubmit(friendship); } try { DBcontext.SubmitChanges(); return true; } catch (Exception e) { log.Error(e); return false; } } }
/// <summary> /// Do registration. /// </summary> /// <param name="login">user's login.</param> /// <param name="password">user's password.</param> /// <returns>true if operation had success; otherwise, false.</returns> public static bool Register(string login, string password) { using (var DBcontext = new LinqToSqlDataContext()) { // get user var user = from users in DBcontext.Users where users.login.Equals(login) select users; if (user.Any()) { // account already exist return false; } else { // new user User newUser = new User { login = login, password = PasswordHash.PasswordHash.CreateHash(password) }; DBcontext.Users.InsertOnSubmit(newUser); try { DBcontext.SubmitChanges(); return true; } catch (Exception e) { log.Error(e); return false; } } } }
/// <summary> /// Add new reply (conversation) to database. /// </summary> /// <param name="senderId">sender's id.</param> /// <param name="receiverId">receiver's id.</param> /// <param name="text">text of reply.</param> /// <param name="time">time.</param> /// <returns>true if operations success; otherwise, false.</returns> public static bool AddNewReply(int senderId, int receiverId, string text, DateTime time) { using (var DBcontext = new LinqToSqlDataContext()) { // get conversation id var data = from conversation in DBcontext.Conversations where (conversation.user_one.Equals(senderId) & conversation.user_two.Equals(receiverId)) | (conversation.user_one.Equals(receiverId) & conversation.user_two.Equals(senderId)) select conversation.conversation_id; int conversationId; // conversation exist if (data.Any()) { conversationId = data.First(); } // conversation dont exist else { // new conversation Conversation conv = new Conversation { user_one = senderId, user_two = receiverId }; DBcontext.Conversations.InsertOnSubmit(conv); try { DBcontext.SubmitChanges(); conversationId = conv.conversation_id; } catch (Exception e) { log.Error(e); return false; } } // new reply ConversationReply reply = new ConversationReply { reply = text, conversation_id = conversationId, user_id = senderId, time = time }; DBcontext.Conversation_replies.InsertOnSubmit(reply); try { DBcontext.SubmitChanges(); return true; } catch (Exception e) { log.Error(e); return false; } } }
/// <summary> /// Add notification about action to db. /// </summary> /// <param name="userOneId">user one id.</param> /// <param name="userTwoId">user two id.</param> /// <param name="action">action.</param> /// <param name="actionTime">time of action.</param> /// <returns>true if operations success; otherwise, false.</returns> public static bool AddNotification(int userOneId, int userTwoId, UserActions action, DateTime actionTime) { using (var DBcontext = new LinqToSqlDataContext()) { // new notification Notification newNotification; switch (action) { case UserActions.AcceptFriendship: newNotification = new Notification { user_one = userOneId, user_two = userTwoId, time = actionTime, accept_friendship = true }; break; case UserActions.RejectFriendship: newNotification = new Notification { user_one = userOneId, user_two = userTwoId, time = actionTime, reject_friendship = true }; break; case UserActions.SendFriendshipRequest: newNotification = new Notification { user_one = userOneId, user_two = userTwoId, time = actionTime, send_friendship = true }; break; case UserActions.CancelFriendshipRequest: newNotification = new Notification { user_one = userOneId, user_two = userTwoId, time = actionTime, cancel_friendship = true }; break; case UserActions.RemoveFromFriends: newNotification = new Notification { user_one = userOneId, user_two = userTwoId, time = actionTime, remove_friend = true }; break; default: return false; } DBcontext.Notifications.InsertOnSubmit(newNotification); try { DBcontext.SubmitChanges(); return true; } catch (Exception e) { log.Error(e); return false; } } }