Beispiel #1
0
        public async void DeleteChat_FAIL(int roomId)
        {
            var now    = DateTime.Now;
            var chats  = new List <RoomChat>();
            var chats2 = new List <RoomChat>();

            //Arrange
            chats.Add(new RoomChat()
            {
                Id = 1, RoomId = roomId, UserId = "testUser", Date = now, Content = "testChat", Type = 1,
            });
            chats.Add(new RoomChat()
            {
                Id = 2, RoomId = roomId, UserId = "testUser", Date = now, Content = "testChat", Type = 1,
            });
            chats2.Add(new RoomChat()
            {
                Id = 3, RoomId = roomId, UserId = "testUser", Date = now, Content = "testChat", Type = 1,
            });

            // Act
            foreach (var chat in chats)
            {
                await RoomChatDAO.Create(roomContext, chat);
            }

            var result     = RoomChatDAO.DeleteRoomChat(roomContext, chats2);
            var resultChat = RoomChatDAO.GetChatByRoomId(roomContext, roomId);

            Assert.NotEmpty(resultChat);
        }
Beispiel #2
0
        public async void GetChatByRoomId(int roomId)
        {
            var chats = new List <RoomChat>();

            //Arrange
            chats.Add(new RoomChat()
            {
                Id = 1, RoomId = 1, UserId = "testUser", Date = DateTime.Now, Content = "testChat", Type = 1,
            });
            chats.Add(new RoomChat()
            {
                Id = 2, RoomId = 1, UserId = "testUser", Date = DateTime.Now, Content = "testChat", Type = 1,
            });
            chats.Add(new RoomChat()
            {
                Id = 3, RoomId = 1, UserId = "testUser", Date = DateTime.Now, Content = "testChat", Type = 1,
            });

            // Act
            foreach (var chat in chats)
            {
                await RoomChatDAO.Create(roomContext, chat);
            }

            var result = RoomChatDAO.GetChatByRoomId(roomContext, roomId);

            if (roomId == 1)
            {
                Assert.Equal(3, result.Count);
            }
            else
            {
                Assert.Empty(result);
            }
        }
Beispiel #3
0
        public async void GetLastChat(int roomId)
        {
            var now   = DateTime.Now;
            var chats = new List <RoomChat>();

            //Arrange
            chats.Add(new RoomChat()
            {
                Id = 1, RoomId = roomId, UserId = "testUser", Date = now, Content = "testChat", Type = 1,
            });
            chats.Add(new RoomChat()
            {
                Id = 2, RoomId = roomId, UserId = "testUser", Date = now, Content = "testChat", Type = 1,
            });
            chats.Add(new RoomChat()
            {
                Id = 3, RoomId = roomId, UserId = "testUser", Date = now, Content = "testChat", Type = 1,
            });

            // Act
            foreach (var chat in chats)
            {
                await RoomChatDAO.Create(roomContext, chat);
            }

            var result = RoomChatDAO.GetLastChat(roomContext, roomId);

            Assert.Equal(3, result.Id);
            Assert.Equal(roomId, result.RoomId);
            Assert.Equal("testUser", result.UserId);
            Assert.Equal("testChat", result.Content);
            Assert.Equal(1, result.Type);
        }
Beispiel #4
0
        public void GetChatByROomId()
        {
            var chats = new List <RoomChat>();

            chats.Add(new RoomChat()
            {
                Id = 1, RoomId = 1, UserId = "testUser", Date = DateTime.Now, Content = "testChat", Type = 1,
            });
            chats.Add(new RoomChat()
            {
                Id = 2, RoomId = 1, UserId = "testUser", Date = DateTime.Now, Content = "testChat", Type = 1,
            });
            chats.Add(new RoomChat()
            {
                Id = 3, RoomId = 1, UserId = "testUser", Date = DateTime.Now, Content = "testChat", Type = 1,
            });

            foreach (var chat in chats)
            {
                RoomChatDAO.Create(_context, chat);
            }
            var rChats = RoomService.GetChatByRoomId(_context, 1);

            Assert.Equal(3, rChats.Count);
        }
Beispiel #5
0
        public RoomServiceTest()
        {
            //setting up context
            factory  = new ConnectionFactory();
            _context = factory.CreateRoomDbContextForInMemory();
            _context.Database.EnsureDeleted();
            _context.Database.EnsureCreated();
            _context.SaveChanges();
            var room = new Room()
            {
                RoomId     = 1,
                Subject    = "testSubject",
                ClassName  = "testName",
                CreatorId  = "testUser",
                SemesterId = 1
            };

            RoomDAO.Create(_context, room);

            var chats = new List <RoomChat>();

            chats.Add(new RoomChat()
            {
                Id = 1, RoomId = 1, UserId = "testUser", Date = DateTime.Now, Content = "testChat", Type = 1,
            });
            chats.Add(new RoomChat()
            {
                Id = 2, RoomId = 1, UserId = "testUser", Date = DateTime.Now, Content = "testChat", Type = 1,
            });
            chats.Add(new RoomChat()
            {
                Id = 3, RoomId = 1, UserId = "testUser", Date = DateTime.Now, Content = "testChat", Type = 1,
            });

            foreach (var chat in chats)
            {
                RoomChatDAO.Create(_context, chat);
            }

            var links = new List <RoomUserLink>();

            links.Add(new RoomUserLink()
            {
                RoomId = 1, RoomUserId = 1, UserId = "testUser"
            });
            links.Add(new RoomUserLink()
            {
                RoomId = 1, RoomUserId = 2, UserId = "testUser1"
            });
            links.Add(new RoomUserLink()
            {
                RoomId = 1, RoomUserId = 3, UserId = "testUser2"
            });

            RoomUserLinkDAO.Create(_context, links);


            _env = new Mock <IWebHostEnvironment>();
            _env.Setup(f => f.ContentRootPath).Returns("");
        }
Beispiel #6
0
        public async void ChatCreateSuccessfully(int roomid)
        {
            //Arrange
            var chat = new RoomChat()
            {
                Id      = 1,
                RoomId  = roomid,
                UserId  = "testUser",
                Date    = DateTime.Now,
                Content = "testChat",
                Type    = 1,
            };
            // Act
            var result = await RoomChatDAO.Create(roomContext, chat);

            // Assert
            Assert.Equal((int)HttpStatusCode.OK, ((ObjectResult)result).StatusCode);
        }
Beispiel #7
0
        public static async Task <IActionResult> CreateRoomChat(RoomDBContext context, HttpRequest request, IWebHostEnvironment env)
        {
            var type   = (ChatType)Convert.ToInt32(request.Form["chatType"]);
            var roomId = Convert.ToInt32(request.Form["roomId"]);

            switch (type)
            {
            case ChatType.Text:
            {
                var roomChat = new RoomChat
                {
                    RoomId  = roomId,
                    UserId  = request.Form["userId"],
                    Content = request.Form["content"],
                    Date    = DateTime.Now,
                    Type    = (int)ChatType.Text
                };
                return(await RoomChatDAO.Create(context, roomChat));
            }

            case ChatType.Image:
            {
                var img       = request.Form.Files[0];
                var extension = Path.GetExtension(img.FileName);

                var imgName = Convert.ToBase64String(
                    System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString())
                    );
                var path = Path.Combine(env.ContentRootPath, $"Files/{roomId}/Chat/Images");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                var imgPath = Path.Combine(path, imgName + extension);
                if (img.Length > 0)
                {
                    using var fileStream = new FileStream(imgPath, FileMode.Create);
                    img.CopyTo(fileStream);
                }
                var roomChat = new RoomChat
                {
                    RoomId   = roomId,
                    UserId   = request.Form["userId"],
                    Content  = $"api/rooms/chat/getFile?fileName={imgName + extension}&roomId={roomId}&type={(int)ChatType.Image}&realName={Path.GetFileName(img.FileName)}",
                    Date     = DateTime.Now,
                    Type     = (int)ChatType.Image,
                    FileName = Path.GetFileName(img.FileName)
                };
                return(await RoomChatDAO.Create(context, roomChat));
            }

            case ChatType.File:
            {
                var file      = request.Form.Files[0];
                var extension = Path.GetExtension(file.FileName);
                var fileName  = Convert.ToBase64String(
                    System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString())
                    );
                var path = Path.Combine(env.ContentRootPath, $"Files/{roomId}/Chat/Files");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                var filePath = Path.Combine(path, fileName + extension);
                if (file.Length > 0)
                {
                    using var fileStream = new FileStream(filePath, FileMode.Create);
                    file.CopyTo(fileStream);
                }
                var roomChat = new RoomChat
                {
                    RoomId   = roomId,
                    UserId   = request.Form["userId"],
                    Content  = $"api/rooms/chat/getFile?fileName={fileName + extension}&roomId={roomId}&type={(int)ChatType.File}&realName={Path.GetFileName(file.FileName)}",
                    Date     = DateTime.Now,
                    Type     = (int)ChatType.File,
                    FileName = Path.GetFileName(file.FileName)
                };
                return(await RoomChatDAO.Create(context, roomChat));
            }

            default:
                return(new ObjectResult(new { message = "wrong chat type" })
                {
                    StatusCode = 500,
                });
            }
        }