Beispiel #1
0
        public async Task <IActionResult> Post(CustomerDto customer)
        {
            // If the caller doesnt provide id we try getting it from claims
            customer.Id ??= User.FindFirstValue(JwtClaimTypes.Name);

            if (customer.Id == null)
            {
                logger.LogDebug("No id provided or found for the User");
                return(BadRequest("No Id provided"));
            }

            var dbCustomer = await dbContext.Customers.FindAsync(customer.Id);

            if (dbCustomer != null)
            {
                logger.LogDebug($"Customer already exists with id: {customer.Id}");
                return(BadRequest("Customer already exists for this user"));
            }

            dbCustomer = new Customer
            {
                Id = customer.Id,
                RegistrationNumber = customer.RegistrationNumber,
                Balance            = customer.Amount
            };

            dbCustomer = (await dbContext.AddAsync(dbCustomer)).Entity;

            await dbContext.SaveChangesAsync();

            logger.LogDebug($"Customer created with id: {customer.Id}");

            return(Created($"customer/{dbCustomer.Id}", dbCustomer));
        }
Beispiel #2
0
        public async Task <string> Get1()
        {
            var dbconnect = _aplloDbContext.Database.GetDbConnection();


            BankDbContext bankDb = new BankDbContext(new DbContextOptionsBuilder <BankDbContext>().UseNpgsql(dbconnect).Options);


            using (var transcation = await _aplloDbContext.Database.BeginTransactionAsync())
            {
                await _aplloDbContext.AddAsync(new TestApllo { Name = "2222撒大声地" });

                await _aplloDbContext.SaveChangesAsync();

                bankDb.Database.UseTransaction(transcation.GetDbTransaction());

                await bankDb.AddAsync(new TestBank { Name = "444444sdsd" });

                await bankDb.SaveChangesAsync();

                await transcation.CommitAsync();
            }

            return("2");
        }