public JsonResult SaveFile(UsableProductViewModel model) { Response response; List <string> excludeFileName = new List <string>(); var user = GetAuthenticatedUser(); try { using (var db = new KiaGalleryContext()) { var entity = db.UsableProduct.Single(x => x.Id == model.id); if (model.fileList != null && model.fileList.Count > 0) { var fileNameList = model.fileList.Select(x => x.fileName).ToList(); // جدا کردن نام فایل های برای جستوجو در LINQ var excludeFile = entity.UsableProductFileList.Where(x => !fileNameList.Any(y => y == x.FileName)).ToList(); // پیدا کردن فایل های حذف شده excludeFileName = excludeFile.Select(x => x.FileName).ToList(); // جدا کردن نام فایل های حذف شده برای پاک کردن فایل فیزیکی excludeFile.ForEach(x => db.UsableProductFile.Remove(x)); // حذف فایل های حذف شده از دیتابیس model.fileList.ForEach(file => { var item = entity.UsableProductFileList.SingleOrDefault(x => x.FileName == file.fileName); // بررسی عدم وجود تصویر در صورت اضافه شدن تصویر و ذخیره در دیتابیس if (item == null) { var fileItem = new UsableProductFile() { UsableProduct = entity, FileName = file.fileName, CreateUserId = user.Id, ModifyUserId = user.Id, CreateDate = DateTime.Now, ModifyDate = DateTime.Now, Ip = Request.UserHostAddress }; db.UsableProductFile.Add(fileItem); } }); } else { db.UsableProductFile.RemoveRange(entity.UsableProductFileList); } db.SaveChanges(); } response = new Response() { status = 200, message = "عکس محصول با موفقیت ذخیره شد." }; } catch (Exception ex) { response = Core.GetExceptionResponse(ex); } return(Json(response, JsonRequestBehavior.AllowGet)); }
public JsonResult Load(int id) { Response response; try { UsableProduct item; using (var db = new KiaGalleryContext()) { item = db.UsableProduct.FirstOrDefault(x => x.Id == id); var data = new UsableProductViewModel { id = item.Id, categoryUsableProductId = item.CategoryUsableProduct.ParentId.Value, categoryChild = item.CategoryUsableProductId, printingHouseId = item.PrintingHouseId, code = item.Code, name = item.Name, order = item.Order, usableProductStatus = item.UsableProductStatus, unit = item.Unit, description = item.Description, //available = item.Available, }; response = new Response { status = 200, data = data, }; } } catch (Exception ex) { response = Core.GetExceptionResponse(ex); } return(Json(response, JsonRequestBehavior.AllowGet)); }
public JsonResult Save(UsableProductViewModel model) { Response response; try { using (var db = new KiaGalleryContext()) { List <string> excludeFileName = new List <string>(); var userId = GetAuthenticatedUserId(); if (model.id != null && model.id > 0) { var product = db.UsableProduct.Single(x => x.Id == model.id); product.CategoryUsableProductId = model.categoryChild; product.Name = model.name; product.PrintingHouseId = model.printingHouseId; product.Code = model.code; product.Unit = model.unit; product.Description = model.description; //product.Available = model.available; product.Order = model.order; product.UsableProductStatus = model.usableProductStatus; product.ModifyUserId = userId; product.ModifyDate = DateTime.Now; product.Ip = Request.UserHostAddress; } else { var item = new UsableProduct() { CategoryUsableProductId = model.categoryChild, PrintingHouseId = model.printingHouseId, Name = model.name, Code = model.code, Unit = model.unit, Description = model.description, //Available = model.available, Order = model.order, UsableProductStatus = model.usableProductStatus, CreateUserId = userId, ModifyUserId = userId, CreateDate = DateTime.Now, ModifyDate = DateTime.Now, Ip = Request.UserHostAddress, }; db.UsableProduct.Add(item); } db.SaveChanges(); } response = new Response() { status = 200, message = "محصول جدید ثبت شد", }; } catch (Exception ex) { response = Core.GetExceptionResponse(ex); } return(Json(response, JsonRequestBehavior.AllowGet)); }
public JsonResult GetUsableProductList(UsableProductViewModel model) { Response response; int dataCount; try { using (var db = new KiaGalleryContext()) { var user = GetAuthenticatedUser(); var query = db.UsableProduct.Where(x => x.UsableProductStatus == UsableProductStatus.Active || x.UsableProductStatus == UsableProductStatus.DisabledVisible).Select(x => x); query = query.Where(x => x.PrintingHouse.Active == true); if (!string.IsNullOrEmpty(model.term?.Trim())) { query = query.Where(x => x.Code.ToString().Contains(model.term.Trim()) || x.Name.Contains(model.term.Trim())); } if (model.categoryId != null && model.categoryId > 0) { query = query.Where(x => x.CategoryUsableProduct.ParentId == model.categoryId); } if (model.categoryChildId != null && model.categoryChildId.Count > 0) { query = query.Where(x => model.categoryChildId.Contains(x.CategoryUsableProductId)); } if (user.PrintingHouseId > 0 && user.PrintingHouseId != null) { query = query.Where(x => x.PrintingHouseId == user.PrintingHouseId); } query = query.OrderBy(x => x.CategoryUsableProduct.Parent.Order).ThenBy(x => x.CategoryUsableProduct.Order).ThenByDescending(x => x.CreateDate); dataCount = query.Count(); var list = query.Select(x => new UsableProductViewModel() { id = x.Id, name = x.Name, code = x.Code, description = x.Description, usableProductStatus = x.UsableProductStatus, unit = x.Unit, count = x.UsableProductCartList.Where(y => y.BranchId == user.BranchId).Select(y => y.Count).FirstOrDefault(), orderId = x.UsableProductCartList.Where(y => y.BranchId == user.BranchId).Select(y => y.Id).FirstOrDefault(), firstFileName = x.UsableProductFileList.OrderBy(y => y.Id).FirstOrDefault().FileName, secondFileName = x.UsableProductFileList.OrderByDescending(y => y.Id).FirstOrDefault().FileName, fileList = x.UsableProductFileList.Select(y => new UsableProductSearchViewModel() { id = y.Id, fileId = y.FileId, fileName = y.FileName, }).ToList(), }).ToList(); list.ForEach(x => { x.usableProductStatusTitle = Enums.GetTitle(x.usableProductStatus); }); response = new Response() { status = 200, data = new { list = list, } }; } } catch (Exception ex) { response = Core.GetExceptionResponse(ex); } return(Json(response, JsonRequestBehavior.AllowGet)); }