Example #1
0
        public virtual OperationResult Save <T>(T obj) where T : Entity
        {
            var result = new OperationResult(Validate(obj));

            using (var db = MefBootstrap.Resolve <ApplicationDbContext>())
            {
                db.Configuration.ValidateOnSaveEnabled = false;

                var dbEntity = db.Set <T>().Find(obj.ID);

                if (dbEntity == null)
                {
                    db.Set <T>().Add(obj);
                }
                else
                {
                    db.Entry(dbEntity).CurrentValues.SetValues(obj);
                    dbEntity.UpdatedDate = DateTime.UtcNow;
                }

                result.AffectedResults = db.SaveChanges();
            }

            return(result);
        }
Example #2
0
        public IEnumerable <TaskItem> GetTaskItems(Board board)
        {
            using (var db = MefBootstrap.Resolve <ApplicationDbContext>())
            {
                var taskItems = db.Set <Board>()
                                .Include(o => o.TaskItems)
                                .Single(o => o.ID == board.ID)
                                .TaskItems.Where(o => !o.TaskItemParentID.HasValue).ToList();

                return(taskItems);
            }
        }
Example #3
0
        public IEnumerable <Board> GetUserBoards(int?id = null)
        {
            var userID = id ?? CurrentUserID;

            using (var db = MefBootstrap.Resolve <ApplicationDbContext>())
            {
                return(db.Boards
                       .Where(o => o.OwnerID == userID && !o.Deleted)
                       .OrderBy(o => o.Name)
                       .ToList());
            }
        }
Example #4
0
        public virtual bool Remove <T>(T obj) where T : Entity
        {
            using (var db = MefBootstrap.Resolve <ApplicationDbContext>())
            {
                db.Set <T>().Attach(obj);
                obj.Deleted = true;

                try
                {
                    return(db.SaveChanges() > 0);
                }
                catch
                {
                    return(false);
                }
            }
        }
Example #5
0
        public App()
        {
            // TODO: Remove
            #region Automatic login - Temp - Remove

            MefBootstrap.Resolve <AuthenticationService>().Authenticate("brunolm", "password");

            Application.Current.MainWindow = ControlFactory.CreateWindow <MainViewModel>();
            Application.Current.MainWindow.Show();

            return;

            #endregion

            MainWindow = ControlFactory.CreateWindow <LoginViewModel>();
            MainWindow.Show();
        }
Example #6
0
        public static T Clone <T>(T source) where T : Entity
        {
            if (source == null)
            {
                return(null);
            }

            if (source == default(T))
            {
                return(default(T));
            }

            using (var db = MefBootstrap.Resolve <ApplicationDbContext>())
            {
                db.Set <T>().Attach(source);
                return(db.Entry(source).OriginalValues.ToObject() as T);
            }
        }
Example #7
0
        // TODO: Remove
        public void CreateDummies()
        {
            var user = (Thread.CurrentPrincipal.Identity as CustomIdentity).CurrentUser;

            using (var db = MefBootstrap.Resolve <ApplicationDbContext>())
            {
                db.Users.Attach(user);

                if (db.Boards.Count() == 0)
                {
                    var boards = new List <Board>
                    {
                        new Board {
                            Name = "C#", Owner = user
                        },
                        new Board {
                            Name = "WPF", Owner = user
                        },
                        new Board {
                            Name = "People Skills", Owner = user
                        },
                        new Board {
                            Name = "Some other board", Owner = user
                        },
                        new Board {
                            Name = "I can haz many boards", Owner = user
                        },
                        new Board {
                            Name = "Look I'm a potato", Owner = user
                        },
                    };

                    db.Boards.AddRange(boards);

                    db.SaveChanges();
                }
            }
        }
Example #8
0
        public void Authenticate(string name, string password)
        {
            Domain.User user         = null;
            var         hashPassword = Hash(password, name);

            using (var db = MefBootstrap.Resolve <ApplicationDbContext>())
            {
                // TODO: Remove
                #region Temp Create User - Remove

                if (db.Users.Count() == 0)
                {
                    db.Users.Add(new Domain.User
                    {
                        Name     = "BrunoLM",
                        Email    = "*****@*****.**",
                        Password = Hash("password", "BrunoLM")
                    });
                    db.SaveChanges();
                }

                #endregion

                user = db.Users
                       .FirstOrDefault(o => o.Name.Equals(name, StringComparison.OrdinalIgnoreCase) &&
                                       o.Password.Equals(hashPassword, StringComparison.OrdinalIgnoreCase));
            }

            if (user == null)
            {
                throw new UnauthorizedAccessException("Invalid username/password");
            }

            AppDomain.CurrentDomain.SetThreadPrincipal(new CustomPrincipal {
                Identity = new CustomIdentity(user)
            });
        }