Beispiel #1
0
        public static GroupResponse GroupUpdate(RoomDBContext context, HttpRequest request)
        {
            var groupId     = Convert.ToInt32(request.Form["groupId"]);
            var listUserIds = (IEnumerable <string>)JsonConvert.DeserializeObject <List <string> >(request.Form["userIds"]);
            var existLinks  = (IEnumerable <string>)context.RoomUserLink.Where(link => link.RoomId == groupId).Select(link => link.UserId).ToList();

            var group = RoomDAO.Get(context, groupId);

            if (group != null)
            {
                var deleteUserIds = existLinks.Except(listUserIds).ToList();
                var addUserIds    = listUserIds.Except(existLinks).ToList();

                var deleteLinks = deleteUserIds.Select(item => RoomUserLinkDAO.GetRoomUserLink(context, groupId, item)).ToList();

                var addLinks = addUserIds.Select(item => new RoomUserLink
                {
                    UserId = item,
                    RoomId = group.RoomId
                }).ToList();

                var result = RoomUserLinkDAO.Create(context, addLinks);
                result = RoomUserLinkDAO.Delete(context, deleteLinks);

                return(new GroupResponse
                {
                    GroupId = group.RoomId,
                    Name = group.Subject,
                    UserIds = RoomUserLinkDAO.GetRoomLink(context, group.RoomId).Select(item => item.UserId).ToList(),
                    StartTime = group.StartDate.ToUniversalTime(),
                    EndTime = group.EndDate.ToUniversalTime()
                });
            }
            return(null);
        }
Beispiel #2
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 #3
0
        public async void DeleteListLinkSuccessfully()
        {
            //Arrange
            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"
            });

            var result = await RoomUserLinkDAO.Create(roomContext, links);

            Assert.Equal((int)HttpStatusCode.OK, ((ObjectResult)result).StatusCode);

            var resultLinks = RoomUserLinkDAO.GetRoomLink(roomContext, 1);

            Assert.Equal(3, resultLinks.Count);

            result = await RoomUserLinkDAO.Delete(roomContext, links);

            Assert.Equal((int)HttpStatusCode.OK, ((ObjectResult)result).StatusCode);

            resultLinks = RoomUserLinkDAO.GetRoomLink(roomContext, 1);

            Assert.Empty(resultLinks);
        }
Beispiel #4
0
        public async void DeleteLinkFail(int roomId)
        {
            //Arrange
            var links = new List <RoomUserLink>();

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

            var result = await RoomUserLinkDAO.Create(roomContext, links);

            Assert.Equal((int)HttpStatusCode.OK, ((ObjectResult)result).StatusCode);

            var resultLinks = RoomUserLinkDAO.GetRoomLink(roomContext, roomId);

            Assert.Equal(3, resultLinks.Count);

            result = await RoomUserLinkDAO.Delete(roomContext, new RoomUserLink()
            {
                RoomId = roomId, RoomUserId = 1, UserId = "testUser12"
            });

            Assert.Equal((int)HttpStatusCode.InternalServerError, ((ObjectResult)result).StatusCode);
        }
Beispiel #5
0
        public async void GetRoomUserLink()
        {
            //Arrange
            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"
            });

            var result = await RoomUserLinkDAO.Create(roomContext, links);

            Assert.Equal((int)HttpStatusCode.OK, ((ObjectResult)result).StatusCode);

            var resultLink = RoomUserLinkDAO.GetRoomUserLink(roomContext, 1, "testUser");

            Assert.Equal(1, resultLink.RoomId);
            Assert.Equal(1, resultLink.RoomUserId);
            Assert.Equal("testUser", resultLink.UserId);

            resultLink = RoomUserLinkDAO.GetRoomUserLink(roomContext, 1, "testUser0");

            Assert.Null(resultLink);
        }
Beispiel #6
0
        public async Task LinkCreateFail(int id)
        {
            //Arrange
            var link = new RoomUserLink()
            {
                RoomId     = 1,
                RoomUserId = id,
                UserId     = "testUser"
            };
            // Act
            var result = await RoomUserLinkDAO.Create(roomContext, link);

            // Assert
            Assert.Equal((int)HttpStatusCode.OK, ((ObjectResult)result).StatusCode);

            result = await RoomUserLinkDAO.Create(roomContext, new RoomUserLink()
            {
                RoomId     = 1,
                RoomUserId = id,
                UserId     = "testUser"
            });

            // Assert
            Assert.Equal((int)HttpStatusCode.InternalServerError, ((ObjectResult)result).StatusCode);
        }
Beispiel #7
0
        public static GroupResponse CreateGroup(RoomDBContext context, HttpRequest request)
        {
            var now = DateTime.Now;

            Room room = new Room
            {
                Subject    = request.Form["name"],
                CreatorId  = request.Form["teacherId"],
                MainRoomId = Convert.ToInt32(request.Form["roomId"]),
                Group      = true,
                StartDate  = now,
                EndDate    = now
            };

            var result = RoomDAO.Create(context, room);
            var group  = RoomDAO.GetLastRoom(context);

            var listUserIds   = JsonConvert.DeserializeObject <List <string> >(request.Form["userIds"]);
            var roomUserLinks = listUserIds.Select(userId => new RoomUserLink
            {
                RoomId = group.RoomId,
                UserId = userId
            }).ToList();
            var existLink = context.RoomUserLink.Where(link => link.RoomId == group.RoomId).ToList();

            if (existLink.Count != 0)
            {
                roomUserLinks = roomUserLinks.Where(link => !existLink.Any(x => x.UserId == link.UserId)).ToList();
            }
            result = RoomUserLinkDAO.Create(context, roomUserLinks);

            return(new GroupResponse
            {
                GroupId = group.RoomId,
                Name = group.Subject,
                UserIds = RoomUserLinkDAO.GetRoomLink(context, group.RoomId).Select(item => item.UserId).ToList(),
                StartTime = group.StartDate.ToUniversalTime(),
                EndTime = group.EndDate.ToUniversalTime()
            });
        }
Beispiel #8
0
        public async Task ListLinkCreateFail()
        {
            //Arrange
            var links = new List <RoomUserLink>();

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

            // Act
            var result = await RoomUserLinkDAO.Create(roomContext, links);

            // Assert
            Assert.Equal((int)HttpStatusCode.InternalServerError, ((ObjectResult)result).StatusCode);
        }
Beispiel #9
0
        public SemesterServiceTest()
        {
            factory     = new ConnectionFactory();
            userContext = factory.CreateUserDbContextForInMemory();
            userContext.Database.EnsureDeleted();
            userContext.Database.EnsureCreated();
            userContext.SaveChanges();

            roomContext = factory.CreateRoomDbContextForInMemory();
            roomContext.Database.EnsureDeleted();
            roomContext.Database.EnsureCreated();
            roomContext.SaveChanges();

            attendanceDAO = new Mock <AttendanceDAO>(userContext);
            logDAO        = new Mock <LogDAO>(userContext);
            var iAttendanceDao = attendanceDAO.As <IAttendanceDAO>();
            var iLogDAO        = logDAO.As <ILogDAO>();
            //mocking user manager
            var users = new List <AppUser>
            {
                new AppUser
                {
                    UserName = "******",
                    Email    = "*****@*****.**"
                },
                new AppUser
                {
                    UserName = "******",
                    Email    = "*****@*****.**"
                },
                new AppUser
                {
                    UserName = "******",
                    Email    = "*****@*****.**"
                },
                new AppUser
                {
                    UserName = "******",
                    Email    = "*****@*****.**"
                },
                new AppUser
                {
                    UserName = "******",
                    Email    = "*****@*****.**"
                },
            }.AsQueryable();

            var fakeUserManager = new Mock <FakeUserManager>();

            fakeUserManager.Setup(x => x.Users).Returns(users.AsQueryable());

            fakeUserManager.Setup(x => x.DeleteAsync(It.IsAny <AppUser>()))
            .ReturnsAsync(IdentityResult.Success);
            fakeUserManager.Setup(x => x.CreateAsync(It.IsAny <AppUser>(), It.IsAny <string>()))
            .ReturnsAsync(IdentityResult.Success);
            fakeUserManager.Setup(x => x.UpdateAsync(It.IsAny <AppUser>()))
            .ReturnsAsync(IdentityResult.Success);

            var mockEnvironment = new Mock <IWebHostEnvironment>();

            //...Setup the mock as needed
            mockEnvironment
            .Setup(m => m.EnvironmentName)
            .Returns("Hosting:UnitTestEnvironment");
            mockEnvironment.Setup(m => m.ContentRootPath).Returns("");
            _SemetserService = new Mock <SemesterService>(fakeUserManager.Object, userContext, roomContext, mockEnvironment.Object, iAttendanceDao.Object, iLogDAO.Object);
            var semester = new Semester()
            {
                Id          = 1,
                Name        = "testSemester",
                File        = "TestFile.xsl",
                LastUpdated = DateTime.Now
            };

            SemesterDAO.Create(roomContext, semester);

            var room = new Room()
            {
                RoomId     = 1,
                Subject    = "testSubject",
                ClassName  = "testName",
                CreatorId  = "testUser",
                SemesterId = 1
            };

            RoomDAO.Create(roomContext, room);

            var links = new List <RoomUserLink>();

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

            // Act
            RoomUserLinkDAO.Create(roomContext, links);

            var schedule = new Timetable()
            {
                Id        = 1,
                RoomId    = 1,
                Date      = DateTime.Now.Date,
                StartTime = TimeSpan.Parse("8:00"),
                EndTime   = TimeSpan.Parse("10:00"),
            };

            // Act
            TimetableDAO.Create(roomContext, schedule);
        }