public bool Register(string fname, string lname, string email, string password)
        {
            using (var work = new UnitOfWork())
            {
                if (work.Users.GetAll().FirstOrDefault(x => x.Email == email) != null)
                {
                    Logger.Error(string.Format(Resources.LogRegisterExistingEmail, email));
                    return false;
                }

                var hash = new Md5CryptoService();

                ProfilePhoto photo = (new UsersPhotoService()).GetRandomPhoto();
                var user = new User(fname, lname, email, password, photo)
                {
                    HashPassword = hash.CalculateMd5Hash(password),
                    LocationId = work.Locations.GetAll().First().Id,
                    UserName = email,
                    Birthdate = DateTime.Now
                };

                work.Users.Create(user);
                work.Save();
                List<User> users = work.Users.GetAll().ToList();
                Logger.Info(string.Format(Resources.RegistrationFinished, email));

                return true;
            }
        }
 public ProfilePhoto GetProfilePhoto(Guid id)
 {
     using (var unit = new UnitOfWork())
     {
         return unit.ProfilePhotos.GetAll().FirstOrDefault(c => c.Id == id);
     }
 }
        public static async Task<IdentityResult> Registration(SocialNetwork.Domain.DataTransferObjects.RegistrationDTO model)
        {
            using (var work = new UnitOfWork())
            {
                List<Role> roles = new List<Role>();
                roles.Add(work.Roles.FindByNameAsync("User").Result);

                ProfilePhoto photo = (new UsersPhotoService()).GetRandomPhoto();
                var user = new User(model.FirstName, model.LastName, model.Email, photo)
                {
                    LocationId = work.Locations.GetAll().First().Id,
                    UserName = model.UserName,
                    Birthdate = DateTime.Now,
                    Roles = roles
                };

                try
                {
                    IdentityUserManager userManager = new IdentityUserManager(work.Users);
                    var res = await userManager.CreateAsync(user, model.Password);
                    work.Save();
                    return res;
                }
                catch (Exception)
                {                    
                    return null;
                }
            }
        }
 public List<Message> GetAllMessageHistory(Guid userId, Guid userFriendId)
 {
     var work = new UnitOfWork();
     return work.Messages.GetAll()
         .Where(c => (c.SenderId == userId && c.ReceiverId == userFriendId)
                     || (c.ReceiverId == userId && c.SenderId == userFriendId))
         .OrderBy(c => c.Date).ToList();
 }
        public bool DeleteProfile(Guid id)
        {
            using (var work = new UnitOfWork())
            {
                work.Users.Delete(id);
                work.Save();

                Logger.Info(string.Format(Resources.ProfileDeleted, id));

                return true;
            }
        }
        public FileStream GetImageById(Guid id)
        {
            using (var uow = new UnitOfWork())
            {
                var image = uow.ProfilePhotos.Get(id);
                if (image != null)
                {
                    return new FileStream(image.Url, FileMode.Open, FileAccess.Read);
                }

                return null;
            }
        }
        public void CreatePhoto(Guid id, string url)
        {
            using (UnitOfWork work = new UnitOfWork())
            {
                work.ProfilePhotos.Create(new Domain.Entities.ProfilePhoto
                {
                    Id = id,
                    Url = url
                });

                work.Save();
            }
        }
        /// <summary>
        ///     Get all friends of user
        /// </summary>
        /// <param name="id">user's id</param>
        /// <returns>Enumeration of all friends</returns>
        public List<User> GetFriends(Guid id)
        {
            List<User> friends = new List<User>();
            using (var work = new UnitOfWork())
            {
               var connections = work.Connections.GetAll()
                                     .Where(x => x.User1Id == id || x.User2Id == id).ToList();
                friends = connections.Select(con => con.User1Id == id
                    ? work.Users.GetAll().FirstOrDefault(x => x.Id == con.User2Id)
                    : work.Users.GetAll().FirstOrDefault(x => x.Id == con.User1Id)).ToList();
            }

            return friends;
        }
        /// <summary>
        /// Adding friend to user
        /// </summary>
        /// <param name="userId1">first user of friend connections</param>
        /// <param name="userId2">second user of friend connection</param>
        public void AddToFriends(Guid userId1, Guid userId2)
        {
            using (var work = new UnitOfWork())
            {
                work.Connections.Create(
                    new Connection
                    {
                        User1Id = userId1,
                        User2Id = userId2
                    });

                work.Save();
            }
        }
        public void DeletePhoto(Guid id)
        {
            using (UnitOfWork work = new UnitOfWork())
            {
                var entity = work.ProfilePhotos.Get(id);

                if (entity != null)
                {
                    if (File.Exists(entity.Url))
                    {
                        File.Delete(entity.Url);
                    }

                    work.ProfilePhotos.Delete(entity.Id);
                }
            }
        }
        //// private readonly IPathProvider pathProvider;
        //// public ImageService(IPathProvider pathProvider)
        //// {
        ////    this.pathProvider = pathProvider;
        //// }
        public ProfilePhoto Create(Guid photoId, Guid userId)
        {
            using (UnitOfWork work = new UnitOfWork())
            {
                ProfilePhoto image = work.ProfilePhotos.Get(photoId);
                var user = work.Users.Get(userId);
                if (user.ProfilePhotoId != null && work.ProfilePhotos.Get(user.ProfilePhotoId) != null)
                {
                    DeletePhoto(user.ProfilePhotoId);
                }

                user.ProfilePhotoId = image.Id;
                work.Save();

                return image;
            }
        }
        /// <summary>
        ///     Get mutual friends of two users
        /// </summary>
        /// <param name="user1">user1's id</param>
        /// <param name="user2">user2's id</param>
        /// <returns>Enumeration of mutual friends</returns>
        public IEnumerable<User> GetMutualFriends(Guid user1, Guid user2)
        {
            if (user1 == user2)
            {
                return GetFriends(user1);
            }

            using (var work = new UnitOfWork())
            {
                var mutualFriendsArr = AnalyzerBridge.GetMutualFriends(
                    work.Connections.GetAll().ToArray(),
                    user1,
                    user2);

                return mutualFriendsArr.Select(
                    friend => work.Users.GetAll()
                        .FirstOrDefault(x => x.Id == friend))
                    .ToList();
            }
        }
        private static void Main()
        {
            using (var work = new UnitOfWork())
            {
                var res = work.Cities;
            }

            var iOService = NinjectKernel.Get<IInputOutputSevice>();

            // Run Bot1
            var botRunner = new BotRunner();
            botRunner.Run();

            iOService.Out.WriteLine(Resources.InfoGreeting);
            Logger.Info(Resources.InfoGreeting);

            Command authenHandlers = GetAuthenticationCommand();
            Command loggedInHandlers = GetLoggedInCommand();

            var exitCommand = false;
            exitMethod = (sender, e) => { exitCommand = true; };
            while (!exitCommand)
            {
                while (!CommandHandler.Session.IsLogged)
                {
                    iOService.Out.WriteLine(Resources.InfoFirsUsage);
                    iOService.Out.Write(Resources.InfoNewCommandLine);

                    authenHandlers.Execute(iOService.In.ReadLine());

                    iOService.Out.WriteLine();
                }

                iOService.Out.Write(Resources.InfoNewCommandLine);
                loggedInHandlers.Execute(iOService.In.ReadLine());

                iOService.Out.WriteLine();
            }
        }
        /// <summary>
        ///     Add user to current session if he is regisrated.
        /// </summary>
        /// <param name="eMail"></param>
        /// <param name="password"></param>
        /// <returns> Returns session key </returns>
        public string LogIn(string eMail, string password)
        {
            string sessionKey = null;
            using (var work = new UnitOfWork())
            {
                string hashedPassword = password;

                var existingSession = SessionManager.GetSessionByEmail(eMail);
                if (existingSession != null)
                {
                    return existingSession.SessionKey;
                }

                var user = work.Users.GetAll().FirstOrDefault(
                    u => u.Email == eMail && u.HashPassword == hashedPassword);

                if (user != null)
                {
                    sessionKey = SessionManager.CreateSession(user);
                }

                return sessionKey;
            }
        }
 public CachingService()
 {
     work = new UnitOfWork();
 }