Ejemplo n.º 1
0
        public void PopulateQuestionTemplate(string questionFile)
        {
            string ConnectionString = Environment.GetEnvironmentVariable("Database__ConnectionString");

            if (string.IsNullOrEmpty(ConnectionString))
            {
                throw new ArgumentException("ConnectionString cannot be empty");
            }
            DbContextOptionsBuilder <BmtDbContext> builder = new DbContextOptionsBuilder <BmtDbContext>();

            builder.UseSqlServer(ConnectionString);
            BmtDbContext context = new BmtDbContext(builder.Options);

            List <QuestionTemplate> questions;

            using (StreamReader reader = new StreamReader(questionFile))
            {
                string json = reader.ReadToEnd();
                questions = JsonSerializer.Deserialize <List <QuestionTemplate> >(json, JsonUtils.SerializerOptions);
            }
            int order = 0;

            foreach (QuestionTemplate q in questions)
            {
                q.CreateDate = DateTimeOffset.UtcNow;
                q.Status     = Status.Active;
                q.Order      = order;
                order       += 1;
            }

            context.QuestionTemplates.AddRange(questions);
            context.SaveChanges();

            Console.WriteLine($"Added {questions.Count} to questionTemplate table in DB");
        }
Ejemplo n.º 2
0
        protected DbContextTestSetup()
        {
            DbContextOptionsBuilder <BmtDbContext> builder = new DbContextOptionsBuilder <BmtDbContext>();
            string connectionString = new SqliteConnectionStringBuilder {
                DataSource = "file::memory:", Cache = SqliteCacheMode.Shared
            }.ToString();

            _connection = new SqliteConnection(connectionString);
            _connection.Open();
            builder.EnableSensitiveDataLogging();
            builder.UseSqlite(_connection);
            _context = new BmtDbContext(builder.Options);
            _context.Database.EnsureCreated();
            InitContent.PopulateDb(_context);
        }
Ejemplo n.º 3
0
        public void ParticipantUnique()
        {
            DbContextOptionsBuilder <BmtDbContext> builder = new DbContextOptionsBuilder <BmtDbContext>();

            builder.UseSqlite(_connection);

            // This is done because bug in xunit making Dbcontext dispose never being called when exception is thrown
            using (var _context = new BmtDbContext(builder.Options))
            {
                EvaluationService evaluationService = new EvaluationService(_context);
                Evaluation        evaluation        = evaluationService.GetAll().First();

                ParticipantService participantService = new ParticipantService(_context);
                participantService.Create("azure_unique_id", evaluation, Organization.All, Role.OrganizationLead);
                Assert.Throws <DbUpdateException>(() => participantService.Create("azure_unique_id", evaluation, Organization.All, Role.OrganizationLead));
            }
        }
Ejemplo n.º 4
0
        public void OrderQuestionsInEvaluation(string evaluationId)
        {
            string ConnectionString = Environment.GetEnvironmentVariable("Database__ConnectionString");

            if (string.IsNullOrEmpty(ConnectionString))
            {
                throw new ArgumentException("ConnectionString cannot be empty");
            }
            DbContextOptionsBuilder <BmtDbContext> builder = new DbContextOptionsBuilder <BmtDbContext>();

            builder.UseSqlServer(ConnectionString);
            BmtDbContext context = new BmtDbContext(builder.Options);

            List <Question> questions = context.Questions.Include(q => q.QuestionTemplate).Where(q => q.Evaluation.Id.Equals(evaluationId)).ToList();

            foreach (Question q in questions)
            {
                q.Order = q.QuestionTemplate.Order;
            }
            context.Questions.UpdateRange(questions);
            context.SaveChanges();
        }
Ejemplo n.º 5
0
        public void SetAdminOrderOnQuestionTemplates()
        {
            string ConnectionString = Environment.GetEnvironmentVariable("Database__ConnectionString");

            if (string.IsNullOrEmpty(ConnectionString))
            {
                throw new ArgumentException("ConnectionString cannot be empty");
            }

            DbContextOptionsBuilder <BmtDbContext> builder = new DbContextOptionsBuilder <BmtDbContext>();

            builder.UseSqlServer(ConnectionString);
            BmtDbContext context = new BmtDbContext(builder.Options);

            var questionTemplates = context.QuestionTemplates;

            foreach (var qt in questionTemplates)
            {
                qt.AdminOrder = qt.Order;
                context.QuestionTemplates.Update(qt);
            }
            context.SaveChanges();
        }
Ejemplo n.º 6
0
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            // In-memory sqlite requires an open connection throughout the whole lifetime of the database
            _sqlConnectionString = Configuration.GetSection("Database").GetValue <string>("ConnectionString");
            if (string.IsNullOrEmpty(_sqlConnectionString))
            {
                DbContextOptionsBuilder <BmtDbContext> builder = new DbContextOptionsBuilder <BmtDbContext>();
                string connectionString = new SqliteConnectionStringBuilder {
                    DataSource = "file::memory:", Cache = SqliteCacheMode.Shared
                }.ToString();
                _connection = new SqliteConnection(connectionString);
                _connection.Open();
                builder.UseSqlite(_connection);

                using (BmtDbContext context = new BmtDbContext(builder.Options))
                {
                    context.Database.EnsureCreated();
                    InitContent.PopulateDb(context);
                }
            }
        }
Ejemplo n.º 7
0
        public void OrderQuestionTemplates(string questionFile)
        {
            string ConnectionString = Environment.GetEnvironmentVariable("Database__ConnectionString");

            if (string.IsNullOrEmpty(ConnectionString))
            {
                throw new ArgumentException("ConnectionString cannot be empty");
            }
            DbContextOptionsBuilder <BmtDbContext> builder = new DbContextOptionsBuilder <BmtDbContext>();

            builder.UseSqlServer(ConnectionString);
            BmtDbContext context = new BmtDbContext(builder.Options);

            List <QuestionTemplate> questionsFromFile;

            using (StreamReader reader = new StreamReader(questionFile))
            {
                string json = reader.ReadToEnd();
                questionsFromFile = JsonSerializer.Deserialize <List <QuestionTemplate> >(json, JsonUtils.SerializerOptions);
            }

            List <QuestionTemplate> questions = context.QuestionTemplates.ToList();

            Console.WriteLine($"Ordering {questions.Count} questions");
            foreach (QuestionTemplate questionTemplate in questions)
            {
                QuestionTemplate qtFromFile = questionsFromFile.First(qt => qt.Text.Equals(questionTemplate.Text));
                questionTemplate.Order = qtFromFile.Order + 1;
            }

            List <int>    ints   = questions.Select(qt => qt.Order).ToList();
            HashSet <int> intSet = new HashSet <int>(ints);

            Console.WriteLine($"Distinct orders after: {intSet.Count}");
            context.QuestionTemplates.UpdateRange(questions);
            context.SaveChanges();
        }
Ejemplo n.º 8
0
 public ClosingRemarkService(BmtDbContext context)
 {
     _context = context;
 }
Ejemplo n.º 9
0
 public AnswerService(BmtDbContext context)
 {
     _context = context;
 }
Ejemplo n.º 10
0
 public NoteService(BmtDbContext context)
 {
     _context = context;
 }
Ejemplo n.º 11
0
 public QuestionTemplateService(BmtDbContext context)
 {
     _context = context;
 }
Ejemplo n.º 12
0
 public ParticipantService(BmtDbContext context)
 {
     _context = context;
 }
Ejemplo n.º 13
0
 public ActionService(BmtDbContext context)
 {
     _context = context;
 }
Ejemplo n.º 14
0
 public ProjectService(BmtDbContext context)
 {
     _context = context;
 }
Ejemplo n.º 15
0
 public EvaluationService(BmtDbContext context)
 {
     _context = context;
 }
Ejemplo n.º 16
0
 public ProjectCategoryService(BmtDbContext context)
 {
     _context = context;
 }
Ejemplo n.º 17
0
 public QuestionService(BmtDbContext context)
 {
     _context = context;
 }