Ejemplo n.º 1
0
        public async Task <IActionResult> PutCustomer([FromRoute] long id, [FromBody] Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != customer.CustomerId)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <Customer> > PostCustomer(Customer customer)
        {
            if (ModelState.IsValid)
            {
                _context.CustomerList.Add(customer);
                await _context.SaveChangesAsync();
            }

            return(CreatedAtAction(nameof(GetCustomer), new { id = customer.Id }, customer));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> AjDelete(int id)
        {
            var customer = db.Customers.Find(id);

            db.Customers.Remove(customer);
            await db.SaveChangesAsync();

            var customers = db.Customers;

            return(PartialView("Myclients", customers));
        }
        public async Task Create(Customer customer)
        {
            _context.Customers.Add(customer);
            await _context.SaveChangesAsync();

            //return customer;
        }
Ejemplo n.º 5
0
        //To Add new customer record
        public async Task <int> AddCustomer(CustomerViewModel customerVM)
        {
            //Determine the next ID
            var newID    = _context.Customers.Select(x => x.Id).Max() + 1;
            var customer = new Customer()
            {
                Id          = newID,
                FirstName   = customerVM.FirstName,
                LastName    = customerVM.LastName,
                DateOfBirth = customerVM.DateOfBirth
            };

            _context.Customers.Add(customer);
            _logger.LogInformation(InformationMessages.CustomerAddedSuccessfully, customer.Id, JsonConvert.SerializeObject(customer));
            return(await _context.SaveChangesAsync());
        }
Ejemplo n.º 6
0
 public async Task <bool> Update(int id, Customer customer)
 {
     _context.Entry(customer).State = EntityState.Modified;
     try
     {
         await _context.SaveChangesAsync();
     }
     catch (DbUpdateConcurrencyException)
     {
         if (!CustomerExists(id))
         {
             return(false);
         }
         else
         {
             throw;
         }
     }
     return(true);
 }
Ejemplo n.º 7
0
        public async Task <int> DeleteAsync(int id)
        {
            try
            {
                Customer customer = await GetCustomerByIdAsync(id);

                if (customer == null)
                {
                    throw new EntityNotFoundException($"There is no Customer with Id {id}");
                }

                _dbContext.Remove(customer);
                return(await _dbContext.SaveChangesAsync());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error occurred");
                throw;
            }
        }
        public async Task HandleAsync(string key, User value)
        {
            _dbContext.Customers.Add(new Customer.Data.Customer
            {
                Id        = value.Id,
                Email     = value.Email,
                FirstName = value.FirstName,
                LastName  = value.LastName,
            });

            await _dbContext.SaveChangesAsync();
        }
        public async Task HandleAsync(OrderCompleted _event, ICorrelationContext context)
        {
            var basket = await _dbContext.Baskets
                         .Include(i => i.Items)
                         .FirstOrDefaultAsync(q => q.CustomerId == context.CustomerId);

            _dbContext.BasketItems.RemoveRange(basket.Items);
            await _dbContext.SaveChangesAsync();

            //no event, just logging
            _logger.LogInformation($"[Local Transaction] : Basket items cleared. CorrelationId: {context.CorrelationId}");
        }
        public async Task HandleAsync(string key, UserCreatedEvent @event)
        {
            _dbContext.Customers.Add(new Data.Customer
            {
                Id        = @event.Id,
                Email     = @event.Email,
                FirstName = @event.FirstName,
                LastName  = @event.LastName,
            });

            await _dbContext.SaveChangesAsync();
        }
Ejemplo n.º 11
0
 public void SeedData()
 {
     if (!_customerDBContex.Customers.Any())
     {
         _customerDBContex.Customers.Add(new Customer {
             ID = 1, Name = "AbC", Address = "Varanasi"
         });
         _customerDBContex.Customers.Add(new Customer {
             ID = 2, Name = "AbC1", Address = "Bangalore"
         });
         _customerDBContex.Customers.Add(new Customer {
             ID = 3, Name = "AbC2", Address = "Delhi"
         });
         _customerDBContex.SaveChangesAsync();
     }
 }
        public async Task HandleAsync(AddProductToBasket command, ICorrelationContext context)
        {
            // Warning: Customer service needs Product service's data.
            // What if Product service can't response for a while? (assume no retry policy)
            // DDD - sharing data betwwen bounded cotext

            var product = await _productHttpService.GetAsync(command.ProductId);

            if (product == null)
            {
                throw new Exception($"Product not found. Id: {command.ProductId}");
            }

            var basket = await _dbContext.Baskets.FirstOrDefaultAsync(q => q.CustomerId == context.CustomerId);

            if (basket == null)
            {
                throw new Exception($"Basket not found for customer: {context.CustomerId}");
            }

            var basketItem = await _dbContext.BasketItems.FirstOrDefaultAsync(q => q.ProductId == command.ProductId);

            if (basketItem != null)
            {
                basketItem.Quantity += command.Quantity;
            }
            else
            {
                _dbContext.BasketItems.Add(
                    new BasketItem
                {
                    BasketId    = basket.Id,
                    ProductId   = command.ProductId,
                    Quantity    = command.Quantity,
                    ProductName = product.Name,
                    UnitPrice   = product.Price
                });
            }

            await _dbContext.SaveChangesAsync();

            // no event, just logging.
            _logger.LogInformation($"[Local Transaction] : {command.Quantity} {product.Name} added to basket. CorrelationId: {context.CorrelationId}");
        }
Ejemplo n.º 13
0
        public async Task HandleAsync(CreateCustomer _event, ICorrelationContext context)
        {
            _dbContext.Customers.Add(new Customer
            {
                Id        = _event.Id,
                Password  = _event.Password,
                Email     = _event.Email,
                FirstName = _event.FirstName,
                LastName  = _event.LastName,
                Address   = _event.Address
            });

            _dbContext.Baskets.Add(new Basket
            {
                CustomerId = _event.Id
            });

            await _dbContext.SaveChangesAsync();

            _logger.LogInformation($"[Local Transaction] : Customer created. CorrelataionId: {context.CorrelationId}");
        }
Ejemplo n.º 14
0
        protected override async Task Handle(UpdateCustomerCommand command, CancellationToken cancellationToken)
        {
            var customer = await _dbContext.Customers.FirstOrDefaultAsync(s => s.Email == command.Email);

            if (customer == null)
            {
                throw new ApplicationException("Email is not found.");
            }

            customer.FirstName   = command.FirstName;
            customer.LastName    = command.LastName;
            customer.Address     = command.Address;
            customer.PhoneNumber = command.PhoneNumber;
            customer.Gender      = command.Gender;
            customer.BirthDate   = command.BirthDate;

            var outboxEvent = new Outbox
            {
                Id            = Guid.NewGuid(),
                AggregateId   = customer.Id,
                AggregateType = "Customer",
                Type          = "CustomerUpdated",
                Payload       = JsonSerializer.Serialize(new CustomerUpdatedEvent
                {
                    Email       = customer.Email,
                    FirstName   = command.FirstName,
                    LastName    = command.LastName,
                    Address     = customer.Address,
                    BirthDate   = customer.BirthDate,
                    PhoneNumber = customer.PhoneNumber,
                    Gender      = customer.Gender
                })
            };

            _dbContext.Customers.Update(customer);

            _dbContext.OutboxEvents.Add(outboxEvent);

            await _dbContext.SaveChangesAsync();
        }
Ejemplo n.º 15
0
 private async Task SaveAsync()
 {
     await _dbContext.SaveChangesAsync();
 }
Ejemplo n.º 16
0
 public async Task CreateAsync(Customer customer)
 {
     _context.Add(customer);
     await _context.SaveChangesAsync();
 }