public async Task <IActionResult> Create(RequestPD product) { var ProductID = Guid.NewGuid(); product.ID = ProductID.ToString(); var reportFiles = new List <ReportFile>(); if (product.file != null) { var savePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Upload", product.file.FileName); using (var stream = new FileStream(savePath, FileMode.Create)) { await product.file.CopyToAsync(stream); } product.ImgUrl = "/Upload/" + product.file.FileName; } if (product.Reportfile != null) { foreach (var item in product.Reportfile) { var savePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Upload", item.FileName); using (var stream = new FileStream(savePath, FileMode.Create)) { await item.CopyToAsync(stream); } reportFiles.Add(new ReportFile() { ProductID = ProductID, FileName = item.FileName, Path = "/Upload/" + item.FileName }); _context.ReportFile.AddRange(reportFiles); await _context.SaveChangesAsync(); } } try { var target = new Products() { ID = ProductID, BuyUrl = product.BuyUrl, ImgUrl = product.ImgUrl, DownDate = product.DownDate, Information = product.Information, IsPromotion = product.IsPromotion, Name = product.Name, Price = product.Price, PublishDate = product.PublishDate, SalePrice = product.SalePrice.Value, SubTitle = product.SubTitle, Weight = product.Weight, Standard = product.Standard }; _context.Products.Add(target); await _context.SaveChangesAsync(); return(Ok()); } catch (Exception ex) { return(StatusCode(Status400BadRequest, new ResponseMessage { Message = "查無資訊" })); } }
public async Task <IActionResult> Edit(RequestPD product) { var reportFiles = new List <ReportFile>(); var products = _context.Products.Where(r => r.ID == Guid.Parse(product.ID)); if (products.Any()) { try { var data = products.FirstOrDefault(); if (product.file != null) { var savePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Upload", product.file.FileName); using (var stream = new FileStream(savePath, FileMode.Create)) { await product.file.CopyToAsync(stream); } data.ImgUrl = "/Upload/" + product.file.FileName; } if (product.Reportfile != null) { foreach (var item in product.Reportfile) { var savePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Upload", item.FileName); using (var stream = new FileStream(savePath, FileMode.Create)) { await item.CopyToAsync(stream); } reportFiles.Add(new ReportFile() { ProductID = Guid.Parse(product.ID), FileName = item.FileName, Path = "/Upload/" + item.FileName }); } _context.ReportFile.AddRange(reportFiles); _context.SaveChanges(); } data.Name = product.Name; data.Weight = product.Weight; data.Price = product.Price; data.SalePrice = product.SalePrice.Value; data.Information = product.Information; data.PublishDate = product.PublishDate; data.DownDate = product.DownDate; data.BuyUrl = product.BuyUrl; data.Standard = product.Standard; data.SubTitle = product.SubTitle; data.IsPromotion = product.IsPromotion; await _context.SaveChangesAsync(); return(Ok()); } catch (Exception ex) { return(StatusCode(Status400BadRequest, new ResponseMessage { Message = ex.Message })); } } else { return(StatusCode(Status400BadRequest, new ResponseMessage { Message = "查無資訊" })); } }