public bool UpdateCategoryById(CategoryInput categoryInput, out string msg)
        {
            IQueryable <Categorylnfo> categorylnfos = _dbContext.Set <Categorylnfo>();
            Categorylnfo categorylnfo = categorylnfos.Where(c => c.CategoryId == categoryInput.CategoryId).FirstOrDefault();

            if (categorylnfo != null)
            {
                try
                {
                    Categorylnfo categorylnfo1 = categorylnfos.Where(c => c.CategoryName == categoryInput.CategoryName).FirstOrDefault();
                    if (categorylnfo1 == null || categoryInput.CategoryName == categorylnfo.CategoryName)
                    {
                        categorylnfo.CategoryName = categoryInput.CategoryName;
                        categorylnfo.CurrentPrice = categoryInput.CurrentPrice;
                        if (_dbContext.SaveChanges() > 0)
                        {
                            msg = "修改成功!";
                            return(true);
                        }
                        msg = "错误!内部出现异常!";
                        return(false);
                    }
                    msg = "该类名已存在!";
                    return(false);
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                    return(false);
                }
            }
            msg = "找不到该类目数据!数据可能被篡改!";
            return(false);
        }
        public CategoryOutput GetCategory(int id)
        {
            Categorylnfo categorylnfo = _dbContext.Set <Categorylnfo>().Find(id);

            if (categorylnfo != null)
            {
                CategoryOutput output = new CategoryOutput
                {
                    CategoryId   = categorylnfo.CategoryId,
                    CategoryName = categorylnfo.CategoryName,
                    CurrentPrice = categorylnfo.CurrentPrice,
                    Unit         = categorylnfo.Unit,
                    AddTime      = categorylnfo.AddTime
                };
                return(output);
            }
            return(null);
        }
        public bool AddCategory(CategoryInput categoryInput, out string msg)
        {
            IQueryable <Categorylnfo> categorylnfos = _dbContext.Set <Categorylnfo>();
            Categorylnfo categorylnfo = categorylnfos.Where(c => c.CategoryId == categoryInput.CategoryId).FirstOrDefault();

            if (categorylnfo == null)
            {
                Categorylnfo categorylnfo1 = categorylnfos.Where(c => c.CategoryName == categoryInput.CategoryName).FirstOrDefault();
                if (categorylnfo1 == null)
                {
                    try
                    {
                        Categorylnfo newCategory = new Categorylnfo
                        {
                            CategoryId   = categoryInput.CategoryId,
                            CategoryName = categoryInput.CategoryName,
                            CurrentPrice = categoryInput.CurrentPrice,
                            Unit         = categoryInput.Unit,
                            DelFlag      = false,
                            AddTime      = DateTime.Now
                        };
                        _dbContext.Set <Categorylnfo>().Add(newCategory);
                        if (_dbContext.SaveChanges() > 0)
                        {
                            msg = "添加成功!";
                            return(true);
                        }
                        msg = "失败!内部出现异常!";
                        return(false);
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        return(false);
                    }
                }
                msg = "该类名已存在!";
                return(false);
            }
            msg = "类目编号已存在!";
            return(false);
        }
        public bool BanCategory(int id, out string msg)
        {
            Categorylnfo categorylnfo = _dbContext.Set <Categorylnfo>().Find(id);

            if (categorylnfo != null)
            {
                try
                {
                    if (categorylnfo.DelFlag == false)
                    {
                        categorylnfo.DelFlag = true;
                        if (_dbContext.SaveChanges() > 0)
                        {
                            msg = "已禁用";
                            return(true);
                        }
                        msg = "错误!内部出现异常!";
                        return(false);
                    }
                    categorylnfo.DelFlag = false;
                    if (_dbContext.SaveChanges() > 0)
                    {
                        msg = "已开启";
                        return(true);
                    }
                    msg = "错误!内部出现异常!";
                    return(false);
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                    return(false);
                }
            }
            msg = "找不到该类目信息!数据可能被篡改!";
            return(false);
        }
Esempio n. 5
0
        /// <summary>
        /// 获取所有图书分类
        /// </summary>
        /// <returns></returns>

        public static List <Categorylnfo> GetAllCategoryList()
        {
            List <Categorylnfo> list = new List <Categorylnfo>();
            string strConn           = ConfigurationManager.ConnectionStrings["BookShopConnectionString"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(strConn))
            {
                conn.Open();
                string         sql = "select*from Categories order by Id desc";
                DataSet        ds  = new DataSet();
                SqlDataAdapter sa  = new SqlDataAdapter(sql, conn);
                sa.Fill(ds);

                //转对象集合
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Categorylnfo c = new Categorylnfo();
                    c.Id   = Convert.ToInt32(dr["Id"].ToString());
                    c.Name = dr["Name"].ToString();
                    list.Add(c);
                }
                return(list);
            }
        }