public void Invoke_ValidData_ReturnsCorrectModel()
        {
            var guid = Guid.NewGuid();

            var userRoom = new DataAccess.Models.UserRoom
            {
                CreatedOn = DateTime.Today
            };

            var messageFromDb = new List <DataAccess.Models.Message>
            {
                new DataAccess.Models.Message
                {
                    Id        = new Guid(),
                    Text      = "Testowa wiadomość",
                    CreatedOn = DateTime.Now,
                    UserRoom  = userRoom
                },
                new DataAccess.Models.Message
                {
                    Id        = new Guid(),
                    Text      = "Testowa wiadomość2",
                    CreatedOn = DateTime.Now,
                    UserRoom  = userRoom
                }
            };

            // prepare
            var mockedMessageRepository = new Mock <IMessageRepository>();

            mockedMessageRepository.Setup(r => r.GetRoomMessagesOffset(It.IsAny <Guid>(), It.IsAny <DateTime>(), It.IsAny <int>()))
            .Returns(messageFromDb.AsQueryable());
            mockedMessageRepository.Setup(r => r.GetById(It.IsAny <Guid>())).Returns(new DataAccess.Models.Message());
            var action = new GetMessagesOffsetByRoomId(mockedMessageRepository.Object);

            // action
            var message = action.Invoke(guid, Guid.NewGuid(), 2);

            // check
            Assert.NotNull(message);
            Assert.Equal("Testowa wiadomość", message[0].Text);
            Assert.Equal("Testowa wiadomość2", message[1].Text);
            Assert.Null(message[0].DeletedOn);
            mockedMessageRepository.Verify(r => r.GetById(It.IsAny <Guid>()), Times.Once);
            mockedMessageRepository.Verify(r => r.GetRoomMessagesOffset(It.IsAny <Guid>(), It.IsAny <DateTime>(), It.IsAny <int>()), Times.Once);
        }
Esempio n. 2
0
        public Guid Invoke(RoomModel room, Guid userId, Guid domainId)
        {
            if (!room.IsValid() || userId == Guid.Empty || domainId == Guid.Empty ||
                // Check for room name duplication in domain
                roomRepository.FindBy(x => x.Name == room.Name && x.Domain.Id == domainId).Count() > 0)
            {
                return(Guid.Empty);
            }

            var domain = domainRepository.GetById(domainId);

            if (domain == null)
            {
                return(Guid.Empty);
            }

            var user = userRepository.GetById(userId);

            if (user == null)
            {
                return(Guid.Empty);
            }

            var newRoom = AutoMapper.Mapper.Map <DataAccess.Models.Room>(room);

            newRoom.Owner  = AutoMapper.Mapper.Map <DataAccess.Models.User>(user);
            newRoom.Domain = AutoMapper.Mapper.Map <DataAccess.Models.Domain>(domain);

            roomRepository.Add(newRoom);

            var userRoom = new DataAccess.Models.UserRoom {
                User = user, Room = newRoom
            };

            userRoomRepository.Add(userRoom);
            _unitOfWork.Save();

            return(newRoom.Id);
        }
Esempio n. 3
0
        public bool Invoke(Guid roomId, List <Guid> users, bool invite = false)
        {
            if (roomId == Guid.Empty || users.Count <= 0 || users.Any(x => x == Guid.Empty) ||
                // Check for existing userRoom
                userRoomRepository.FindBy(x => x.Room.Id == roomId && users.Any(y => y == x.User.Id)).Count() > 0)
            {
                return(false);
            }

            var room = roomRepository.GetById(roomId);

            if (room == null || !room.IsPublic && !invite)
            {
                return(false);
            }

            foreach (var user in users)
            {
                var userFromDb = userRepository.GetById(user);

                if (userFromDb == null)
                {
                    return(false);
                }

                var userRoomToAdd = new DataAccess.Models.UserRoom {
                    User = userFromDb, Room = room
                };

                userRoomRepository.Add(userRoomToAdd);
            }

            _unitOfWork.Save();

            return(true);
        }
        public RoomModel Invoke(Guid issuerUserId, List <Guid> users)
        {
            // Check if users wants to create a conversation with himself
            if (issuerUserId == Guid.Empty || users.Count <= 0 || users.Count > 8 || users.Any(x => x.Equals(issuerUserId) || x.Equals(Guid.Empty)) || users.Distinct().Count() != users.Count)
            {
                return(null);
            }

            var issuerUser = userRepository.GetById(issuerUserId);

            if (issuerUser == null)
            {
                return(null);
            }

            var privateConversationList = userRepository.GetPrivateConversationsTargets(issuerUserId).ToList();
            var listRoom = privateConversationList.Select(x => x.Room).Distinct();

            // Check if private conversation already exists
            foreach (var room in listRoom)
            {
                var userRoomsList = privateConversationList.Where(x => x.Room == room);
                var dict          = new Dictionary <DataAccess.Models.UserRoom, bool>();
                var searchedCount = 0;

                // FIXME: Bug when creating multiple users priv conversation room when there's priv conversation to one of selected user list
                foreach (var connection in userRoomsList)
                {
                    dict.Add(connection, false);

                    if (users.Any(x => x.Equals(connection.User.Id)))
                    {
                        searchedCount += 1;
                        Debug.WriteLine("######### Znaleziono istniejące połączenie #########");
                        Debug.WriteLine(connection.User.Name);
                        dict[connection] = true;
                    }
                }

                if (searchedCount == users.Count)
                {
                    Debug.WriteLine("######### Jest tyle samo znalezień co userów do których chce sie podpiąć #########");
                }

                if (dict.All(x => x.Value))
                {
                    return(null);
                }
            }

            // Create repository method for that?
            var userList = userRepository.GetAll().Where(x => users.Contains(x.Id)).ToList();

            var roomName = "";

            foreach (var item in userList)
            {
                roomName += item.Name + ", ";
            }

            roomName += issuerUser.Name;

            var userDomain = domainRepository.GetDomainByUserId(issuerUser.Id);

            var newRoom = new DataAccess.Models.Room
            {
                Name   = roomName,
                Owner  = AutoMapper.Mapper.Map <DataAccess.Models.User>(issuerUser),
                Domain = userDomain,
                IsPrivateConversation = true,
                IsPublic = false
            };

            roomRepository.Add(newRoom);

            var userRoom = new DataAccess.Models.UserRoom {
                User = issuerUser, Room = newRoom
            };

            userRoomRepository.Add(userRoom);

            foreach (var user in userList)
            {
                var userRoom2 = new DataAccess.Models.UserRoom {
                    User = user, Room = newRoom
                };
                userRoomRepository.Add(userRoom2);
            }

            var returnRoom = AutoMapper.Mapper.Map <RoomModel>(newRoom);

            _unitOfWork.Save();

            return(returnRoom);
        }