public async Task InsertCustomersSampleData(CustomersDbContext db)
        {
            var states = GetStates();

            db.States.AddRange(states);
            try
            {
                int numAffected = await db.SaveChangesAsync();

                _Logger.LogInformation(@"Saved {numAffected} states");
            }
            catch (Exception exp)
            {
                _Logger.LogError($"Error in {nameof(CustomersDbSeeder)}: " + exp.Message);
                throw;
            }

            var customers = GetCustomers(states);

            db.Customers.AddRange(customers);

            try
            {
                int numAffected = await db.SaveChangesAsync();

                _Logger.LogInformation($"Saved {numAffected} customers");
            }
            catch (Exception exp)
            {
                _Logger.LogError($"Error in {nameof(CustomersDbSeeder)}: " + exp.Message);
                throw;
            }
        }
Пример #2
0
        public async Task <IActionResult> PutCustomer(int id, Customer customer)
        {
            if (id != customer.id)
            {
                return(BadRequest());
            }

            _context.Entry(customer).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task InsertCustomersSampleData(CustomersDbContext db)
        {
            var states = GetStates();
            db.States.AddRange(states);
            try
            {
                await db.SaveChangesAsync();
            }
            catch (Exception exp)
            {                
                _logger.LogError($"Error in {nameof(CustomersDbSeeder)}: " + exp.Message);
                throw; 
            }

            var customers = GetCustomers(states);
            db.Customers.AddRange(customers);

            try
            {
              await db.SaveChangesAsync();
            }
            catch (Exception exp)
            {
              _logger.LogError($"Error in {nameof(CustomersDbSeeder)}: " + exp.Message);
              throw;
            }

        }
        public async Task <(bool IsSuccess, Db.Customer Customer, string ErrorMessage)> PostCustomerAsync([FromBody] Models.Customer customer)
        {
            try
            {
                logger?.LogInformation("Creating customer");
                var mapper      = configurationProvider.CreateMapper();
                var newcustomer = mapper.Map <Db.Customer>(customer);
                if (newcustomer != null)
                {
                    dbContext.Customers.Add(newcustomer);
                    await dbContext.SaveChangesAsync();

                    logger?.LogInformation($"product created {newcustomer}");

                    var createPostCustomerCommand = new CreatePostCustomerCommand(newcustomer.Id, customer.Name, customer.Address);
                    await eventBus.SendCommand(createPostCustomerCommand);

                    return(true, newcustomer, null);
                }


                return(false, null, "Not created");
            }
            catch (Exception ex)
            {
                logger?.LogError(ex.ToString());
                return(false, null, ex.Message);
            }
        }
        public async Task <IActionResult> Create([Bind("ID,Title,Author,Price,Rating")] Books books)
        {
            if (ModelState.IsValid)
            {
                _context.Add(books);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(books));
        }
        public async Task <IActionResult> Create([Bind("Id,Place,Address,Description,Open_Hours")] Attraction attraction)
        {
            if (ModelState.IsValid)
            {
                _context.Add(attraction);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(attraction));
        }
Пример #7
0
        public async Task <IActionResult> Create([Bind("Id,Name,Mobile,Email,Address")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(customer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(customer));
        }
        public async Task <IActionResult> Create([Bind("Id,Number_of_Tickets,Ticket_Price,CustomerId,AttractionId")] Booking booking)
        {
            if (ModelState.IsValid)
            {
                _context.Add(booking);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AttractionId"] = new SelectList(_context.Attraction, "Id", "Address", booking.AttractionId);
            ViewData["CustomerId"]   = new SelectList(_context.Customer, "Id", "Id", booking.CustomerId);
            return(View(booking));
        }
        public async Task <IActionResult> Create([Bind("ID,CustomerID,BooksID,OrderDate")] Order order)
        {
            if (ModelState.IsValid)
            {
                _context.Add(order);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BooksID"]    = new SelectList(_context.Books, "ID", "ID", order.BooksID);
            ViewData["CustomerID"] = new SelectList(_context.Customer, "ID", "ID", order.CustomerID);
            return(View(order));
        }
        public async Task <Customer> InsertCustomerAsync(Customer customer)
        {
            _Context.Add(customer);
            try
            {
                await _Context.SaveChangesAsync();
            }
            catch (System.Exception exp)
            {
                _Logger.LogError($"Error in {nameof(InsertCustomerAsync)}: " + exp.Message);
            }

            return(customer);
        }
Пример #11
0
        public async Task <ActionResult <CustomersForInsertModel> > InputCustomerAsync(CustomersForInsertModel customer)
        {
            try
            {
                CustomersModel newCustomer = GetNewCustomer(customer);
                _context.Add(newCustomer);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(NoContent());
        }
Пример #12
0
        public async Task <IActionResult> Post([FromBody] Customer customer)
        {
            try
            {
                _customersDbContext.Customers.Add(customer);
                await _customersDbContext.SaveChangesAsync();

                return(Ok(customer));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                _logger.LogError(ex.Message);
                throw;
            }
        }
Пример #13
0
        public async Task <ActionResult <Customer> > CreateCustomerAsync([FromBody, Required] CustomerInput customer)
        {
            var newCustomer = new Customer(customer);

            try
            {
                await context.AddAsync(newCustomer);

                await context.SaveChangesAsync();
            }
            catch (Exception)
            {
                return(BadRequest());
            }

            return(CreatedAtAction("GetCustomer", new { id = newCustomer.Id }, newCustomer));
        }
Пример #14
0
        public async Task Setup()
        {
            using var context = new CustomersDbContext(dbContextOptions);
            await context.AddRangeAsync(customers);

            await context.SaveChangesAsync();

            customersController = new CustomersController(new CustomersDbContext(dbContextOptions));
        }
Пример #15
0
        public async Task <IActionResult> Index()
        {
            var newCustomer = new Customer {
                Name = "Michelle Thomas", City = "Los Angeles"
            };

            _context.Customers.Add(newCustomer);
            await _context.SaveChangesAsync();

            var customer = _context.Customers.FirstOrDefault();

            return(View(customer));
        }
Пример #16
0
        public async Task <RegisterViewModel> Register(CreateUserModel registerRequest)
        {
            var register = customersDbContext.Customers.SingleOrDefault(customer => (customer.Email == registerRequest.Email || customer.Phone == registerRequest.Phone));

            if (register != null)
            {
                return(null);
            }

            var customer = _mapper.Map <User>(registerRequest);

            await customersDbContext.Customers.AddAsync(customer);

            await customersDbContext.SaveChangesAsync();

            return(new RegisterViewModel {
                Fullname = customer.Fullname, Status = "200", Message = "Success"
            });
        }
 public async Task Add(Customer customer)
 {
     _customersDbContext.Customers.Add(customer);
     await _customersDbContext.SaveChangesAsync();
 }