public bool TradeCandy(Guid userCandyId1, Guid userCandyId2)
 {
     using (var db = new SqlConnection(_connectionString))
     {
         var candyRepo = new CandyRepository();
         var offerRepo = new OfferRepository();
         var user1     = GetUserFromDatabase(userCandyId1);
         var user2     = GetUserFromDatabase(userCandyId2);
         var candy1    = candyRepo.GetCandyFromDatabase(userCandyId1);
         var candy2    = candyRepo.GetCandyFromDatabase(userCandyId2);
         var userId1   = user1.Id;
         var userId2   = user2.Id;
         var candyId1  = candy1.Id;
         var candyId2  = candy2.Id;
         offerRepo.RemoveOffer(userCandyId1);
         offerRepo.RemoveOffer(userCandyId2);
         DeleteUserCandyEntry(userCandyId1);
         DeleteUserCandyEntry(userCandyId2);
         var sql        = @"INSERT INTO [UserCandy]
                     ([Userid], [CandyId])
                     VALUES
                         (@userId1, @candyId2),
                         (@userId2, @candyId1)";
         var parameters = new { userId1, userId2, candyId1, candyId2 };
         return(db.Execute(sql, parameters) == 2);
     }
 }
        public User GetUser(Guid userId)
        {
            var candyRepo = new CandyRepository();
            var offerRepo = new OfferRepository();

            using (var db = new SqlConnection(_connectionString))
            {
                var sql        = @"SELECT *
                            FROM [User]
                            WHERE [Id] = @userId";
                var parameters = new { userId };
                var user       = db.QueryFirst <User>(sql, parameters);
                var candies    = candyRepo.FetchUsersCandyList(user);
                user.CandyOwned = candies;
                var favoriteCandyName = candyRepo.FetchFavoriteCandyName(user);
                user.FavoriteTypeOfCandyName = favoriteCandyName;
                user.OffersIn  = new List <UsersOffersIn>();
                user.OffersOut = new List <UsersOffersOut>();
                foreach (OwnedCandy ownedCandy in candies)
                {
                    var offersOut = offerRepo.FetchUsersOffersOut(ownedCandy.UserCandyId);
                    user.OffersOut.AddRange(offersOut);
                    var offersIn = offerRepo.FetchUsersOffersIn(ownedCandy.UserCandyId);
                    user.OffersIn.AddRange(offersIn);
                }
                return(user);
            }
        }
 public IEnumerable <User> GetAllUsers()
 {
     using (var db = new SqlConnection(_connectionString))
     {
         var candyRepo = new CandyRepository();
         var offerRepo = new OfferRepository();
         var users     = db.Query <User>
                         (
             @"SELECT * 
               FROM [User]"
                         ).ToList();
         foreach (User user in users)
         {
             var candies = candyRepo.FetchUsersCandyList(user);
             user.CandyOwned = candies;
             var favoriteCandyName = candyRepo.FetchFavoriteCandyName(user);
             user.FavoriteTypeOfCandyName = favoriteCandyName;
             user.OffersIn  = new List <UsersOffersIn>();
             user.OffersOut = new List <UsersOffersOut>();
             foreach (OwnedCandy ownedCandy in candies)
             {
                 var offersOut = offerRepo.FetchUsersOffersOut(ownedCandy.UserCandyId);
                 user.OffersOut.AddRange(offersOut);
                 var offersIn = offerRepo.FetchUsersOffersIn(ownedCandy.UserCandyId);
                 user.OffersIn.AddRange(offersIn);
             }
         }
         return(users);
     }
 }
        public User GetUserByEmailAndPassword(string email, string password)
        {
            var candyRepo = new CandyRepository();
            var offerRepo = new OfferRepository();

            using (var db = new SqlConnection(_connectionString))
            {
                var sql          = @"SELECT *
                            FROM [User]
                            WHERE ([Password] = @password AND [Email] = @email)";
                var parameters   = new { email, password };
                var userToReturn = db.QueryFirst <User>(sql, parameters);
                var candies      = candyRepo.FetchUsersCandyList(userToReturn);
                userToReturn.CandyOwned = candies;
                var favoriteCandyName = candyRepo.FetchFavoriteCandyName(userToReturn);
                userToReturn.FavoriteTypeOfCandyName = favoriteCandyName;
                userToReturn.OffersIn  = new List <UsersOffersIn>();
                userToReturn.OffersOut = new List <UsersOffersOut>();
                foreach (OwnedCandy ownedCandy in candies)
                {
                    var offersOut = offerRepo.FetchUsersOffersOut(ownedCandy.UserCandyId);
                    userToReturn.OffersOut.AddRange(offersOut);
                    var offersIn = offerRepo.FetchUsersOffersIn(ownedCandy.UserCandyId);
                    userToReturn.OffersIn.AddRange(offersIn);
                }
                return(userToReturn);
            }
        }
        public User GetUser(Guid userId)
        {
            var candyRepo = new CandyRepository();

            using (var db = new SqlConnection(_connectionString))
            {
                var sql        = @"SELECT *
                            FROM [User]
                            WHERE [Id] = @userId";
                var parameters = new { userId };
                var user       = db.QueryFirst <User>(sql, parameters);
                var candies    = candyRepo.FetchUsersCandyList(user);
                user.CandyOwned = candies;
                var favoriteCandyName = candyRepo.FetchFavoriteCandyName(user);
                user.FavoriteTypeOfCandyName = favoriteCandyName;
                return(user);
            }
        }
        public User GetUserByEmailAndPassword(string email, string password)
        {
            var candyRepo = new CandyRepository();

            using (var db = new SqlConnection(_connectionString))
            {
                var sql          = @"SELECT *
                            FROM [User]
                            WHERE ([Password] = @password AND [Email] = @email)";
                var parameters   = new { email, password };
                var userToReturn = db.QueryFirst <User>(sql, parameters);
                var candies      = candyRepo.FetchUsersCandyList(userToReturn);
                userToReturn.CandyOwned = candies;
                var favoriteCandyName = candyRepo.FetchFavoriteCandyName(userToReturn);
                userToReturn.FavoriteTypeOfCandyName = favoriteCandyName;
                return(userToReturn);
            }
        }
 public IEnumerable <User> GetAllUsers()
 {
     using (var db = new SqlConnection(_connectionString))
     {
         var candyRepo = new CandyRepository();
         var users     = db.Query <User>
                         (
             @"SELECT * 
               FROM [User]"
                         ).ToList();
         foreach (User user in users)
         {
             var candies = candyRepo.FetchUsersCandyList(user);
             user.CandyOwned = candies;
             var favoriteCandyName = candyRepo.FetchFavoriteCandyName(user);
             user.FavoriteTypeOfCandyName = favoriteCandyName;
         }
         return(users);
     }
 }
 public bool DonateCandy(Guid userIdWhoIsDonating, Guid userCandyIdToDonate)
 {
     using (var db = new SqlConnection(_connectionString))
     {
         var candyRepo     = new CandyRepository();
         var offerRepo     = new OfferRepository();
         var candyToDonate = candyRepo.GetCandyFromDatabase(userCandyIdToDonate);
         var userToDonate  = WhoToDonateTo(candyToDonate.Id, userIdWhoIsDonating);
         DeleteUserCandyEntry(userCandyIdToDonate);
         offerRepo.RemoveOffer(userCandyIdToDonate);
         var sql  = @"INSERT INTO [UserCandy]
                     ([UserId], [CandyId])
                     VALUES
                         (@userId, @candyId)";
         var sql2 = @"UPDATE [User]
                     SET AmountOfCandyDonated += 1
                     WHERE Id = @UserId";
         db.Execute(sql2, new { UserId = userIdWhoIsDonating });
         return(db.Execute(sql, new { CandyId = candyToDonate.Id, UserId = userToDonate.Id }) == 1);
     }
 }
        public User WhoToDonateTo(Guid candyId, Guid userIdWhoIsDonating)
        {
            using (var db = new SqlConnection(_connectionString))
            {
                var candyRepo      = new CandyRepository();
                var candyTypeId    = candyRepo.GetCandyTypeId(candyId);
                var sql            = @"SELECT * 
                            FROM [User]
                            WHERE ([FavoriteTypeOfCandyId] = @candyTypeId AND [Id] != @userId)";
                var parameters     = new { CandyTypeId = candyTypeId, UserId = userIdWhoIsDonating };
                var userToDonateTo = db.QueryFirstOrDefault <User>(sql, parameters);

                /* If their is no user who has that for their favorite candy
                *   Then it looks for the user with the least amount of candy */
                if (userToDonateTo == null)
                {
                    var users = GetAllUsers().ToList();
                    var user  = users.OrderBy(user => user.CandyOwned.Count()).First <User>();
                    return(user);
                }
                return(userToDonateTo);
            }
        }