private async Task <CreateContainerViewModel> CreateViewModelWithPopulatedFormInputs() { var containerVM = new CreateContainerViewModel(); containerVM.ContainerTypes = await _context.ContainerTypes.AsNoTracking().Select(ct => new ContainerTypes { Id = ct.Id, Type = ct.Type }).ToListAsync(); containerVM.ContainerDepartments = await _context.ContainerDepartments.AsNoTracking().Select(cd => new ContainerDepartments { Id = cd.Id, Department = cd.Department }).ToListAsync(); containerVM.Bays = await _context.Bays.AsNoTracking().Select(b => new SelectListItem() { Value = b.Id.ToString(), Text = b.Bay.ToString() }).ToListAsync(); containerVM.Doors = await _context.Doors.AsNoTracking().Select(d => new SelectListItem() { Value = d.Id.ToString(), Text = d.Door }).ToListAsync(); containerVM.Suppliers = await _context.Suppliers.AsNoTracking().OrderBy(s => s.Supplier).Select(s => new SelectListItem() { Value = s.Id.ToString(), Text = s.Supplier }).ToListAsync(); containerVM.Statuses = await _context.ContainerStatus.AsNoTracking().Select(cs => new SelectListItem() { Selected = cs.Id == 1 ? true : false, Value = cs.Id.ToString(), Text = cs.Status }).ToListAsync(); return(containerVM); }
public void Validate_InvalidNumber_HaveNumberValidationError(string number) { var model = new CreateContainerViewModel { Categoria = ContainerCategory.Exportation, Cliente = "Fernando", Numero = number, Status = ContainerStatus.Full, Tipo = 20 }; TestValidationResult <CreateContainerViewModel> result = _createContainerValidator.TestValidate(model); result.ShouldHaveValidationErrorFor(container => container.Numero); }
public void Validate_CategoryOutOfEnum_HaveCategoryValidationError() { var model = new CreateContainerViewModel { Categoria = (ContainerCategory)2, Cliente = "Fernando", Numero = "abcd1234567", Status = ContainerStatus.Full, Tipo = 20 }; TestValidationResult <CreateContainerViewModel> result = _createContainerValidator.TestValidate(model); result.ShouldHaveValidationErrorFor(container => container.Categoria); }
public void Validate_InvalidType_HaveTypeValidationError() { var model = new CreateContainerViewModel { Categoria = ContainerCategory.Importation, Cliente = "Fernando", Numero = "abcd1234567", Status = ContainerStatus.Empty, Tipo = 15 }; TestValidationResult <CreateContainerViewModel> result = _createContainerValidator.TestValidate(model); result.ShouldHaveValidationErrorFor(container => container.Tipo); }
public void Validate_CustomerNameIsEmpty_HaveCustomerValidationError() { var model = new CreateContainerViewModel { Categoria = ContainerCategory.Importation, Cliente = "", Numero = "abcd1234567", Status = ContainerStatus.Empty, Tipo = 20 }; TestValidationResult <CreateContainerViewModel> result = _createContainerValidator.TestValidate(model); result.ShouldHaveValidationErrorFor(container => container.Cliente); }
public void Validate_CustomerNameGreaterThan50_HaveCustomerValidationError() { var model = new CreateContainerViewModel { Categoria = ContainerCategory.Importation, Cliente = "IjCQagrhMsxQwIZqfCINnYlYtDuNbKRnYZjteFlDSyNrXvRqLDCmTDTeZdpd", Numero = "abcd1234567", Status = ContainerStatus.Empty, Tipo = 20 }; TestValidationResult <CreateContainerViewModel> result = _createContainerValidator.TestValidate(model); result.ShouldHaveValidationErrorFor(container => container.Cliente); }
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)); }
public void Validate_ValidModel_ShouldNotHaveValidationErrors(CreateContainerViewModel model) { TestValidationResult <CreateContainerViewModel> result = _createContainerValidator.TestValidate(model); result.ShouldNotHaveAnyValidationErrors(); }
public async Task <IActionResult> Update(CreateContainerViewModel containerVM) { if (ModelState.IsValid == false) { return(View(await CreateViewModelWithPopulatedFormInputs())); } var container = await _context.Containers.FindAsync(containerVM.ContainerId); if (container == null) { return(NotFound("Container record not found")); } container.RefNum = containerVM.RefNum; container.ExpTimeOfArrival = containerVM.ExpTimeOfArrival; container.ExpNumOfUnits = containerVM.ExpNumOfUnits; container.ExpNumOfPallets = containerVM.ExpNumOfPallets; container.ActTimeOfArrival = containerVM.ActTimeOfArrival; container.ActNumOfUnits = containerVM.ActNumOfUnits; container.ActNumOfPallets = containerVM.ActNumOfPallets; container.ArrivalDate = DateTime.Today; container.DepartmentId = containerVM.ContainerDepartmentId; container.TypeId = containerVM.ContainerTypeId; container.BayId = containerVM.BayId; container.DoorId = containerVM.DoorId; container.StatusId = containerVM.ContainerStatusId; container.ItemDiscrepancy = (containerVM.ExpNumOfUnits.HasValue && containerVM.ActNumOfUnits.HasValue) ? (containerVM.ExpNumOfUnits.Value - containerVM.ActNumOfUnits.Value) : (int?)null; _context.Update(container); foreach (var comment in containerVM.Comments) { if (comment.Id == 0) { var containerComment = new ContainerComments() { ContainerId = container.Id, Comment = comment.Comment }; _context.Add(containerComment); } else if (comment.Id != 0) { _context.Update(comment); } } if (containerVM.SupplierIds.Count() > 0) { var savedSuppliers = await _context.ContainerSuppliers.Where(cs => cs.ContainerId == container.Id).ToListAsync(); var suppliersToDel = new List <ContainerSuppliers>(savedSuppliers); foreach (var supplierId in containerVM.SupplierIds) { bool addThisSupplier = true; foreach (var savedSupplier in savedSuppliers) { if (savedSupplier.SupplierId == supplierId) { addThisSupplier = false; suppliersToDel.Remove(savedSupplier); } } if (addThisSupplier) { var containerSuppliers = new ContainerSuppliers() { ContainerId = container.Id, SupplierId = supplierId.Value }; _context.Add(containerSuppliers); } } _context.RemoveRange(suppliersToDel); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create(CreateContainerViewModel containerVM) { if (ModelState.IsValid == false) { return(View(await CreateViewModelWithPopulatedFormInputs())); } int?itemD = null; if (containerVM.ExpNumOfUnits != null && containerVM.ActNumOfUnits != null) { itemD = containerVM.ExpNumOfUnits.Value - containerVM.ActNumOfUnits.Value; } var container = new Containers() { RefNum = containerVM.RefNum, ExpTimeOfArrival = containerVM.ExpTimeOfArrival, ExpNumOfUnits = containerVM.ExpNumOfUnits, ExpNumOfPallets = containerVM.ExpNumOfPallets, ActTimeOfArrival = containerVM.ActTimeOfArrival, ActNumOfUnits = containerVM.ActNumOfUnits, ActNumOfPallets = containerVM.ActNumOfPallets, ArrivalDate = DateTime.Today, DepartmentId = containerVM.ContainerDepartmentId, TypeId = containerVM.ContainerTypeId, BayId = containerVM.BayId, DoorId = containerVM.DoorId, StatusId = containerVM.ContainerStatusId, ItemDiscrepancy = (containerVM.ExpNumOfUnits.HasValue && containerVM.ActNumOfUnits.HasValue) ? (containerVM.ExpNumOfUnits.Value - containerVM.ActNumOfUnits.Value) : (int?)null }; _context.Add(container); await _context.SaveChangesAsync(); foreach (var comment in containerVM.Comments) { var containerComment = new ContainerComments() { ContainerId = container.Id, Comment = comment.Comment }; _context.Add(containerComment); } if (containerVM.SupplierIds?.Count() > 0) { foreach (var supplierId in containerVM.SupplierIds) { var containerSuppliers = new ContainerSuppliers() { ContainerId = container.Id, SupplierId = supplierId.Value }; _context.Add(containerSuppliers); } } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }