コード例 #1
0
 public ActionResult GetProductDetail(string productCode)
 {
     var product = ProductManagement.GetProduct(productCode);
     var productDetail = new EditProductDTO
         {
             Id = product.Id,
             Name = product.Name,
             CategoryId = product.CategoryId,
             Code = product.Code,
             Price = Convert.ToDecimal(product.Price),
             IsCreating = false,
             Hidden = product.Hidden
         };
     return Json(new { productDetail }, JsonRequestBehavior.AllowGet);
 }
コード例 #2
0
        public static void CreateProduct(EditProductDTO dto)
        {
            SqlTransaction transaction = null;
            try
            {
                var con = new SqlConnection(GetConnection());
                con.Open();
                transaction = con.BeginTransaction();
                var cmd = new SqlCommand
                {
                    Transaction = transaction,
                    Connection = con,
                    CommandType = CommandType.Text,
                    CommandText =
                        @"INSERT INTO [finplanweb].[dbo].[products]
                           ([productCode]
                           ,[description]
                           ,[addedDate]
                           ,[modifiedDate]
                           ,[price]
                           ,[categoriesID]
                           ,[hidden])" +
                        "VALUES (@code, @name, @addedDate, @modifiedDate, @price, @categoryID,0)"
                };
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@code", dto.Code);
                cmd.Parameters.AddWithValue("@name", dto.Name);
                cmd.Parameters.AddWithValue("@addedDate", DateTime.Now);
                cmd.Parameters.AddWithValue("@modifiedDate", DBNull.Value);
                cmd.Parameters.AddWithValue("@price", dto.Price);
                cmd.Parameters.AddWithValue("@categoryID", dto.CategoryId);
                cmd.ExecuteNonQuery();
                transaction.Commit();

                if (con.State != ConnectionState.Closed) return;
                con.Close();
            }
            catch (SqlException ex)
            {
                transaction.Rollback();
                var msg = "Insert errors";
                msg += ex.Message;
                throw new Exception(msg);
            }
        }
コード例 #3
0
        public ActionResult CreateUpdateProduct(EditProductDTO dto)
        {
            if (dto == null)
            {
                throw new NullReferenceException("DTO cannot be null");
            }
            var validationIds = new List<string>();
            var validationMessage = Validate(dto, out validationIds);
            if (!validationMessage.Any())
            {
                if (dto.IsCreating)
                {
                    ProductManagement.CreateProduct(dto);
                }
                else
                {
                    ProductManagement.UpdateProduct(dto);
                }

                var products = ProductManagement.GetProductsIncludeHidden();
                var categories = CategoryManagement.GetAllCategory();
                var totalProductPage = (int)Math.Ceiling(((double)products.Count() / (double)pageSize));
                return Json(new
                {
                    products = ApplyPaging(products, 1).Select(x => ConvertToProductDTO(x, categories)),
                    passed = !validationMessage.Any(),
                    validationIds,
                    validationMessage = string.Join("</br>", validationMessage),
                    totalProductPage
                });
            }

            return Json(new
            {
                passed = !validationMessage.Any(),
                validationIds,
                validationMessage = string.Join("</br>", validationMessage)
            });
        }
コード例 #4
0
        public List<string> Validate(EditProductDTO product, out List<string> invalidIds)
        {
            var validationMessage = new List<string>();
            var validationId = new List<string>();

            if (product.IsCreating && string.IsNullOrEmpty(product.Code))
            {
                validationMessage.Add("Code is empty.");
                validationId.Add("Code");
            }

            if (product.CategoryId == default(int))
            {
                validationMessage.Add("Category is empty.");
                validationId.Add("Category");
            }

            if (string.IsNullOrEmpty(product.Name))
            {
                validationMessage.Add("Name is empty.");
                validationId.Add("Name");
            }

            if (product.Price == default(decimal))
            {
                validationMessage.Add("Price is empty.");
                validationId.Add("Price");
            }

            invalidIds = validationId;
            return validationMessage;
        }
コード例 #5
0
 public static void UpdateProduct(EditProductDTO product)
 {
     using (var connection = new SqlConnection(GetConnection()))
     {
         const string sql = @"UPDATE [dbo].[products] SET Description=@d, modifiedDate=@md, price=@p, categoriesID=@cid, hidden=@hide WHERE [productId] = @pid";
         connection.Open();
         var cmd = new SqlCommand(sql, connection);
         cmd.Parameters.Add(new SqlParameter("@d", SqlDbType.NVarChar)).Value = product.Name;
         cmd.Parameters.Add(new SqlParameter("@md", SqlDbType.DateTime)).Value = DateTime.Now;
         cmd.Parameters.Add(new SqlParameter("@p", SqlDbType.Money)).Value = product.Price;
         cmd.Parameters.Add(new SqlParameter("@cid", SqlDbType.Int)).Value = product.CategoryId;
         cmd.Parameters.Add(new SqlParameter("@hide", SqlDbType.Bit)).Value = product.Hidden;
         cmd.Parameters.Add(new SqlParameter("@pid", SqlDbType.Int)).Value = product.Id;
         cmd.ExecuteNonQuery();
     }
 }