/// <summary>
        /// Gets a dataset by id and checks the access rights
        /// </summary>
        /// <param name="id">dataset id</param>
        /// <param name="userId">userid</param>
        /// <returns>Dataset</returns>
        public DatasetDto GetById(int id, int?userId)
        {
            Dataset dataset = _datasetRepository.GetById(id);

            if (dataset.UserId == userId || dataset.Flags.HasFlag(DataSetFlags.Public))
            {
                return(DatasetMapper.ToDto(dataset));
            }
            else
            {
                throw new Exception("Not Authorized");
            }
        }
        /// <summary>
        /// Gets all editable datasets for the user
        /// </summary>
        /// <param name="userId">userid</param>
        /// <returns>list of datasets</returns>
        public List <DatasetDto> GetAllEditable(int userId)
        {
            List <DatasetDto> dtoList = new List <DatasetDto>();

            foreach (Dataset ds in _datasetRepository.GetAllEditable(userId))
            {
                try
                {
                    dtoList.Add(DatasetMapper.ToDto(ds));
                }
                catch { }// ignore error so other datasets can be loaded correctly
            }
            return(dtoList);
        }
 private Dataset xorDataset = null; //cache
 private DatasetDto GetXorDatasetDto(DataSetFlags flags)
 {
     if (xorDataset == null)
     {
         xorDataset = new Dataset()
         {
             Data   = File.ReadAllText("Resources/xor_ok.json"),
             Flags  = flags,
             UserId = 0,
             Name   = "XOR"
         };
     }
     return(DatasetMapper.ToDto(xorDataset));
 }
        /// <summary>
        ///  Checks the right for the update process and updates the dataset and
        /// </summary>
        /// <param name="datasetDto">dataset</param>
        /// <param name="userId">userid</param>
        /// <returns>the updated dataset</returns>
        public DatasetDto Update(DatasetDto datasetDto, int userId)
        {
            ValidateDataset(datasetDto);

            Dataset result = null;

            if (datasetDto.Id > 0)
            {
                Dataset ds = _datasetRepository.GetById(datasetDto.Id);
                ds.Data     = datasetDto.Json;
                ds.Flags    = datasetDto.Flags;
                ds.Name     = datasetDto.Name;
                ds.Modified = DateTime.Now;
                if (ds.UserId != userId)
                {
                    throw new Exception("No permission to update this dataset.");
                }

                _datasetRepository.DataContext.Commit();
                result = ds;
            }
            else
            {
                Dataset ds = new Dataset()
                {
                    Id       = 0,
                    Name     = datasetDto.Name,
                    Flags    = datasetDto.Flags,
                    Data     = datasetDto.Json,
                    UserId   = userId,
                    Created  = DateTime.Now,
                    Modified = DateTime.Now
                };
                _datasetRepository.Add(ds);
                _datasetRepository.DataContext.Commit();
                result = ds;
            }

            return(DatasetMapper.ToDto(result));
        }
        /// <summary>
        /// Gets a dataset by name
        /// </summary>
        /// <param name="name">name of the dataset</param>
        /// <returns>DatasetDto</returns>
        public DatasetDto GetByName(string name)
        {
            Dataset ds = _datasetRepository.GetByName(name);

            return(DatasetMapper.ToDto(ds));
        }