private void CreateContactAndDialog(DbMain.EFDbContext.ChatEntities db, out DbMain.EFDbContext.Contact contact, DbMain.EFDbContext.User user, RelationStatus status) { contact = new DbMain.EFDbContext.Contact() { AdderId = curUser.Id, InvitedId = user.Id, RelationTypeId = (int)status }; DbMain.EFDbContext.Conversation conversation = new DbMain.EFDbContext.Conversation() { AuthorId = curUser.Id, PartnerId = user.Id, Name = $"Dialog: {curUser.Name} - {user.Name}", ConversationTypeId = (int)ConversationType.Dialog }; contact.Conversation = conversation; DbMain.EFDbContext.ConversationMember member = new DbMain.EFDbContext.ConversationMember() { MemberId = curUser.Id, Conversation = conversation, MemberStatusId = (int)ConversationMemberStatus.Admin, }; DbMain.EFDbContext.ConversationMember member2 = new DbMain.EFDbContext.ConversationMember() { MemberId = user.Id, Conversation = conversation, MemberStatusId = (int)(status != RelationStatus.BlockedByMe ? ConversationMemberStatus.Admin : ConversationMemberStatus.Blocked), AddedId = curUser.Id }; db.ConversationMembers.Add(member); db.ConversationMembers.Add(member2); db.Conversations.Add(conversation); db.Contacts.Add(contact); }
public OperationResult <Conversation> CreateConversation(string Name, bool IsOpen = false) { Console.WriteLine("Create conversation"); try { using (DbMain.EFDbContext.ChatEntities db = new DbMain.EFDbContext.ChatEntities()) { DbMain.EFDbContext.Conversation conversation = new DbMain.EFDbContext.Conversation() { AuthorId = curUser.Id, Name = Name, ConversationTypeId = (int)(IsOpen ? ConversationType.OpenConversation : ConversationType.PrivateConversation) }; db.ConversationMembers.Add(new DbMain.EFDbContext.ConversationMember() { Conversation = conversation, MemberId = curUser.Id, MemberStatusId = (int)ConversationMemberStatus.Admin }); db.Conversations.Add(conversation); if (db.SaveChanges() > 0) { return(new OperationResult <Conversation>(new Conversation() { ConversationType = IsOpen ? ConversationType.OpenConversation : ConversationType.PrivateConversation, Id = conversation.Id, MyStatus = ConversationMemberStatus.Admin, Name = Name })); } return(new OperationResult <Conversation>(null, false, "Internal error")); } } catch (Exception ex) { return(new OperationResult <Conversation>(null, false, "Internal error")); } }