Пример #1
0
        /// <summary>
        /// Update the party details
        /// </summary>
        /// <param name="updatedParty"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public PartyTable UpdateParty(PartyTable updatedParty)
        {
            using (var context = new EntitiesChooseEm())
            { //check for party being null?
                PartyTable party = context.PartyTables.FirstOrDefault(p => p.ID == updatedParty.ID);

                party.startDate         = updatedParty.startDate;
                party.endDate           = updatedParty.endDate;
                party.locationLatitude  = updatedParty.locationLatitude;
                party.locationLongitude = updatedParty.locationLongitude;
                party.privacy           = updatedParty.privacy;
                party.Name            = updatedParty.Name;
                party.RowVersion      = updatedParty.RowVersion;
                party.AvailablePlaces = updatedParty.AvailablePlaces;


                context.PartyTables.Attach(party);
                context.Entry(party).State = System.Data.Entity.EntityState.Modified;

                var num = context.SaveChanges();

                if (num != 1)
                {
                    throw new Exception("Concurrency Error");
                }
            }
            return(updatedParty);
        }
        public UsersAtParty CreateUserAtParty(UsersAtParty userAtParty, byte[] RowVersion)
        {
            PartyRepository pr = new PartyRepository();

            using (var db = new EntitiesChooseEm())
            {
                using (var dbContextTransaction = db.Database.BeginTransaction())
                {
                    var selectedParty = db.PartyTables.First(p => p.ID == userAtParty.partyID);
                    if (selectedParty.AvailablePlaces > 0)
                    {
                        db.UsersAtParties.Add(userAtParty);
                        db.SaveChanges();
                        selectedParty.AvailablePlaces -= 1;
                        selectedParty.RowVersion       = RowVersion;
                    }
                    else
                    {
                        throw new Exception("No places left, refresh the page");
                    }

                    try { pr.UpdateParty(selectedParty);
                          dbContextTransaction.Commit(); }
                    catch (Exception ex)
                    {
                        dbContextTransaction.Rollback();
                        throw ex;
                    }
                }
            }
            return(userAtParty);
        }
Пример #3
0
 public PartyTable CreateParty(PartyTable party)
 {
     using (var db = new EntitiesChooseEm())
     {
         db.PartyTables.Add(party);
         db.SaveChanges();
     }
     return(party);
 }
 public void RevertRating(SongRatingFromPartyTable rating, int previousType)
 {
     using (var db = new EntitiesChooseEm())
     {
         var aux = db.SongRatingFromPartyTables.First(p => p.userID == rating.userID && p.songURL == rating.songURL && p.partyID == rating.partyID);
         aux.voteType = previousType;
         db.SaveChanges();
     }
 }
Пример #5
0
 public bool AddSongInPartyPlaylist(SongsForPartyTable playlist)
 {
     using (var db = new EntitiesChooseEm())
     {
         db.SongsForPartyTables.Add(playlist);
         db.SaveChanges();
     }
     return(true);
 }
Пример #6
0
 public SongsForPartyTable UpdateSongRating(SongsForPartyTable songWithUpdatedRating)
 {
     using (var context = new EntitiesChooseEm())
     {
         SongsForPartyTable song = context.SongsForPartyTables.FirstOrDefault(p => p.songURL == songWithUpdatedRating.songURL && p.partyID == songWithUpdatedRating.partyID);
         song.rating = songWithUpdatedRating.rating;
         context.SaveChanges();
     }
     return(songWithUpdatedRating);
 }
 public UsersAtParty UpdateUserAtParty(UsersAtParty updatedUsers)
 {
     using (var context = new EntitiesChooseEm())
     { //check for user being null?
         UsersAtParty user = context.UsersAtParties.FirstOrDefault(u => u.userID == updatedUsers.userID);
         user.status          = updatedUsers.status;
         user.adminPrivileges = updatedUsers.adminPrivileges;
         context.SaveChanges();
     }
     return(updatedUsers);
 }
        /// <summary>
        /// Rollback method
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="partyId"></param>
        /// <returns></returns>
        public bool RollBackUserAtParty(long userId, long partyId)
        {
            using (var context = new EntitiesChooseEm())
            {
                context.UsersAtParties.Remove(context.UsersAtParties.FirstOrDefault
                                                  (u => u.userID == userId && u.partyID == partyId));

                context.SaveChanges();
            }
            return(true);
        }
Пример #9
0
        public bool RemoveSongFromPartyPlaylist(long partyID, string songURL)
        {
            using (var db = new EntitiesChooseEm())
            {
                db.SongRatingFromPartyTables.RemoveRange(db.SongRatingFromPartyTables.Where(p => ((p.partyID == partyID) && (p.songURL == songURL))));

                db.SongsForPartyTables.RemoveRange(db.SongsForPartyTables.Where(p => ((p.partyID == partyID) && (p.songURL == songURL))));
                db.SaveChanges();
            }
            return(true);
        }
 public SongRatingFromPartyTable VoteSong(SongRatingFromPartyTable songWithUpdatedRating)
 {
     using (var db = new EntitiesChooseEm())
     {
         SongRatingFromPartyTable ratingfromDB = db.SongRatingFromPartyTables.FirstOrDefault(r => r.partyID == songWithUpdatedRating.partyID && r.userID == songWithUpdatedRating.userID && r.songURL == songWithUpdatedRating.songURL);
         ratingfromDB.voteType = songWithUpdatedRating.voteType;
         songWithUpdatedRating = ratingfromDB;
         db.SaveChanges();
     }
     return(songWithUpdatedRating);
 }
        public bool RemoveUser(long ID)
        {
            UserTable user = null;

            using (var db = new EntitiesChooseEm())
            {
                user = db.UserTables.FirstOrDefault(p => p.ID == ID);
                db.UserTables.Remove(user);
                db.SaveChanges();
            }
            return(true);
        }
        public static long AddLoginAccount(UserLoginTable userLogin)
        {
            var hash = SecurePasswordHasher.Hash(userLogin.password);

            userLogin.password = hash;
            System.Diagnostics.Debug.WriteLine(" addloginaccount: " + hash + " - " + userLogin.password);
            using (var db = new EntitiesChooseEm())
            {
                db.UserLoginTables.Add(userLogin);
                db.SaveChanges();
            }
            return(userLogin.userID);
        }
 public UserTable UpdateUser(UserTable updatedUser)
 {
     using (var context = new EntitiesChooseEm())
     {
         UserTable user = context.UserTables.First(u => u.ID == updatedUser.ID);
         user.firstName      = updatedUser.firstName;
         user.lastName       = updatedUser.lastName;
         user.accountType    = updatedUser.accountType;
         user.zip            = updatedUser.zip;
         user.currentPartyID = updatedUser.currentPartyID;
         context.SaveChanges();
     }
     return(updatedUser);
 }
        public bool RemoveUserAtParty(long userId, long partyId)
        {
            using (var context = new EntitiesChooseEm())
            { // check for user being null?
                UsersAtParty user = context.UsersAtParties.FirstOrDefault(u => u.userID == userId && u.partyID == partyId);
                context.UsersAtParties.Remove(user);

                //change back the number of available spaces
                var aux = context.PartyTables.First(p => p.ID == partyId);
                aux.AvailablePlaces += 1;

                context.SaveChanges();
            }
            return(true);
        }
Пример #15
0
        /// <summary>
        /// Remove a friendship, 2 user ids required
        /// </summary>
        /// <param name="firstUserId"></param>
        /// <param name="secondUserId"></param>
        /// <returns></returns>
        public bool RemoveFriendLink(long firstUserId, long secondUserId)
        {
            using (var context = new EntitiesChooseEm())
            {
                var friendLink = context.FriendTables.FirstOrDefault(u => u.idOne == firstUserId && u.idTwo == secondUserId);

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

                context.FriendTables.Remove(friendLink);
                context.SaveChanges();
            }
            return(true);
        }
        public void SetRating(int total, string uri, long partyID, byte[] atmRowVersion)
        {
            using (var db = new EntitiesChooseEm()) {
                var aux = db.SongsForPartyTables.First(p => p.songURL == uri && p.partyID == partyID);
                aux.rating     = total;
                aux.RowVersion = atmRowVersion;
                db.SongsForPartyTables.Attach(aux);
                db.Entry(aux).State = System.Data.Entity.EntityState.Modified;

                var num = db.SaveChanges();

                if (num != 1)
                {
                    throw new Exception("Concurrency Error");
                }
            }
        }
Пример #17
0
        /// <summary>
        /// Delete a party from the database
        /// </summary>
        /// <param name="partyId"></param>
        /// <returns></returns>
        public bool RemoveParty(long partyId)
        {
            PartyTable party;

            using (var db = new EntitiesChooseEm())
            {
                party = db.PartyTables.FirstOrDefault(p => p.ID == partyId);
                if (party == null)
                {
                    return(false);
                }

                db.PartyTables.Remove(party);
                db.SaveChanges();
            }
            return(true);
        }
        public bool CreateUser(UserTable user)
        {
            using (var db = new EntitiesChooseEm())
            {
                //try
                //{                 //var user = new UserTable { firstName = firstName, lastName = lastName, accountType = accountType, zip = zip, currentPartyID = currentPartyID };
                db.UserTables.Add(user);
                db.SaveChanges();
                //}
                //catch(Exception e)
                //{

                //    System.Diagnostics.Debug.WriteLine(e.ToString());

                //}
            }
            return(true);
        }
        public long Createuser(UserTable user)
        {
            UserTable aux = new UserTable();

            using (var db = new EntitiesChooseEm())
            {
                try
                {                 //var user = new UserTable { firstName = firstName, lastName = lastName, accountType = accountType, zip = zip, currentPartyID = currentPartyID };
                    db.UserTables.Add(user);
                    db.SaveChanges();
                    aux = db.UserTables.FirstOrDefault(row => row.firstName == user.firstName && row.lastName == user.lastName);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.ToString());
                }
            }
            return(aux.ID);
        }
Пример #20
0
        /// <summary>
        /// Register a friendship in db if
        /// </summary>
        /// <param name="firstUserId"></param>
        /// <param name="secondUserId"></param>
        /// <returns></returns>
        public bool CreateFriendsLink(long firstUserId, long secondUserId)
        {
            if (AreFriends(firstUserId, secondUserId))
            {
                return(false);
            }

            using (var db = new EntitiesChooseEm())
            {
                var friendsLink = new FriendTable
                {
                    idOne = firstUserId,
                    idTwo = secondUserId
                };
                db.FriendTables.Add(friendsLink);
                db.SaveChanges();
            }
            return(true);
        }
        public bool AddSongRating(SongRatingFromPartyTable rating)
        {
            int previousType;

            using (var db = new EntitiesChooseEm())
            {
                if (!db.SongRatingFromPartyTables.Any(p => p.userID == rating.userID && p.songURL == rating.songURL && p.partyID == rating.partyID))
                {
                    db.SongRatingFromPartyTables.Add(rating);
                    previousType = 0;
                    db.SaveChanges();
                }
                else
                {
                    var aux = db.SongRatingFromPartyTables.FirstOrDefault(p => p.userID == rating.userID && p.songURL == rating.songURL && p.partyID == rating.partyID);
                    previousType = aux.voteType;


                    if (db.UsersAtParties.First(p => p.partyID == rating.partyID && p.userID == rating.userID).adminPrivileges)
                    {
                        aux.voteType += rating.voteType;
                    }
                    else
                    {
                        //Checks for vote change
                        if (aux.voteType == 1 && rating.voteType == 1)
                        {
                            return(true);
                        }
                        else if (aux.voteType == 1 && rating.voteType == -1)
                        {
                            aux.voteType = 0;
                        }
                        else if (aux.voteType == -1 && rating.voteType == -1)
                        {
                            return(true);
                        }
                        else if (aux.voteType == -1 && rating.voteType == 1)
                        {
                            aux.voteType = 0;
                        }
                        else
                        {
                            aux.voteType = rating.voteType;
                        }
                    }


                    db.SaveChanges();
                }
                var result = db.SongRatingFromPartyTables.GroupBy(p => p.songURL == rating.songURL && p.partyID == rating.partyID)
                             .Select(g => new { membername = g.Key, total = g.Sum(i => i.voteType) });

                var atmRowVersion = db.SongsForPartyTables.First(p => p.songURL == rating.songURL && p.partyID == rating.partyID).RowVersion;

                foreach (var group in result)
                {
                    if (group.membername)
                    {
                        try {
                            SetRating(group.total, rating.songURL, rating.partyID, atmRowVersion);
                        }

                        catch (Exception ex) {
                            RevertRating(rating, previousType);
                            throw ex;
                        }
                    }
                }
            }
            return(true);
        }