public ActionResult AddWatch([Bind(Include = "WatchDescription, WatchCode, Quantity, Price, MovementID, ModelId, BandMaterial, CaseRadius, CaseMaterial, Discount, Guarantee")] Watch watch, HttpPostedFileBase thumbnail) { if (ModelState.IsValid) { watch.WaterResistant = Request["water"] == "yes"; watch.LEDLight = Request["led"] == "yes"; watch.Alarm = Request["alarm"] == "yes"; watch.WatchCode = watch.WatchCode.ToUpper(); bool duplicateCode = watchService.IsDuplicatedWatchCode(watch.WatchCode); bool validImage = FileTypeDetector.IsImageFile(thumbnail); //check if watch code is unique if (!validImage || duplicateCode) { //code đã tồn tại hoặc hình ảnh không hợp lệ //thông báo điền lại code //prefill các field var movement = movementService.GetMovementList(); var watchModel = watchModelService.GetModelsList(); var viewModel = watchService.PrepopulateInputValue(watch, movement, watchModel); if (duplicateCode) { viewModel.DuplicateErrorMessage = "Watch with code '" + watch.WatchCode + "' already existed"; } if (!validImage) { viewModel.InvalidImageFileMessage = "Invalid Thumbnail. Upload file is not an image"; } return(View("~/Views/Admin/admin_manage_watch_add.cshtml", viewModel)); } //lưu hình ảnh xuống máy string path = HostingEnvironment.MapPath("~/Content/img/ProductThumbnail/") + watch.WatchCode + DateTime.Now.ToBinary(); //thumbnail.InputStream.Position = 0; ImageProcessHelper.ResizedImage(thumbnail.InputStream, 360, 500, ResizeMode.Pad, ref path); watch.Thumbnail = path; watch.PublishedTime = DateTime.Now; watch.PublishedBy = Session.GetCurrentUserInfo("Username"); watch.Status = true; if (watchService.AddNewWatch(watch)) { TempData["SHOW_MODAL"] = @"<script>$('#successModal').modal();</script>"; return(RedirectToAction("AddWatch", "Admin")); } return(Content("Unexpected Error")); } return(RedirectToAction("NotFound", "Home")); }
public bool UpdateWatchInfo(Watch watch, HttpPostedFileBase thumbnail) { Watch trackedWatch = db.Watches.Find(watch.WatchID); db.Watches.Attach(trackedWatch); trackedWatch.WatchCode = watch.WatchCode; trackedWatch.WatchDescription = watch.WatchDescription; trackedWatch.Quantity = watch.Quantity; trackedWatch.Price = watch.Price; trackedWatch.MovementID = watch.MovementID; trackedWatch.ModelID = watch.ModelID; trackedWatch.WaterResistant = watch.WaterResistant; trackedWatch.BandMaterial = watch.BandMaterial; trackedWatch.CaseRadius = watch.CaseRadius; trackedWatch.CaseMaterial = watch.CaseMaterial; trackedWatch.Discount = watch.Discount; trackedWatch.LEDLight = watch.LEDLight; trackedWatch.Alarm = watch.Alarm; trackedWatch.Status = watch.Status; trackedWatch.Guarantee = watch.Guarantee; if (thumbnail != null) { //đổi ảnh thumbnail //lưu hình ảnh xuống máy string path = HostingEnvironment.MapPath("~/Content/img/ProductThumbnail/") + watch.WatchCode + DateTime.Now.ToBinary(); ImageProcessHelper.ResizedImage(thumbnail.InputStream, 360, 500, ResizeMode.Pad, ref path); trackedWatch.Thumbnail = path; } int result = db.SaveChanges(); if (db.Entry(trackedWatch).State == EntityState.Unchanged || result > 0) { return(true); } return(false); }
public int ImportWatchFromFile(HttpPostedFileBase excel, HttpPostedFileBase zip, string username) { int totalImported = 0; using (ExcelPackage package = new ExcelPackage(excel.InputStream)) using (ZipArchive archive = new ZipArchive(zip.InputStream, ZipArchiveMode.Read)) { ExcelWorksheet workSheet = package.Workbook.Worksheets[1]; int startRow = workSheet.Dimension.Start.Row; int endRow = workSheet.Dimension.End.Row; int startCol = workSheet.Dimension.Start.Column; int endCol = workSheet.Dimension.End.Column; string watchCodeColumn, watchDescriptionColumn, quantityColumn, priceColumn, movementIdColumn, modelIdColumn, waterResistantColumn, bandMaterialColumn, caseRadiusColumn, caseMaterialColumn, discountColumn, ledLightColumn, guaranteeColumn, alarmColumn; using (var headers = workSheet.Cells[startRow, startRow, startCol, endCol]) { watchCodeColumn = headers.First(h => h.Value.Equals("WatchCode")).Address[0].ToString(); watchDescriptionColumn = headers.First(h => h.Value.Equals("WatchDescription")).Address[0].ToString(); priceColumn = headers.First(h => h.Value.Equals("Price")).Address[0].ToString(); quantityColumn = headers.First(h => h.Value.Equals("Quantity")).Address[0].ToString(); movementIdColumn = headers.First(h => h.Value.Equals("MovementID")).Address[0].ToString(); modelIdColumn = headers.First(h => h.Value.Equals("ModelID")).Address[0].ToString(); waterResistantColumn = headers.First(h => h.Value.Equals("WaterResistant")).Address[0].ToString(); bandMaterialColumn = headers.First(h => h.Value.Equals("BandMaterial")).Address[0].ToString(); caseRadiusColumn = headers.First(h => h.Value.Equals("CaseRadius")).Address[0].ToString(); caseMaterialColumn = headers.First(h => h.Value.Equals("CaseMaterial")).Address[0].ToString(); discountColumn = headers.First(h => h.Value.Equals("Discount")).Address[0].ToString(); ledLightColumn = headers.First(h => h.Value.Equals("LEDLight")).Address[0].ToString(); guaranteeColumn = headers.First(h => h.Value.Equals("Guarantee")).Address[0].ToString(); alarmColumn = headers.First(h => h.Value.Equals("Alarm")).Address[0].ToString(); } for (int row = startRow + 1; row <= endRow; row++) { try { string watchCode = workSheet.Cells[watchCodeColumn + row].Value.ToString().ToUpper().Trim(); if (Regex.Match(watchCode, "^[A-Za-z0-9-]{1,}$").Success&& !IsDuplicatedWatchCode(watchCode)) { //validate code pattern ////Duplicate code found. Skip add. Otherwise will continue to check ZipArchiveEntry entry = archive.Entries.FirstOrDefault(e => e.Name.Contains(watchCode)); //No Thumbnail match watchcode. Skip add. Otherwise will continue if (entry != null) { Watch watch = new Watch { WatchCode = watchCode, WatchDescription = workSheet.Cells[watchDescriptionColumn + row].Value == null ? null : workSheet.Cells[watchDescriptionColumn + row].Value.ToString().Trim(), BandMaterial = workSheet.Cells[bandMaterialColumn + row].Value == null ? null : workSheet.Cells[bandMaterialColumn + row].Value.ToString().Trim(), CaseMaterial = workSheet.Cells[caseMaterialColumn + row].Value == null ? null : workSheet.Cells[caseMaterialColumn + row].Value.ToString().Trim(), Quantity = Int32.Parse(workSheet.Cells[quantityColumn + row].Value.ToString().Trim()), Price = Double.Parse(workSheet.Cells[priceColumn + row].Value.ToString().Trim()), MovementID = Int32.Parse(workSheet.Cells[movementIdColumn + row].Value.ToString().Trim()), ModelID = Int32.Parse(workSheet.Cells[modelIdColumn + row].Value.ToString().Trim()), CaseRadius = Double.Parse(workSheet.Cells[caseRadiusColumn + row].Value.ToString().Trim()), Discount = Int32.Parse(workSheet.Cells[discountColumn + row].Value.ToString().Trim()), Guarantee = Int32.Parse(workSheet.Cells[guaranteeColumn + row].Value.ToString().Trim()), WaterResistant = workSheet.Cells[waterResistantColumn + row].Value.ToString().Trim().Equals("true", StringComparison.OrdinalIgnoreCase), LEDLight = workSheet.Cells[ledLightColumn + row].Value.ToString().Trim().Equals("true", StringComparison.OrdinalIgnoreCase), Alarm = workSheet.Cells[alarmColumn + row].Value.ToString().Trim().Equals("true", StringComparison.OrdinalIgnoreCase), }; if (watch.Quantity >= 0 && watch.Price >= 0 && watch.CaseRadius.GetValueOrDefault() >= 0 && watch.Discount >= 0 && watch.Guarantee >= 0) { string path = HostingEnvironment.MapPath("~/Content/img/ProductThumbnail/") + watchCode + DateTime.Now.ToBinary(); ImageProcessHelper.ResizedImage(entry.Open(), 360, 500, ResizeMode.Pad, ref path); watch.Thumbnail = path; watch.PublishedTime = DateTime.Now; watch.PublishedBy = username; watch.Status = true; db.Watches.Add(watch); db.SaveChanges(); totalImported++; } } } } catch (Exception) { } } } return(totalImported); }