public void UpdateDatasetInclCheckRights()
        {
            (DataContext dbContext, UserService userService, UserRepository userRepository, DatasetRepository datasetRepository, DatasetService datasetService)
                = InitDb("UpdateDataset");
            User adminUser   = CreateInitialuser(dbContext, "*****@*****.**");
            User initialUser = CreateInitialuser(dbContext, "*****@*****.**");

            Dataset dataset = CreateInitialXorDataset(dbContext, adminUser, DataSetFlags.Public);

            DatasetDto datasetDto = new DatasetDto();

            datasetDto.Name  = "Dataset name changed";
            datasetDto.Flags = DataSetFlags.Private;
            datasetDto.Json  = dataset.Data;
            datasetDto.Id    = dataset.Id;

            datasetService.Update(datasetDto, adminUser.Id);

            Dataset    updatedDataset    = dbContext.Datasets.First(x => x.Id == dataset.Id);
            DatasetDto updatedDatasetDto = new DatasetDto();

            updatedDatasetDto.Json = updatedDataset.Data;

            Assert.AreEqual(datasetDto.Name, updatedDataset.Name);
            Assert.AreEqual(datasetDto.Flags, updatedDataset.Flags);
            Assert.ThrowsException <Exception>(() => datasetService.Update(datasetDto, initialUser.Id));
        }
Example #2
0
 public DatasetDto Post(DatasetDto datasetDto)
 {
     try
     {
         return(_datasetService.Update(datasetDto, UserId.Value));
     }
     finally
     {
         DbContext.Dispose();
     }
 }
Example #3
0
        public async void GetDatasetByIdAsync_ShouldReturnDataset_IfExists()
        {
            var datasetId = 10;

            _ = await ClearTestDatabase();

            var datasets = CreateTestDatasetList(datasetId);

            var datasetService = DatasetServiceMockFactory.GetDatasetService(_serviceProvider, datasets);

            DatasetDto result = await datasetService.GetByIdAsync(datasetId);

            Assert.NotNull(result);
        }
Example #4
0
 /// <summary>
 /// Maps a Dataset to a DatasetDto
 /// </summary>
 /// <param name="ds">Dataset</param>
 /// <returns>DatasetDto</returns>
 public static DatasetDto ToDto(Dataset ds)
 {
     if (ds != null)
     {
         DatasetDto dto = new DatasetDto();
         dto.Name   = ds.Name;
         dto.Flags  = ds.Flags;
         dto.Id     = ds.Id;
         dto.Json   = ds.Data;
         dto.UserId = ds.UserId;
         dto.User   = ds.User;
         return(dto);
     }
     return(null);
 }
Example #5
0
 protected void PerformUsualTestForPostedDatasets(DatasetDto datasetDto)
 {
     if (String.IsNullOrWhiteSpace(datasetDto.Name))
     {
         throw new ArgumentException($"Field Dataset.Name is required. Current value: {datasetDto.Name}");
     }
     if (String.IsNullOrWhiteSpace(datasetDto.Classification))
     {
         throw new ArgumentException($"Field Dataset.Classification is required. Current value: {datasetDto.Classification}");
     }
     if (String.IsNullOrWhiteSpace(datasetDto.Location))
     {
         throw new ArgumentException($"Field Dataset.Location is required. Current value: {datasetDto.Location}");
     }
 }
Example #6
0
        public async Task <IActionResult> Post(int?id, [FromBody] DatasetDto dataset)
        {
            if (id == null || id <= 0)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                Project projectToUpdate = await context.Projects
                                          .SingleOrDefaultAsync(p => p.ProjectId == id);

                if (projectToUpdate == null)
                {
                    return(NotFound());
                }

                Dataset mappedDataset   = mapper.Map <Dataset>(dataset);
                Dataset datasetToUpdate = await context.Datasets
                                          .SingleOrDefaultAsync(d => d.ProjectId == id);

                if (datasetToUpdate == null)
                {
                    datasetToUpdate           = new Dataset();
                    datasetToUpdate.ProjectId = id ?? default(int);
                    datasetToUpdate.Data      = mappedDataset.Data;
                    context.Datasets.Add(datasetToUpdate);
                }
                else
                {
                    datasetToUpdate.Data = mappedDataset.Data;
                }

                projectToUpdate.DateUpdated = DateTime.Now;

                await context.SaveChangesAsync();

                return(Ok(mapper.Map <DatasetDto>(datasetToUpdate)));
            }

            return(BadRequest(ModelState));
        }
        /// <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>
        /// Validates the dataset
        /// </summary>
        /// <param name="dataset">dataset</param>
        public void ValidateDataset(DatasetDto dataset)
        {
            DatasetEnvelopeDto datasetEnvelope = dataset.Dataset;

            if (string.IsNullOrEmpty(dataset.Name))
            {
                throw new Exception("Please enter a name for the provided dataset.");
            }
            else
            {
                if (_datasetRepository.GetByName(dataset.Name) != null && dataset.Id < 1)
                {
                    throw new Exception("A dataset with this name already exists.");
                }
            }

            if (datasetEnvelope.Items.Length > 0)
            {
                int inputLength  = datasetEnvelope.Items.FirstOrDefault()?.Data?.Length ?? throw new Exception("First item entry has no 'Data' attribute");
                int outputLength = datasetEnvelope.Items.FirstOrDefault()?.Labels?.Length ?? throw new Exception("First item entry has no 'Label' attribute");
                foreach (DataItemDto item in datasetEnvelope.Items)
                {
                    if (item.Labels == null || item.Data == null)
                    {
                        throw new Exception($"Item entry {(Array.IndexOf(datasetEnvelope.Items, item) + 1)} must have a label and a data attribute.");
                    }

                    if (item.Data.Length < 1)
                    {
                        throw new Exception($"Item {(Array.IndexOf(datasetEnvelope.Items, item) + 1)} 'Data' attribute must have length > 0");
                    }

                    if (item.Labels.Length < 1)
                    {
                        throw new Exception($"Item {(Array.IndexOf(datasetEnvelope.Items, item) + 1)} 'Labels' attribute must have length > 0");
                    }

                    if (item.Data.Length != inputLength)
                    {
                        throw new Exception($"Dataset is not consistent. Item number {(Array.IndexOf(datasetEnvelope.Items, item) + 1)} " +
                                            $" 'Data' attribute has a different length ({item.Data.Length}) than the first Item ({inputLength})");
                    }

                    if (item.Labels.Length != outputLength)
                    {
                        throw new Exception($"Dataset is not consistent. Item number {(Array.IndexOf(datasetEnvelope.Items, item) + 1)} " +
                                            $" 'Label' attribute has a different length ({item.Labels.Length}) than the first Item ({outputLength})");
                    }

                    if (datasetEnvelope.ProblemType.ToLower() == "classification")
                    {
                        bool foundOne = false;
                        foreach (int label in item.Labels)
                        {
                            if (label == 1d && foundOne)
                            {
                                throw new Exception($"Item number {(Array.IndexOf(datasetEnvelope.Items, item) + 1)} is not one-hot encoded. Two '1' found.");
                            }
                            if (label == 1d)
                            {
                                foundOne = true;
                            }
                            if (label != 0d && label != 1d)
                            {
                                throw new Exception($"Item number {(Array.IndexOf(datasetEnvelope.Items, item) + 1)} is not one-hot encoded. Only '0' and '1's are allowed.");
                            }
                        }
                        if (!foundOne)
                        {
                            throw new Exception($"Item number {(Array.IndexOf(datasetEnvelope.Items, item) + 1)} is not one-hot encoded. No '1' was found.");
                        }
                    }
                }
            }
            else
            {
                throw new Exception("No Items found!");
            }

            if (datasetEnvelope.OutputDescription != null && datasetEnvelope.OutputDescription.Length > 0)
            {
                if (datasetEnvelope.Items.First().Labels.Length != datasetEnvelope.OutputDescription.Length)
                {
                    throw new Exception("'OutputDescription' must have the same number of descriptions like there are Outputneurons / Label entries.");
                }
                if (datasetEnvelope.ProblemType.ToLower() == "regression")
                {
                    throw new Exception("'OutputDescription' only possible with problem type 'classification'.");
                }
            }

            if (datasetEnvelope.Layers != null && datasetEnvelope.Layers.Length > 0)
            {
                if (datasetEnvelope.Layers.Length < 2)
                {
                    throw new Exception("If you want to provide layers, you must provide at least two layers (input and outputlayer)");
                }

                LayerDto inputLayer = datasetEnvelope.Layers.First();
                if (!string.IsNullOrEmpty(inputLayer.Activation) || inputLayer.Bias)
                {
                    throw new Exception("Inputlayer can not have an activation Function or a Bias!");
                }

                if (datasetEnvelope.Items.First().Data.Length != datasetEnvelope.Layers.First().Neurons)
                {
                    throw new Exception($"Inputlayer must have same number of neurons like the length of the data input ({datasetEnvelope.Items.First().Data.Length})");
                }
                if (datasetEnvelope.Items.First().Labels.Length != datasetEnvelope.Layers.Last().Neurons)
                {
                    throw new Exception($"Outputlayer must have same number of neurons like the length of the label ({datasetEnvelope.Items.First().Labels.Length})");
                }

                for (int i = 0; i < datasetEnvelope.Layers.Length; i++)
                {
                    LayerDto layer = datasetEnvelope.Layers[i];
                    if (layer.Neurons < 1)
                    {
                        throw new Exception($"Layer {i + 1} must have a positive number of neurons");
                    }

                    if (!string.IsNullOrEmpty(layer.Activation) && i != 0)
                    {
                        if (!new List <string>()
                        {
                            "relu", "sigmoid", "tanh", "softmax", "linear"
                        }.Contains(layer.Activation.ToLower()))
                        {
                            throw new Exception($"Layer {i + 1} activation function must have value 'linear', 'relu', 'sigmoid', 'softmax' or 'tanh'");
                        }
                    }
                }
            }

            if (datasetEnvelope.Matrix != null && datasetEnvelope.Matrix.Length > 0)
            {
                if (datasetEnvelope.Matrix.Length != 2)
                {
                    throw new Exception("Please enter two numbers [rows, columns] for the Matrix Property");
                }
                else
                {
                    int length = datasetEnvelope.Matrix[0] * datasetEnvelope.Matrix[1];
                    if (length != datasetEnvelope.Items.First().Data.Length)
                    {
                        throw new Exception($"The vector of the matrix must have the same length like the data input vector which is {datasetEnvelope.Items.First().Data.Length}.");
                    }
                }
            }

            if (datasetEnvelope.TrainingSettings != null)
            {
                if (!string.IsNullOrEmpty(datasetEnvelope.TrainingSettings.Optimizer))
                {
                    if (!new List <string>()
                    {
                        "rmsprop", "adam", "sgd", "sgdm"
                    }.Contains(datasetEnvelope.TrainingSettings.Optimizer.ToLower()))
                    {
                        throw new Exception("TrainingSetting Optimizer must have value 'rmsprop', 'adam', 'sgd' or 'sgdm'");
                    }
                    if (!new List <string>()
                    {
                        "rmsprop", "sgdm"
                    }.Contains(datasetEnvelope.TrainingSettings.Optimizer.ToLower()) && datasetEnvelope.TrainingSettings.Momentum.HasValue)
                    {
                        throw new Exception("Momentum only possible with Optimizer 'rmsprop' or 'sgdm'");
                    }
                }

                if (!string.IsNullOrEmpty(datasetEnvelope.TrainingSettings.Loss))
                {
                    if (!new List <string>()
                    {
                        "mse", "sce"
                    }.Contains(datasetEnvelope.TrainingSettings.Loss.ToLower()))
                    {
                        throw new Exception("TrainingSetting Loss must have value 'mse' (=mean squared error) or 'sce' (=softmax cross entropy).");
                    }
                }
            }

            if (!string.IsNullOrEmpty(datasetEnvelope.ProblemType))
            {
                if (!new List <string>()
                {
                    "classification", "regression"
                }.Contains(datasetEnvelope.ProblemType.ToLower()))
                {
                    throw new Exception("ProblemType must have value 'Classification' or 'Regression' ");
                }
            }
            else
            {
                throw new Exception("Problemtype [Regression, Classification] required!");
            }
        }
Example #9
0
        public async Task <DatasetDto> UpdateAsync(int datasetId, DatasetDto updatedDataset)
        {
            var datasetFromDb = await _preApprovedDatasetModelService.GetByIdAsync(datasetId, UserOperation.PreApprovedDataset_Create_Update_Delete);

            PerformUsualTestForPostedDatasets(updatedDataset);

            if (!String.IsNullOrWhiteSpace(updatedDataset.Name) && updatedDataset.Name != datasetFromDb.Name)
            {
                datasetFromDb.Name = updatedDataset.Name;
            }
            if (!String.IsNullOrWhiteSpace(updatedDataset.Location) && updatedDataset.Location != datasetFromDb.Location)
            {
                datasetFromDb.Location = updatedDataset.Location;
            }
            if (!String.IsNullOrWhiteSpace(updatedDataset.Classification) && updatedDataset.Classification != datasetFromDb.Classification)
            {
                datasetFromDb.Classification = updatedDataset.Classification;
            }
            if (!String.IsNullOrWhiteSpace(updatedDataset.StorageAccountName) && updatedDataset.StorageAccountName != datasetFromDb.StorageAccountName)
            {
                datasetFromDb.StorageAccountName = updatedDataset.StorageAccountName;
            }
            if (updatedDataset.LRAId != datasetFromDb.LRAId)
            {
                datasetFromDb.LRAId = updatedDataset.LRAId;
            }
            if (updatedDataset.DataId != datasetFromDb.DataId)
            {
                datasetFromDb.DataId = updatedDataset.DataId;
            }
            if (updatedDataset.SourceSystem != datasetFromDb.SourceSystem)
            {
                datasetFromDb.SourceSystem = updatedDataset.SourceSystem;
            }
            if (updatedDataset.BADataOwner != datasetFromDb.BADataOwner)
            {
                datasetFromDb.BADataOwner = updatedDataset.BADataOwner;
            }
            if (updatedDataset.Asset != datasetFromDb.Asset)
            {
                datasetFromDb.Asset = updatedDataset.Asset;
            }
            if (updatedDataset.CountryOfOrigin != datasetFromDb.CountryOfOrigin)
            {
                datasetFromDb.CountryOfOrigin = updatedDataset.CountryOfOrigin;
            }
            if (updatedDataset.AreaL1 != datasetFromDb.AreaL1)
            {
                datasetFromDb.AreaL1 = updatedDataset.AreaL1;
            }
            if (updatedDataset.AreaL2 != datasetFromDb.AreaL2)
            {
                datasetFromDb.AreaL2 = updatedDataset.AreaL2;
            }
            if (updatedDataset.Tags != datasetFromDb.Tags)
            {
                datasetFromDb.Tags = updatedDataset.Tags;
            }
            if (updatedDataset.Description != datasetFromDb.Description)
            {
                datasetFromDb.Description = updatedDataset.Description;
            }
            datasetFromDb.Updated = DateTime.UtcNow;
            Validate(datasetFromDb);
            await _db.SaveChangesAsync();

            return(await GetByIdAsync(datasetFromDb.Id));
        }
        public void DatasetValidationTest()
        {
            (DataContext dbContext, UserService userService, UserRepository userRepository, DatasetRepository datasetRepository, DatasetService datasetService)
                = InitDb("DatasetValidationTest");
            User adminUser = CreateInitialuser(dbContext, "*****@*****.**");

            DatasetDto dsDto = GetXorDatasetDto(DataSetFlags.Private);

            datasetService.ValidateDataset(dsDto);

            //One hot encoding
            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Items.First().Labels = new double[] { 0d, 0d };
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Items.First().Labels = new double[] { 1d, 1d };
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Items.First().Labels = new double[] { 2d, 0d };
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Items.First().Labels = new double[] { 0d };
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            // Labels and Data defined
            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Items.First().Labels = null;
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Items.First().Data = null;
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Items.Last().Labels = null;
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Items.Last().Data = null;
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            // Outputdescription
            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.OutputDescription = new string[] { "class 1" };
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.OutputDescription = new string[] { "class 1", "class 2" };
            datasetService.ValidateDataset(dsDto);

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.OutputDescription = null;
            datasetService.ValidateDataset(dsDto);

            // Layer Definition
            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Layers.Last().Neurons = 5;
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Layers.First().Neurons = 1;
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Layers = new LayerDto[] { new LayerDto()
                                                    {
                                                        Neurons = 3
                                                    } };
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Layers[1].Neurons = 0;
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Layers[1].Activation = "Unknown";
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Layers[1].Activation = "";
            datasetService.ValidateDataset(dsDto);

            //Matrix
            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Matrix = new int[] { 1, 2 };
            datasetService.ValidateDataset(dsDto);

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Matrix = new int[] { 2, 2 };
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.Matrix = null;
            datasetService.ValidateDataset(dsDto);

            // Training definition
            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.TrainingSettings.Loss = "";
            datasetService.ValidateDataset(dsDto);

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.TrainingSettings.Loss = "Unknown";
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.TrainingSettings.Optimizer = "";
            datasetService.ValidateDataset(dsDto);

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.TrainingSettings.Optimizer = "Unknown";
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            //Problem Type
            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.ProblemType = "Unknown";
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            dsDto = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Dataset.ProblemType = "";
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            // Name
            dsDto      = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Name = "";
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto));

            CreateInitialXorDataset(dbContext, adminUser, DataSetFlags.Private);
            dsDto      = GetXorDatasetDto(DataSetFlags.Private);
            dsDto.Name = "XOR";
            Assert.ThrowsException <Exception>(() => datasetService.ValidateDataset(dsDto)); //already exists
        }
Example #11
0
        public async Task <IActionResult> UpdateAsync(int id, DatasetDto dataset)
        {
            var updatedDataset = await _datasetService.UpdateAsync(id, dataset);

            return(new JsonResult(updatedDataset));
        }
Example #12
0
        /// <summary>
        /// Configures the needed services for the webapp and creates the initial data for the database
        /// </summary>
        /// <param name="services">servicecollection</param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options =>
            {
                options.LoginPath         = "/Login/UserLogin/";
                options.Cookie.Expiration = TimeSpan.FromDays(14);
                options.Cookie.HttpOnly   = true;
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            if (_env.IsEnvironment("Development"))
            {
                ConnectionString = Configuration.GetConnectionString("DevelopmentDb");
            }
            else if (_env.IsEnvironment("Test"))
            {
                ConnectionString = Configuration.GetConnectionString("TestDb");
            }
            else if (_env.IsEnvironment("Production"))
            {
                ConnectionString = Configuration.GetConnectionString("ProductionDb");
            }
            else
            {
                throw new Exception("Environment not defined.");
            }


            using (DataContext dbContext = DataContextFactory.GetDataContext(ConnectionString))
            {
                DbCreator.CreateDbIfNotExist(dbContext, _env.IsEnvironment("Development"));
                UserService    userService = new UserService(new UserRepository(dbContext));
                DatasetService dsService   = new DatasetService(new DatasetRepository(dbContext));

                if (Configuration.GetValue <bool>("RegenerateStaticDatasets"))
                {
                    DatasetDto xor               = dsService.GetByName(XorDataset);
                    DatasetDto dice              = dsService.GetByName(DiceDataset);
                    DatasetDto or                = dsService.GetByName(OrDataset);
                    DatasetDto numbers           = dsService.GetByName(NumbersDataset);
                    DatasetDto validationNumbers = dsService.GetByName(ValidationNumbersDataset);
                    DatasetDto letters           = dsService.GetByName(LettersDataset);
                    if (xor != null)
                    {
                        dsService.Delete(xor.Id, xor.UserId);
                    }
                    if (dice != null)
                    {
                        dsService.Delete(dice.Id, dice.UserId);
                    }
                    if (or != null)
                    {
                        dsService.Delete(or.Id, or.UserId);
                    }
                    if (numbers != null)
                    {
                        dsService.Delete(numbers.Id, numbers.UserId);
                    }
                    if (validationNumbers != null)
                    {
                        dsService.Delete(validationNumbers.Id, validationNumbers.UserId);
                    }
                    if (letters != null)
                    {
                        dsService.Delete(letters.Id, letters.UserId);
                    }
                }

                foreach (UserDto user in GetInitialUsers())
                {
                    try
                    {
                        userService.Create(user);
                    }
                    catch { };
                }
                dbContext.SaveChanges();

                foreach (DatasetDto dsDto in GetInitalDatasets(dbContext.Users.First().Id))
                {
                    try
                    {
                        dsService.Update(dsDto, dbContext.Users.First().Id);
                    }
                    catch (Exception ex)
                    {
                    };
                }
            }
        }
Example #13
0
        /// <summary>
        /// Gets the list of the inital datasets
        /// </summary>
        /// <param name="userId">userid which creates the dataset</param>
        /// <returns>list of datasets</returns>
        private List <DatasetDto> GetInitalDatasets(int userId)
        {
            List <DatasetDto> datasets   = new List <DatasetDto>();
            string            location   = System.Reflection.Assembly.GetEntryAssembly().Location;
            string            directory  = System.IO.Path.GetDirectoryName(location);
            DatasetDto        xorDataset = new DatasetDto()
            {
                Json   = File.ReadAllText($"{directory}/xor.json"),
                Flags  = DataSetFlags.Public,
                UserId = userId,
                Name   = XorDataset
            };
            DatasetDto diceDataset = new DatasetDto()
            {
                Json   = File.ReadAllText($"{directory}/dice.json"),
                Flags  = DataSetFlags.Public,
                UserId = userId,
                Name   = DiceDataset
            };
            DatasetDto orDataset = new DatasetDto()
            {
                Json   = File.ReadAllText($"{directory}/or.json"),
                Flags  = DataSetFlags.Public,
                UserId = userId,
                Name   = OrDataset
            };
            DatasetDto numbersDataset = new DatasetDto()
            {
                Json   = File.ReadAllText($"{directory}/numbers.json"),
                Flags  = DataSetFlags.Public,
                UserId = userId,
                Name   = NumbersDataset
            };

            DatasetDto letters = new DatasetDto()
            {
                Json   = File.ReadAllText($"{directory}/letters.json"),
                Flags  = DataSetFlags.Public,
                UserId = userId,
                Name   = LettersDataset
            };

            DatasetDto numbersValidation = new DatasetDto()
            {
                Json   = File.ReadAllText($"{directory}/numbers_validation.json"),
                Flags  = DataSetFlags.Public,
                UserId = userId,
                Name   = ValidationNumbersDataset
            };

            DatasetDto lettersValidation = new DatasetDto()
            {
                Json   = File.ReadAllText($"{directory}/letters_validation.json"),
                Flags  = DataSetFlags.Public,
                UserId = userId,
                Name   = ValidationLettersDataset
            };

            datasets.Add(xorDataset);
            datasets.Add(diceDataset);
            datasets.Add(orDataset);
            datasets.Add(numbersDataset);
            datasets.Add(letters);
            datasets.Add(numbersValidation);
            datasets.Add(lettersValidation);

            return(datasets);
        }