public void UpdateUser(User updatedUser)
        {
            using (PhotoShareContext context = new PhotoShareContext())
            {
                User userToUpdate = context.Users
                                    .Include("BornTown")
                                    .Include("CurrentTown")
                                    .SingleOrDefault(u => u.Id == updatedUser.Id);

                if (userToUpdate != null)
                {
                    if (updatedUser.BornTown != null &&
                        (userToUpdate.BornTown == null || updatedUser.BornTown.Id != userToUpdate.BornTown.Id))
                    {
                        userToUpdate.BornTown = context.Towns.Find(updatedUser.BornTown.Id);
                    }

                    if (updatedUser.CurrentTown != null &&
                        (userToUpdate.CurrentTown == null || updatedUser.CurrentTown.Id != userToUpdate.CurrentTown.Id))
                    {
                        userToUpdate.CurrentTown = context.Towns.Find(updatedUser.CurrentTown.Id);
                    }

                    if (updatedUser.Password != userToUpdate.Password)
                    {
                        userToUpdate.Password = updatedUser.Password;
                    }

                    context.Entry(userToUpdate).State = EntityState.Modified;
                    context.SaveChanges();
                }
            }
        }
        public void DeleteUser(User user)
        {
            using (PhotoShareContext context = new PhotoShareContext())
            {
                user.IsDeleted = true;                              // v.1
                context.Users.Attach(user);
                context.Entry(user).State = EntityState.Modified;

                //User userToDelete = context.Users.Find(user.Id);  // v.2
                //userToDelete.IsDeleted = true;

                context.SaveChanges();
            }
        }
Esempio n. 3
0
        public void AddFriend(string username1, string username2)
        {
            var requestingUser = this.GetByUsername(username1);

            var addedFriend = this.GetByUsername(username2);

            context.Entry(requestingUser).Collection(ru => ru.FriendsAdded).Load();

            var friends = requestingUser.FriendsAdded.ToList();

            if (friends.Any(f => f.Friend.Username == username2))
            {
                throw new InvalidOperationException(string.Format(ExeptionMessageHandler.AlreadyFriendsExeption, username2, username1));
            }

            requestingUser.FriendsAdded
            .Add(new Friendship()
            {
                User   = requestingUser,
                Friend = addedFriend
            });

            context.SaveChanges();
        }
Esempio n. 4
0
        public void UpdateUser(User updatedUser)
        {
            // Solution with specifically FK properties in the User model for the virtual Town properties;

            using (PhotoShareContext context = new PhotoShareContext())
            {
                context.Users.Attach(updatedUser);
                context.Entry(updatedUser).State = EntityState.Modified;
                context.SaveChanges();
            }

            // Solution with disabled lazy loading and no FK properties in the User model for the virtual Town properties;

            //using (PhotoShareContext context = new PhotoShareContext())
            //{
            //    User user = context.Users
            //        .Include(u => u.BornTown)
            //        .Include(u => u.CurrentTown)
            //        .SingleOrDefault(u => u.Id == updatedUser.Id);

            //    if (user != null)
            //    {
            //        if (user.Password != updatedUser.Password)
            //        {
            //            user.Password = updatedUser.Password;
            //        }

            //        if (updatedUser.BornTown != null &&
            //           (user.BornTown == null || user.BornTown.Id != updatedUser.BornTown.Id))
            //        {
            //            user.BornTown = context.Towns.Find(updatedUser.BornTown.Id);
            //        }

            //        if (updatedUser.CurrentTown != null &&
            //           (user.CurrentTown == null || user.CurrentTown.Id != updatedUser.CurrentTown.Id))
            //        {
            //            user.CurrentTown = context.Towns.Find(updatedUser.CurrentTown.Id);
            //        }

            //        context.SaveChanges();
            //    }
            //}
        }