Exemple #1
0
        public async Task <AppUser> Create(AppUser user)
        {
            _context.AppUsers.Add(user);
            await _context.SaveChangesAsync();

            return(user);
        }
Exemple #2
0
        public async Task ReplaceTestAnalysis(Guid testId, TestAnalysis newTest)
        {
            var test = await _context.LimpingTests.Include(x => x.TestAnalysis).SingleAsync(x => x.Id == testId);

            if (test.TestAnalysis != null)
            {
                _context.TestAnalyses.Remove(test.TestAnalysis);
            }

            test.TestAnalysis          = newTest;
            _context.Entry(test).State = EntityState.Modified;
            await _context.SaveChangesAsync();
        }
Exemple #3
0
        public async Task <LimpingTest> InsertTest(string userId, string testData, TestAnalysis testAnalysis)
        {
            var limpingTest = new LimpingTest
            {
                AppUserId    = userId,
                TestData     = testData,
                Date         = DateTime.Now,
                TestAnalysis = testAnalysis
            };

            _context.LimpingTests.Add(limpingTest);
            await _context.SaveChangesAsync();

            return(limpingTest);
        }
        private async Task <LimpingTest> AddDefaultDataForAnalysis(LimpingDbContext context)
        {
            var analysis = new TestAnalysis
            {
                Id              = Guid.NewGuid(),
                Description     = "Something",
                EndValue        = 1.5,
                LimpingSeverity = LimpingSeverityEnum.Medium,
            };
            var appUser = context.AppUsers.Add(new AppUser
            {
                Id           = "1",
                UserName     = "******",
                LimpingTests = new List <LimpingTest>(),
                Email        = "f",
            }).Entity;
            var limpingTest = context.LimpingTests.Add(new LimpingTest
            {
                AppUserId    = appUser.Id,
                Date         = DateTime.Now,
                TestAnalysis = analysis,
                TestData     = "{numbers: [1, 2, 3]}"
            }).Entity;
            await context.LimpingTests.AddAsync(limpingTest);

            await context.SaveChangesAsync();

            context.Entry(limpingTest).State = EntityState.Detached;
            context.Entry(appUser).State     = EntityState.Detached;
            context.Entry(analysis).State    = EntityState.Detached;
            return(limpingTest);
        }
Exemple #5
0
 private async Task AddDefaultDataForLimping(LimpingDbContext context)
 {
     context.AppUsers.Add(new AppUser
     {
         Id           = "1",
         UserName     = "******",
         LimpingTests = new List <LimpingTest>(),
         Email        = "f",
     });
     await context.SaveChangesAsync();
 }
Exemple #6
0
        public override async Task Apply()
        {
            var defaultUser = new AppUser
            {
                Email        = "*****@*****.**",
                Id           = "-1",
                LimpingTests = new List <LimpingTest>(),
                UserName     = "******",
            };

            _context.AppUsers.Add(defaultUser);
            await _context.SaveChangesAsync();
        }
Exemple #7
0
        // Npgsql does not seem to properly support HasData, so use this as a workaround:
        // https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/367
        public async Task ApplyDataMigrations()
        {
            var appliedMigrations = await _context.DataMigrations
                                    .ToDictionaryAsync(dataMigration => dataMigration.Id, _ => true);

            var newMigrations = AllMigrations
                                .Where(migration => !appliedMigrations.ContainsKey(migration.Id));

            foreach (var migration in newMigrations)
            {
                await migration.Apply();

                await _context.DataMigrations
                .AddAsync(new DataMigration(migration.Id));

                await _context.SaveChangesAsync();
            }
        }
Exemple #8
0
        public async Task <IActionResult> CreateUser([FromBody] CreateUserDto userDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // Check if a user with the same username or email exsists
            var hasConflict = _context
                              .AppUsers
                              .Any(user =>
                                   user.UserName == userDto.UserName ||
                                   user.Email == userDto.Email
                                   );

            // Return conflict if it does
            if (hasConflict)
            {
                return(Conflict());
            }
            else
            {
                // Create the user
                var user = new AppUser
                {
                    UserName = userDto.UserName,
                    Email    = userDto.Email
                };
                _context.AppUsers.Add(user);
                await _context.SaveChangesAsync();

                // Transform the response into HAL
                var response = new GetUserResponse(
                    user,
                    LinkGenerator.Users.Create("self")
                    );
                return(Ok(response));
            }
        }