public void CreateAndUpdateSuppplier(AddSupplierDTO dto) { if (dto.SupplierId != 0) { var supplier = dBContext.Suppliers.FirstOrDefault(x => x.Id == dto.SupplierId); if (supplier != null) { supplier.CompanyId = dto.CompanyId; supplier.ContactNumber = dto.ContactNumber; supplier.Address = dto.Address; supplier.EmailAddress = dto.EmailAddress; supplier.SupplierName = dto.SupplierName; dBContext.SaveChanges(); } } else { var supplier = new Supplier() { CompanyId = dto.CompanyId, ContactNumber = dto.ContactNumber, Address = dto.Address, EmailAddress = dto.EmailAddress, SupplierName = dto.SupplierName }; dBContext.Suppliers.Add(supplier); dBContext.SaveChanges(); } }
public async Task <ActionResult> PostSuppliers([FromBody] AddSupplierDTO addSupplierDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var preSupplier = _mapper.Map <Suppliers>(addSupplierDto); _repo.Add(preSupplier); var saveSupplier = await _repo.SaveAsync(preSupplier); var supplierResponse = _mapper.Map <SupplierResponseDTO>(saveSupplier); return(StatusCode(201, new { supplierResponse })); }
public IActionResult CreateSupplier(AddSuppliersVM addSuppliersVM) { if (!ModelState.IsValid) { ShowToaster("Please fill required fields", ToasterLevel.Danger); return(RedirectToAction("Suppliers", "Company", new { companyId = addSuppliersVM.CompanyId })); } var config = new MapperConfiguration(cfg => cfg.CreateMap <AddSuppliersVM, AddSupplierDTO>()); var mapper = new Mapper(config); AddSupplierDTO dto = mapper.DefaultContext.Mapper.Map <AddSupplierDTO>(addSuppliersVM); _userService.CreateAndUpdateSuppplier(dto); ShowToaster("Supplier created successfully", ToasterLevel.Success); return(RedirectToAction("Suppliers", "Company", new { companyId = dto.CompanyId })); }