public async Task <ActionResult <HandlingViewModel> > Create(string containerNumber, CreateHandlingViewModel model) { Container container = await _containerContext.Containers .AsNoTracking() .Select(_container => new Container { Id = _container.Id, Number = _container.Number }) .FirstOrDefaultAsync(_container => _container.Number == containerNumber); if (container is null) { return(NotFound()); } Handling handling = _mapper.Map <Handling>(model); handling.ContainerId = container.Id; _ = await _containerContext.Handlings.AddAsync(handling); _ = await _containerContext.SaveChangesAsync(); HandlingViewModel viewModel = _mapper.Map <HandlingViewModel>(handling); return(CreatedAtAction(nameof(GetById), new { containerId = container.Id, handlingId = handling.Id }, viewModel)); }
public async Task <ActionResult> Register(OrderForm form) { using (var dbContext = new ContainerContext()) { var customer = await dbContext.Customers.FirstOrDefaultAsync(c => c.Email == form.Email); if (customer == null) { customer = new Customer() { Email = form.Email }; dbContext.Customers.Add(customer); await dbContext.SaveChangesAsync(); } // TODO use dbcontext store order var order = new Order { CustomerId = customer.Id, NumberOfContainers = form.NumberOfUnits }; dbContext.Orders.Add(order); await dbContext.SaveChangesAsync(); // TODO produce containers for (var i = 0; i < form.NumberOfUnits; i++) { var container = new Container { OrderId = order.Id, ProductionDate = DateTime.UtcNow }; await Task.Delay(500); dbContext.Conatiners.Add(container); } await dbContext.SaveChangesAsync(); // TODO send out confirmation email } return(RedirectToAction("ThankYou")); }
public async Task <ActionResult <ContainerViewModel> > Create(CreateContainerViewModel model) { Container container = _mapper.Map <Container>(model); string numberInDatabase = await _containerContext.Containers .AsNoTracking() .Select(_container => _container.Number) .FirstOrDefaultAsync(number => number == container.Number); if (numberInDatabase is not null) { return(Conflict()); } _ = await _containerContext.Containers.AddAsync(container); _ = await _containerContext.SaveChangesAsync(); ContainerViewModel viewModel = _mapper.Map <ContainerViewModel>(container); return(CreatedAtAction(nameof(GetById), new { id = container.Id }, viewModel)); }