(九)控制台应用编写

(一)前言

其实控制台,需要先把相关的接口实例,在后续的操作中才能更好的使用。这也是.NET core 或者是.NET 5以上版本的开发形式。看代码吧!

(二)BlogNewsController控制台

/// <summary>
/// 构造函数
/// </summary>
private readonly IBlogNewsService _blogNewsService;

/// <summary>
/// 文章接口实例
/// </summary>
/// <param name="iblogNewsService"></param>
public BlogNewsController(IBlogNewsService iblogNewsService)
{
    this._blogNewsService = iblogNewsService;
}

/// <summary>
/// 查询全部文章
/// </summary>
/// <returns></returns>
[HttpGet("BlogNews")]
public async Task<ActionResult<ApiResult>> GetBlogNews()
{
    var data = await _blogNewsService.QueryAsync();
    if (data == null)
    {
       return ApiResultHelper.Error("查询不到相关的是数据!");
    }
    else
    {
        return ApiResultHelper.Success(data);
    }
}

/// <summary>
/// 添加文章
/// </summary>
/// <param name="title"></param>
/// <param name="content"></param>
/// <param name="typeid"></param>
/// <returns></returns>
[HttpPost("Create")]
public async Task<ActionResult<ApiResult>> GetCreate(string title, string content, int typeid)
{
    //数据验证
    BlogNews blogNews = new BlogNews
    {
        BrowseCount = 0,
        Title = title,
        Coutent = content,
        Time = DateTime.Now,
        TypeId = typeid,
        WriteID = 1
     };
     bool b = await _blogNewsService.CreateAsync(blogNews);

     if (!b)
     {
         return ApiResultHelper.Error("添加失败,服务器发生错误!");
     }
     else
     {
         return ApiResultHelper.Success(blogNews);
     }
}

/// <summary>
/// 删除文章
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpDelete("Delete")]
public async Task<ActionResult<ApiResult>> GetDelete(int id)
{
    bool b = await _blogNewsService.DeleteAsync(id);
    if (!b)
    {
        return ApiResultHelper.Error("添加失败,服务器发生错误!");
    }
    else
    {
         return ApiResultHelper.Success(b);
    }
}

/// <summary>
/// 修改文章
/// </summary>
/// <param name="id"></param>
/// <param name="title"></param>
/// <param name="content"></param>
/// <param name="typeid"></param>
/// <returns></returns>
[HttpPut("Edit")]
public async Task<ActionResult<ApiResult>> GetEdit(int id, string title, string content, int typeid)
{
     var blogNews = await _blogNewsService.FindAsync(id);
     if (blogNews == null)
     {
         return ApiResultHelper.Error("没有找到该文章");
      }
      blogNews.Title = title;
      blogNews.Coutent = content;
      blogNews.TypeId = typeid;

      bool b = await _blogNewsService.EditAsync(blogNews);
      if (!b)
      {
          return ApiResultHelper.Error("修改失败!");
      }
      else
      {
           return ApiResultHelper.Success(blogNews);
      }

}

(三)TypeController控制台

        /// <summary>
        /// 构造函数
        /// </summary>
        private readonly ITypeInfoService _iTypeInfoService;

        /// <summary>
        /// 文章类型接口实例
        /// </summary>
        /// <param name="iTypeInfoService"></param>
        public TypeController(ITypeInfoService iTypeInfoService)
        {
           this._iTypeInfoService = iTypeInfoService;/// <summary>
        /// 构造函数
        /// </summary>
        private readonly ITypeInfoService _iTypeInfoService;

        /// <summary>
        /// 文章类型接口实例
        /// </summary>
        /// <param name="iTypeInfoService"></param>
        public TypeController(ITypeInfoService iTypeInfoService)
        {
           this._iTypeInfoService = iTypeInfoService;
        }

        /// <summary>
        /// 查询全部文章类型
        /// </summary>
        /// <returns></returns>
        [HttpGet("Types")]
        public async Task<ApiResult> GetTypes()
        {
            var Types = await _iTypeInfoService.QueryAsync();
            if (Types.Count==0)
            {
                return ApiResultHelper.Error("查询不到相关的是数据!");
            }
            else
            {
                return ApiResultHelper.Success(Types);
            }
        }

        /// <summary>
        /// 添加文章类型
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost("Create")]
        public async Task<ApiResult> PostCreate( string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return ApiResultHelper.Error("类型名称不能为空!");
            }
            TypeInfo type = new TypeInfo
            {
                Name = name
            };
            bool b = await _iTypeInfoService.CreateAsync(type);
            if (!b)
            {
                return ApiResultHelper.Error("添加失败!");
            }
            else
            {
                return ApiResultHelper.Success(b);
            }
        }

        /// <summary>
        /// 修改文章类型
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPut("Edit")]
        public async Task<ApiResult> PutEdit(int id,string name)
        {
            var type = await _iTypeInfoService.FindAsync(id);
            if (type == null)
            {
                return ApiResultHelper.Error("没有找该文章的类型!");
            }
            type.Name = name;
            bool b = await _iTypeInfoService.EditAsync(type);

            if (!b)
            {
                return ApiResultHelper.Error("修改失败!");
            }
            else
            {
                return ApiResultHelper.Success(type);
            }
        }

        /// <summary>
        /// 删除文章类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("Delete")]
        public async Task<ApiResult> Delete(int id)
        {
            bool b = await _iTypeInfoService.DeleteAsync(id);
            if (!b)
            {
                return ApiResultHelper.Error("删除失败!");
            }
            else
            {
                return ApiResultHelper.Success(b);
            }
        }
        }

        /// <summary>
        /// 查询全部文章类型
        /// </summary>
        /// <returns></returns>
        [HttpGet("Types")]
        public async Task<ApiResult> GetTypes()
        {
            var Types = await _iTypeInfoService.QueryAsync();
            if (Types.Count==0)
            {
                return ApiResultHelper.Error("查询不到相关的是数据!");
            }
            else
            {
                return ApiResultHelper.Success(Types);
            }
        }

        /// <summary>
        /// 添加文章类型
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost("Create")]
        public async Task<ApiResult> PostCreate( string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return ApiResultHelper.Error("类型名称不能为空!");
            }
            TypeInfo type = new TypeInfo
            {
                Name = name
            };
            bool b = await _iTypeInfoService.CreateAsync(type);
            if (!b)
            {
                return ApiResultHelper.Error("添加失败!");
            }
            else
            {
                return ApiResultHelper.Success(b);
            }
        }

        /// <summary>
        /// 修改文章类型
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPut("Edit")]
        public async Task<ApiResult> PutEdit(int id,string name)
        {
            var type = await _iTypeInfoService.FindAsync(id);
            if (type == null)
            {
                return ApiResultHelper.Error("没有找该文章的类型!");
            }
            type.Name = name;
            bool b = await _iTypeInfoService.EditAsync(type);

            if (!b)
            {
                return ApiResultHelper.Error("修改失败!");
            }
            else
            {
                return ApiResultHelper.Success(type);
            }
        }

        /// <summary>
        /// 删除文章类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("Delete")]
        public async Task<ApiResult> Delete(int id)
        {
            bool b = await _iTypeInfoService.DeleteAsync(id);
            if (!b)
            {
                return ApiResultHelper.Error("删除失败!");
            }
            else
            {
                return ApiResultHelper.Success(b);
            }
        }

(四)展示效果(测试的话,自己测试咯)

到这里就把基本的增删改查,搞定了 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值