/// <summary>
        ///
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public OperationResult <ModelDashboard> Create(ModelDashboard d)
        {
            OperationResult <ModelDashboard> res;

            try
            {
                var existing = DataContext
                               .Dashboards
                               .ForActiveOrg()
                               .FirstOrDefault(x =>
                                               x.Title == d.Title &&
                                               d.FolderId == x.FolderId);

                if (null != existing)
                {
                    if (!ExpandoHelper.GetProperty <bool>(d, "Overwrite"))
                    {
                        return(OperationResult <ModelDashboard> .Create(ErrorCode.BadCreateDashboardDuplicate));
                    }

                    DataContext.Dashboards.Remove(existing);
                }

                var entity = d
                             .ToEntity(DataContext)
                             .IncludeActiveOrgId(DataContext);

                entity.Versions.Add(new DashboardVersion(d));

                DataContext.Add(entity);

                DataContext.SaveChanges();

                var model = entity
                            .UpdateId(d)
                            .ToModel()
                            .AddTime(DataContext.Entry(entity))
                            .AddVersion(DataContext.Entry(entity));

                res = OperationResult <ModelDashboard> .Create(model);
            }
            catch (Exception e)
            {
                res = OperationResult <ModelDashboard> .Create(ErrorCode.BadCreateDashboard, e);
            }

            return(res);
        }
            /// <summary>
            ///
            /// </summary>
            /// <returns></returns>
            public ModelDashboard ToModel(int userId)
            {
                var model = new ModelDashboard()
                {
                    Id    = Dashboard.Id.HasValue ? Dashboard.Id.Value : 0,
                    Title = Dashboard.Title,
                    Uid   = Dashboard.Uid,
                    Tags  = Dashboard.Tags,
                    Data  = Dashboard.Data?.ToString(),

                    FolderId = (0 == FolderId) ? ( int? )null : FolderId
                };

                model.Bag.Overwrite = Overwrite;
                model.Bag.Message   = Message;
                model.Bag.Version   = Dashboard.Version;
                model.Bag.UserId    = userId;

                return(model);
            }
        /// <summary>
        ///
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        private object ToDashboardReply(ModelDashboard d)
        {
            return(new
            {
                d.Id,
                d.Uid,
                d.Title,
                d.Url,
                Type = "dash-db",
                d.Tags,
                IsStarred = d.Stars.Contains(ActualUser.Id),

                d.FolderId,
                d.Bag.FolderUid,
                d.Bag.FolderTitle,
                d.Bag.FolderUrl,

                d.Bag.Version
            });
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public OperationResult <ModelDashboard> Update(ModelDashboard d)
        {
            OperationResult <ModelDashboard> res;

            try
            {
                var entity = DataContext
                             .Dashboards
                             .ForActiveOrg()
                             .Include(x => x.Tags)
                             .Include(x => x.Alerts)
                             .FirstOrDefault(x => x.Uid == d.Uid);

                if (null == entity)
                {
                    return(OperationResult <ModelDashboard> .Create(ErrorCode.BadGetDashboard));
                }

                var existing = DataContext
                               .Dashboards
                               .ForActiveOrg()
                               .FirstOrDefault(x =>
                                               x.Title == d.Title &&
                                               d.FolderId == x.FolderId &&
                                               x.Uid != d.Uid);

                if (null != existing)
                {
                    if (!ExpandoHelper.GetProperty <bool>(d, "Overwrite"))
                    {
                        return(OperationResult <ModelDashboard> .Create(ErrorCode.BadCreateDashboardDuplicate));
                    }

                    DataContext.Dashboards.Remove(existing);
                }

                var currentVersion = ( int )DataContext
                                     .Entry(entity)
                                     .Property(DataContext.COL_VERSION)
                                     .CurrentValue;

                DataContext
                .Entry(entity)
                .State = EntityState.Modified;

                if (currentVersion > ExpandoHelper.GetProperty <int>(d, "Version"))
                {
                    return(OperationResult <ModelDashboard> .Create(ErrorCode.BadUpdateDashboardVersionMismatch));
                }

                entity.Update(DataContext, d);

                DataContext.SaveChanges();

                var model = entity
                            .ToModel()
                            .AddTime(DataContext.Entry(entity))
                            .AddVersion(DataContext.Entry(entity));

                res = OperationResult <ModelDashboard> .Create(model);
            }
            catch (AlertValidationException e)
            {
                res = OperationResult <ModelDashboard> .Create(ErrorCode.BadUpdateDashboardValidation, e);
            }
            catch (Exception e)
            {
                res = OperationResult <ModelDashboard> .Create(ErrorCode.BadUpdateDashboard, e);
            }

            return(res);
        }