protected void ButtonSendMessage_Click(object sender, EventArgs e)
        {
            string username = this.TextBoxUsername.Text;
            string message  = this.TextBoxMessage.Text;

            var context = new WebChatContext();
            var author  = context.Users.FirstOrDefault(u => u.Username == username);

            if (author == null)
            {
                var newUser = new User()
                {
                    Username = username
                };

                author = context.Users.Add(newUser);
                context.SaveChanges();
            }

            var newMessage = new Message()
            {
                Text         = this.TextBoxMessage.Text,
                Author       = author,
                CreationDate = DateTime.Now
            };

            context.Messages.Add(newMessage);
            context.SaveChanges();
        }
예제 #2
0
        public void UpdateProfile(ProfileViewModel model)
        {
            var entity = this.ctx.User.FirstOrDefault(u => u.Id == model.Id);

            entity.Email    = model.Email;
            entity.Username = model.Username;
            ctx.User.Update(entity);
            ctx.SaveChanges();
        }
예제 #3
0
        public void AddThread(ThreadViewModel thread)
        {
            var threadModel = this.mappingService.MapThreadViewModelToThreadModel(thread);

            ctx.Thread.Add(threadModel);
            ctx.SaveChanges();
        }
        public JsonResult SendMessage()
        {
            if (HttpContext.Session.GetInt32("user") == null)
            {
                return(Json(new { status = "error", message = "User is not logged in" }));
            }

            var id = HttpContext.Session.GetInt32("user");

            // var currentUser = _context.User.FirstOrDefault(m => m.id == id);
            /// var currentUser = TempData["userData"] as User;
            var          contact   = Convert.ToInt32(Request.Form["contact"]);
            string       socket_id = Request.Form["socket_id"];
            Conversation convo     = new Conversation
            {
                sender_id   = (int)id,
                message     = Request.Form["message"],
                receiver_id = contact
            };

            _context.Add(convo);
            _context.SaveChanges();

            var conversationChannel = getConvoChannel((int)id, contact);

            pusher.TriggerAsync(
                conversationChannel,
                "new_message",
                convo,
                new TriggerOptions()
            {
                SocketId = socket_id
            });

            return(Json(convo));
        }
예제 #5
0
 public void Save()
 {
     context.SaveChanges();
 }
예제 #6
0
        public async Task CreateNewGroupAsyncShould_ReturntheSameChatIfItAlreadyExists()
        {
            const string databaseName = nameof(CreateNewGroupAsyncShould_ReturntheSameChatIfItAlreadyExists);

            using (var arrangeContext = new WebChatContext(TestUtils.GetOptions(databaseName)))
            {
                var user1 = new User()
                {
                    Id = "1", UserName = "******"
                };
                var user2 = new User()
                {
                    Id = "2", UserName = "******"
                };

                await arrangeContext.Users.AddAsync(user1);

                await arrangeContext.Users.AddAsync(user2);

                var UserGroup1 = new UserGroup()
                {
                    UserId = "1", GroupId = 1, User = user1
                };
                var UserGroup2 = new UserGroup()
                {
                    UserId = "2", GroupId = 1, User = user2
                };

                var Group1UserGroups = new List <UserGroup>();
                Group1UserGroups.Add(UserGroup1);
                Group1UserGroups.Add(UserGroup2);

                var oldGroup = new Group()
                {
                    Id               = 1,
                    IsPrivateChat    = true,
                    LastActivityDate = new DateTime(2019, 5, 5),
                    UserGroups       = Group1UserGroups
                };
                await arrangeContext.Groups.AddAsync(oldGroup);

                arrangeContext.SaveChanges();
            }


            using (var actContext =
                       new WebChatContext(TestUtils.GetOptions(databaseName)))
            {
                GroupService sut = new GroupService(actContext);

                var groupRequest = new CreateGroupRequest()
                {
                    Name = null, UserIds = { "1", "2" }
                };
                var result = await sut.CreateNewGroupAsync(groupRequest, "1");

                Assert.AreEqual(1, result.Id);
                Assert.AreEqual(true, result.IsPrivateChat);
                Assert.AreEqual(new DateTime(2019, 5, 5), result.LastActivityDate);
            };
        }
예제 #7
0
        public async Task GetUserGroupsAsyncShould_ReturnEmptyUserGroupsListIfNoUserGroupsExist()
        {
            const string databaseName = nameof(GetUserGroupsAsyncShould_ReturnEmptyUserGroupsListIfNoUserGroupsExist);

            using (var arrangeContext =
                       new WebChatContext(TestUtils.GetOptions(databaseName)))
            {
                CreateGroupRequest groupRequest = new CreateGroupRequest()
                {
                    Name    = null,
                    UserIds = { "3", "2" }
                };

                var user2 = new User()
                {
                    Id = "2", UserName = "******"
                };
                var user3 = new User()
                {
                    Id = "3", UserName = "******"
                };

                await arrangeContext.Users.AddAsync(user2);

                await arrangeContext.Users.AddAsync(user3);

                var UserGroup2 = new UserGroup()
                {
                    UserId = "2", GroupId = 1, User = user2
                };
                var UserGroup1 = new UserGroup()
                {
                    UserId = "3", GroupId = 1, User = user3
                };

                var Group1UserGroups = new List <UserGroup> {
                    UserGroup1, UserGroup2
                };

                await arrangeContext.UserGroups.AddAsync(UserGroup1);

                await arrangeContext.UserGroups.AddAsync(UserGroup2);

                var Group = new Group()
                {
                    Id               = 1,
                    IsPrivateChat    = true,
                    LastActivityDate = new DateTime(2019, 5, 5),
                    UserGroups       = Group1UserGroups
                };
                await arrangeContext.Groups.AddAsync(Group);

                arrangeContext.SaveChanges();
            }

            using (var actContext =
                       new WebChatContext(TestUtils.GetOptions(databaseName)))
            {
                GroupService sut    = new GroupService(actContext);
                var          result = await sut.GetUserGroupsAsync("1");

                Assert.AreEqual(0, result.Count);
            };
        }