public IHttpActionResult Create([FromBody] UserProductModel product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } int maxId = _products.Max(x => x.Id); product.Id = (maxId + 1); _products.ToList().Add(product); return(Content(HttpStatusCode.Created, product.Id)); }
/// <summary> /// 用户添加 /// </summary> /// <param name="model"></param> /// <returns></returns> public static bool Add(UserModel model, string[] roleIds) { UserModel.repo.BeginTransaction(); StringBuilder sqlLog = new StringBuilder(); try { if (string.IsNullOrEmpty(model.CreateMan + "")) { model.CreateMan = MTConfig.CurrentUserID.ToInt(); } model.Password = PwdThreeEncrypt.PasswordThreeMd5Encrypt(model.Password); string newUserID = UserModel.repo.Insert(model).ToString(); sqlLog.Append(UserModel.repo.LastCommand + "\n"); if (roleIds != null) { foreach (string roleId in roleIds) { RoleModel roles = new RoleModel(); roles = RoleModel.FirstOrDefault("where id = " + roleId); UserProductModel userProduct = new UserProductModel(); userProduct.ProductId = roles.ProductId; userProduct.UserId = newUserID.ToInt(); userProduct.Insert(); int i = 0; if (int.TryParse(roleId, out i)) { UserRoleModel userRole = new UserRoleModel(); userRole.UserID = newUserID.ToInt(); userRole.RoleID = roleId.ToInt(); userRole.CreateMan = !string.IsNullOrEmpty(model.CreateMan + "") ? model.CreateMan : MTConfig.CurrentUserID.ToInt(); userRole.Insert(); sqlLog.Append(UserModel.repo.LastCommand + "\n"); } } } UserModel.repo.CompleteTransaction(); LogDAL.AppendSQLLog(string.IsNullOrEmpty(model.CreateMan + "") ? MTConfig.CurrentUserID : model.CreateMan + "", "User", sqlLog.ToString()); } catch (Exception) { UserModel.repo.AbortTransaction(); sqlLog = null; return(false); } return(true); }
/// <summary> /// 用户编辑 /// </summary> /// <param name="model"></param> /// <returns></returns> public static bool Edit(UserModel model, string[] roleIds) { UserModel.repo.BeginTransaction(); StringBuilder sqlLog = new StringBuilder(); try { model.CreateMan = MTConfig.CurrentUserID.ToInt(); UserModel.repo.Save(model); sqlLog.Append(UserModel.repo.LastCommand); UserRoleModel.Delete("where UserID = @0", model.ID); sqlLog.Append(UserModel.repo.LastCommand + "\n"); if (roleIds != null) { foreach (string roleId in roleIds) { UserProductModel.Delete(" where UserID = @0", model.ID); RoleModel roles = new RoleModel(); roles = RoleModel.FirstOrDefault("where id = " + roleId); UserProductModel userProduct = new UserProductModel(); userProduct.ProductId = roles.ProductId; userProduct.UserId = model.ID.ToInt(); userProduct.Insert(); int i = 0; if (int.TryParse(roleId, out i)) { UserRoleModel userRole = new UserRoleModel(); userRole.UserID = model.ID.ToInt(); userRole.RoleID = roleId.ToInt(); userRole.CreateMan = MTConfig.CurrentUserID.ToInt(); userRole.Insert(); sqlLog.Append(UserModel.repo.LastCommand + "\n"); } } } UserModel.repo.CompleteTransaction(); LogDAL.AppendSQLLog(MTConfig.CurrentUserID, "User", sqlLog.ToString()); } catch (Exception) { UserModel.repo.AbortTransaction(); sqlLog = null; return(false); } return(true); }
private int ParseFileAndReturnNumberOfLine(HttpPostedFileBase file) { var numberOfLine = 0; using (var package = new ExcelPackage(file.InputStream)) { var workBook = package.Workbook; if (workBook != null) { if (workBook.Worksheets.Count > 0) { var currentWorksheet = workBook.Worksheets.First(); var startRow = 2; for (int rowNumber = startRow; rowNumber <= currentWorksheet.Dimension.End.Row; rowNumber++) { var products = new UserProductModel(); int columnIndex = 1; if (currentWorksheet.Cells[rowNumber, columnIndex].Value != null) { products.ProductName = currentWorksheet.Cells[rowNumber, columnIndex].Value.ToString(); } columnIndex++; if (currentWorksheet.Cells[rowNumber, columnIndex].Value != null) { products.Category = currentWorksheet.Cells[rowNumber, columnIndex].Value.ToString(); } columnIndex++; if (currentWorksheet.Cells[rowNumber, columnIndex].Value != null) { decimal price = 0.0M; decimal.TryParse(currentWorksheet.Cells[rowNumber, columnIndex].Value.ToString(), out price); products.Price = price; } columnIndex++; if (currentWorksheet.Cells[rowNumber, columnIndex].Value != null) { products.Model = currentWorksheet.Cells[rowNumber, columnIndex].Value.ToString(); } numberOfLine++; } } } return(numberOfLine); } }
private static byte[] GenerateTemplateExcel() { byte[] str; List <UserProductModel> products = new UserProductModel[] { new UserProductModel { Id = 1, ProductName = "Yamaha", Category = "Moto", Price = 12000, Model = "MT09 SP" }, new UserProductModel { Id = 2, ProductName = "BMW", Category = "Moto", Price = 21000, Model = "S1000R" }, new UserProductModel { Id = 3, ProductName = "Honda", Category = "Moto", Price = 13000, Model = "Hornet 1000" }, new UserProductModel { Id = 4, ProductName = "Yamaha", Category = "Moto", Price = 8999, Model = "MT07" }, new UserProductModel { Id = 5, ProductName = "BMW", Category = "Moto", Price = 23000, Model = "S1000RR" }, new UserProductModel { Id = 6, ProductName = "Honda", Category = "Moto", Price = 17500, Model = "CB 1000 F" } }.ToList(); int i = 1; using (var xls = new ExcelPackage()) { var sheet = xls.Workbook.Worksheets.Add("Products"); sheet.Cells[1, 1].Value = "Name"; sheet.Cells[1, 2].Value = "Category"; sheet.Cells[1, 3].Value = "Price"; sheet.Cells[1, 4].Value = "Model"; foreach (var item in products) { i++; sheet.Cells[i, 1].Value = item.ProductName; sheet.Cells[i, 2].Value = item.Category; sheet.Cells[i, 3].Value = item.Price.ToString("#.##"); sheet.Cells[i, 4].Value = item.Model; } str = xls.GetAsByteArray(); } return(str); }
public ActionResult Edit(int?id, int actiontype = 0) { UserProductViewModel userpor = new UserProductViewModel(); ViewBag.actiontype = actiontype; if (actiontype == 1) { ViewData[EditFlag] = true; userpor.ProductList = ProductModel.Fetch(""); userpor.RoleList = RoleModel.Fetch(""); return(View(userpor)); } userpor.ProductList = ProductModel.Fetch(""); userpor.UserProductList = UserProductModel.Fetch(" where userid = " + id); userpor.RoleList = RoleModel.Fetch(""); userpor.UserRoleList = UserRoleModel.Fetch(" where userid = " + id); userpor.User = UserModel.FirstOrDefault(" where id = " + id); ViewData[EditFlag] = true; return(View(userpor)); }
public IHttpActionResult Update([FromBody] UserProductModel product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //TODO GetbyId if (!_products.ToList().Exists(x => x.Id == product.Id)) { return(NotFound()); } var productToUpdate = _products.ToList().Find(x => x.Id == product.Id); productToUpdate.ProductName = product.ProductName; productToUpdate.Price = product.Price; productToUpdate.Category = product.Category; productToUpdate.Model = product.Model; _products.ToList().RemoveAll(x => x.Id == product.Id); _products.ToList().Add(product); return(Ok()); }