public SaveResponse Update(IUnitOfWork uow, UserPermissionUpdateRequest request)
 {
     return(new MyRepository().Update(uow, request));
 }
 public SaveResponse Update(IUnitOfWork uow, UserPermissionUpdateRequest request)
 {
     return new MyRepository().Update(uow, request);
 }
        public SaveResponse Update(IUnitOfWork uow, UserPermissionUpdateRequest request)
        {
            Check.NotNull(request, "request");
            Check.NotNull(request.UserID, "userID");
            Check.NotNull(request.Permissions, "permissions");

            var userID  = request.UserID.Value;
            var oldList = new Dictionary <string, bool>(StringComparer.OrdinalIgnoreCase);

            foreach (var p in GetExisting(uow.Connection, userID, request.Module, request.Submodule))
            {
                oldList[p.PermissionKey] = p.Granted.Value;
            }

            var newList = new Dictionary <string, bool>(StringComparer.OrdinalIgnoreCase);

            foreach (var p in request.Permissions)
            {
                newList[p.PermissionKey] = p.Granted ?? false;
            }

            if (oldList.Count == newList.Count &&
                oldList.All(x => newList.ContainsKey(x.Key) && newList[x.Key] == x.Value))
            {
                return(new SaveResponse());
            }

            foreach (var k in oldList.Keys)
            {
                if (newList.ContainsKey(k))
                {
                    continue;
                }

                new SqlDelete(fld.TableName)
                .Where(
                    new Criteria(fld.UserId) == userID &
                    new Criteria(fld.PermissionKey) == k)
                .Execute(uow.Connection);
            }

            foreach (var k in newList.Keys)
            {
                if (!oldList.ContainsKey(k))
                {
                    uow.Connection.Insert(new MyRow
                    {
                        UserId        = userID,
                        PermissionKey = k,
                        Granted       = newList[k]
                    });
                }
                else if (oldList[k] != newList[k])
                {
                    new SqlUpdate(fld.TableName)
                    .Where(
                        fld.UserId == userID &
                        fld.PermissionKey == k)
                    .Set(fld.Granted, newList[k])
                    .Execute(uow.Connection);
                }
            }

            BatchGenerationUpdater.OnCommit(uow, fld.GenerationKey);

            return(new SaveResponse());
        }