public async Task <IActionResult> DemandCreate(DemandCreateDto demandCreate) { if (!ModelState.IsValid) { ViewBag.DemandTypes = await _demandService.GetListAsync(); return(View("Index", new CreateDto { DemandCreate = demandCreate })); } demandCreate.FilePath = Path.Combine(_webHostEnvironment.WebRootPath, _fileUploadSettings.PhotoPath); IResult result = await _demandService.CreateDemandAsync(demandCreate); if (!result.IsSuccess) { ViewBag.DemandTypes = await _demandService.GetListAsync(); ModelState.AddModelError("SaveError", result.Message); return(View("Index", new CreateDto { DemandCreate = demandCreate })); } return(RedirectToAction("Index", "Demand")); }
public async Task <IResult> CreateDemandAsync(DemandCreateDto demandCreateDto) { string imageName = demandCreateDto.Image != null?Guid.NewGuid().ToString() + "." + demandCreateDto.Image.FileName.Split('.')[1] : ""; _demandDAL.Add(new Demand { DemandTypeId = demandCreateDto.DemandTypeId, ImageName = $"{imageName}", Name = demandCreateDto.Name, Price = demandCreateDto.Price }); int result = await _uow.Complete(); if (result == 0) { return(new ErrorResult(CRUDMessages.CreateMessage)); } if (demandCreateDto.Image != null) { var fileLocate = $"{demandCreateDto.FilePath}/{imageName}"; using (var stream = new FileStream(fileLocate, FileMode.Create)) { demandCreateDto.Image.CopyTo(stream); } } return(new SuccessResult()); }
public async Task <ActionResult> CreateDemond([FromBody] DemandCreateDto demand) { int id = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var logineduser = await _auth.VerifyUser(id); var num = String.Format("{0:d9}", (DateTime.Now.Ticks / 10) % 1000000000); Demand _newDemand = new Demand() { Name = demand.Name, DemandNumber = num, Company = logineduser.Company, CreateDate = DateTime.Now, CheckStatus = 1, Status = true, Created = logineduser }; _context.Demands.Add(_newDemand); await _context.SaveChangesAsync(); foreach (var item in demand.DemandProduct) { DemandProduct _newDemanProduct = new DemandProduct() { Demand = _newDemand, Product = await _context.Products.FirstOrDefaultAsync(s => s.Id == item.ProductId), Quantity = item.Quantity, Parcel = await _context.Parcels.FirstOrDefaultAsync(s => s.Id == item.ParcelId), Country = await _context.Country.FirstOrDefaultAsync(s => s.Id == item.CountryId), Workers = await _context.Workers.FirstOrDefaultAsync(s => s.Id == item.RequestingWorkerId), ExpirationDate = item.ExpirationDate, RequiredDate = item.RequiredDate }; _context.DemandProducts.Add(_newDemanProduct); await _context.SaveChangesAsync(); } return(StatusCode(201)); }