Beispiel #1
0
        /// <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;
                }
            }
        }
 partial void DeleteConversation(Conversation instance);
 partial void UpdateConversation(Conversation instance);
		private void detach_Conversations1(Conversation entity)
		{
			this.SendPropertyChanging();
			entity.User1 = null;
		}
 partial void InsertConversation(Conversation instance);
		private void attach_Conversations(Conversation entity)
		{
			this.SendPropertyChanging();
			entity.User = this;
		}