Пример #1
0
        public async Task <ChatRoomDto> GetOrCreatePrivateRoomWith(string userId)
        {
            string currentUserId = identityService.CurrentUserId;

            if (currentUserId == userId)
            {
                throw new ArgumentException("Same user error");
            }

            if (!(await this.userRepo.FindByIdAsync(userId) is UserEntity user))
            {
                throw new ArgumentException("User does not exists");
            }

            var privateRoom = await roomRepo.GetPrivateRoomAsync(currentUserId, userId);

            if (privateRoom == null)
            {
                ChatRoomEntity newChatRoom = new ChatRoomEntity
                {
                    IsPrivate   = true,
                    OwnerUserId = currentUserId.ToLower(),
                    UserRooms   = new List <UsersChatRoomsEntity> {
                        new UsersChatRoomsEntity {
                            UserId = currentUserId
                        }, new UsersChatRoomsEntity {
                            UserId = userId
                        }
                    }
                };
                string id = await roomRepo.CreateAsync(newChatRoom);

                privateRoom = await roomRepo.FindByIdAsync(id);
            }

            ChatRoomDto model = mapper.Map <ChatRoomEntity, ChatRoomDto>(privateRoom);

            await notificator.Notificate(new NewChatNotification(model, model.Participants.Select(x => x.Id)));

            return(model);
        }