Beispiel #1
0
        /// <summary>
        /// 资源文件地址 有关需要改变文件目录的时候需要改变的地方
        /// </summary>
        /// <param name="cId"></param>
        /// <returns></returns>
        public IActionResult Delete([Required] int cId)
        {
            if (ModelState.IsValid)
            {
                if (!_analysis.GetLoginUserConfig(HttpContext).Power.CourcesManager)
                {
                    return(Json(new
                    {
                        isOk = false,
                        title = "错误提示",
                        message = "你并无课程管理操作权限"
                    }));
                }

                Cource c = _context.Cources.Find(cId);

                if (c != null)
                {
                    //先把资源文件删除掉
                    List <Resource> resources = _context.Resources.Where(r => r.CourceId == cId && r.ResourceType == ResourceType.Vedio).ToList();
                    foreach (var res in resources)
                    {
                        String path = Path.GetFullPath($@"{_hosting.WebRootPath}/video/{res.ResourceUrl}");
                        if (System.IO.File.Exists(path))
                        {
                            System.IO.File.Delete(path);
                        }
                    }
                    _context.RemoveRange(_context.Resources.Where(r => r.CourceId == cId));
                    _context.Cources.Remove(c);
                    _context.SaveChanges();
                    return(Json(new
                    {
                        isOk = false,
                        title = "消息提示",
                        message = "删除成功!"
                    }));
                }
                else
                {
                    return(Json(new
                    {
                        isOk = false,
                        title = "错误提示",
                        message = "课程不存在,或者已经被删除"
                    }));
                }
            }
            else
            {
                return(Json(new
                {
                    isOk = false,
                    error = _analysis.ModelStateDictionaryError(ModelState),
                    title = "错误提示",
                    message = "参数错误,传递了不符合规定的参数"
                }));
            }
        }