.NET 云原生架构师训练营(模块二 基础巩固 MongoDB API实现)--学习笔记

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介: - 问题查询单个实现- 问题查询列表实现- 问题跨集合查询实现- 问题创建实现- 问题更新实现- 问题回答实现- 问题评论实现- 问题投票实现- 回答实现

2.5.7 MongoDB -- API实现

  • 问题查询单个实现
  • 问题查询列表实现
  • 问题跨集合查询实现
  • 问题创建实现
  • 问题更新实现
  • 问题回答实现
  • 问题评论实现
  • 问题投票实现
  • 回答实现

QuestionController

namespace LighterApi.Controller
{
    [ApiController]
    [Route("api/[controller]")]
    public class QuestionController : ControllerBase
    {
        private readonly IMongoCollection<Question> _questionCollection;

        private readonly IMongoCollection<Vote> _voteCollection;

        private readonly IMongoCollection<Answer> _answerCollection;

        public QuestionController(IMongoClient mongoClient)
        {
            var database = mongoClient.GetDatabase("lighter");

            _questionCollection = database.GetCollection<Question>("questions");
            _voteCollection = database.GetCollection<Vote>("votes");
            _answerCollection = database.GetCollection<Answer>("answers");
        }
    }
}

问题查询单个实现

linq 查询

[HttpGet]
[Route("{id}")]
public async Task<ActionResult<Question>> GetAsync(string id, CancellationToken cancellationToken)
{
    var question = await _questionCollection.AsQueryable()
        .FirstOrDefaultAsync(q => q.Id == id, cancellationToken: cancellationToken);
    if (question == null)
        return NotFound();

    return Ok(question);
}

mongo 查询表达式

var filter = Builders<Question>.Filter.Eq(q => q.Id, id);
await _questionCollection.Find(filter).FirstOrDefaultAsync(cancellationToken);

构造空查询条件的表达式

var filter = string.IsNullOrEmpty(id)
    ? Builders<Question>.Filter.Empty
    : Builders<Question>.Filter.Eq(q => q.Id, id);

多段拼接 filter

var filter2 = Builders<Question>.Filter.And(filter, Builders<Question>.Filter.Eq(q => q.TenantId , "001"));

问题查询列表实现

  • 数据 AnyIn查询
  • 排序 sort : StirngFieldDefinition
  • 分页 skip, limit
[HttpGet]
public async Task<ActionResult<List<Question>>> GetListAsync([FromQuery] List<string> tags,
    CancellationToken cancellationToken, [FromQuery] string sort = "createdAt", [FromQuery] int skip = 0,
    [FromQuery] int limit = 10)
{
    //// linq 查询
    //await _questionCollection.AsQueryable().Where(q => q.ViewCount > 10)
    //    .ToListAsync(cancellationToken: cancellationToken);

    var filter = Builders<Question>.Filter.Empty;

    if (tags != null && tags.Any())
    {
        filter = Builders<Question>.Filter.AnyIn(q => q.Tags, tags);
    }

    var sortDefinition = Builders<Question>.Sort.Descending(new StringFieldDefinition<Question>(sort));

    var result = await _questionCollection
        .Find(filter)
        .Sort(sortDefinition)
        .Skip(skip)
        .Limit(limit)
        .ToListAsync(cancellationToken: cancellationToken);

    return Ok(result);
}

问题跨集合查询实现

[HttpGet]
[Route("{id}/answers")]
public async Task<ActionResult> GetWithAnswerAsync(string id, CancellationToken cancellationToken)
{
    // linq 查询
    var query = from question in _questionCollection.AsQueryable()
                where question.Id == id
                join a in _answerCollection.AsQueryable() on question.Id equals a.QuestionId into answers
                select new { question, answers };

    var result = await query.FirstOrDefaultAsync(cancellationToken);

    if (result == null)
        return NotFound();

    return Ok(result);
}

问题创建实现

[HttpPost]
public async Task<ActionResult<Question>> CreateAsync([FromBody] Question question,
    CancellationToken cancellationToken)
{
    question.Id = Guid.NewGuid().ToString();
    await _questionCollection.InsertOneAsync(question, new InsertOneOptions {BypassDocumentValidation = false},
        cancellationToken);
    return StatusCode((int) HttpStatusCode.Created, question);
}

问题更新实现

[HttpPatch]
[Route("{id}")]
public async Task<ActionResult> UpdateAsync([FromRoute] string id, [FromBody] QuestionUpdateRequest request,
    CancellationToken cancellationToken)
{
    if (string.IsNullOrEmpty(request.Summary))
        throw new ArgumentNullException(nameof(request.Summary));

    var filter = Builders<Question>.Filter.Eq(q => q.Id, id);

    var update = Builders<Question>.Update
        .Set(q => q.Title, request.Title)
        .Set(q => q.Content, request.Content)
        .Set(q => q.Tags, request.Tags)
        .Push(q => q.Comments, new Comment { Content = request.Summary, CreatedAt = DateTime.Now });

    await _questionCollection.UpdateOneAsync(filter, update, cancellationToken: cancellationToken);

    return Ok();
}

使用拼接的方式构建表达式

var updateFieldList = new List<UpdateDefinition<Question>>();

if (!string.IsNullOrWhiteSpace(request.Title))
    updateFieldList.Add(Builders<Question>.Update.Set(q => q.Title, request.Title));

if (!string.IsNullOrWhiteSpace(request.Content))
    updateFieldList.Add(Builders<Question>.Update.Set(q => q.Content, request.Content));

if (request.Tags != null && request.Tags.Any())
    updateFieldList.Add(Builders<Question>.Update.Set(q => q.Tags, request.Tags));

updateFieldList.Add(Builders<Question>.Update.Push(q => q.Comments,
    new Comment {Content = request.Summary, CreatedAt = DateTime.Now}));

var update = Builders<Question>.Update.Combine(updateFieldList);

问题回答实现

[HttpPost]
[Route("{id}/answer")]
public async Task<ActionResult<Answer>> AnswerAsync([FromRoute] string id, [FromBody] AnswerRequest request,
    CancellationToken cancellationToken)
{
    var answer = new Answer {QuestionId = id, Content = request.Content, Id = Guid.NewGuid().ToString()};
    _answerCollection.InsertOneAsync(answer, cancellationToken);

    var filter = Builders<Question>.Filter.Eq(q => q.Id, id);
    var update = Builders<Question>.Update.Push(q => q.Answers, answer.Id);

    await _questionCollection.UpdateOneAsync(filter, update, null, cancellationToken);

    return Ok();
}

问题评论实现

[HttpPost]
[Route("{id}/comment")]
public async Task<ActionResult> CommentAsync([FromRoute] string id, [FromBody] CommentRequest request,
    CancellationToken cancellationToken)
{
    var filter = Builders<Question>.Filter.Eq(q => q.Id, id);
    var update = Builders<Question>.Update.Push(q => q.Comments,
        new Comment {Content = request.Content, CreatedAt = DateTime.Now});

    await _questionCollection.UpdateOneAsync(filter, update, null, cancellationToken);

    return Ok();
}

问题投票实现

[HttpPost]
[Route("{id}/up")]
public async Task<ActionResult> UpAsync([FromBody] string id, CancellationToken cancellationToken)
{
    var vote = new Vote
    {
        Id = Guid.NewGuid().ToString(),
        SourceType = ConstVoteSourceType.Question,
        SourceId = id,
        Direction = EnumVoteDirection.Up
    };

    await _voteCollection.InsertOneAsync(vote, cancellationToken);

    var filter = Builders<Question>.Filter.Eq(q => q.Id, id);
    var update = Builders<Question>.Update.Inc(q => q.VoteCount, 1).AddToSet(q => q.VoteUps, vote.Id);
    await _questionCollection.UpdateOneAsync(filter, update);

    return Ok();
}

[HttpPost]
[Route("{id}/down")]
public async Task<ActionResult> DownAsync([FromBody] string id, CancellationToken cancellationToken)
{
    var vote = new Vote
    {
        Id = Guid.NewGuid().ToString(),
        SourceType = ConstVoteSourceType.Question,
        SourceId = id,
        Direction = EnumVoteDirection.Down
    };

    await _voteCollection.InsertOneAsync(vote, cancellationToken);

    var filter = Builders<Question>.Filter.Eq(q => q.Id, id);
    var update = Builders<Question>.Update.Inc(q => q.VoteCount, -1).AddToSet(q => q.VoteDowns, vote.Id);
    await _questionCollection.UpdateOneAsync(filter, update);

    return Ok();
}

回答实现

namespace LighterApi.Controller
{
    [ApiController]
    [Route("api/[controller]")]
    public class AnswerController : ControllerBase
    {
        private readonly IMongoCollection<Vote> _voteCollection;

        private readonly IMongoCollection<Answer> _answerCollection;

        public AnswerController(IMongoClient mongoClient)
        {
            var database = mongoClient.GetDatabase("lighter");

            _voteCollection = database.GetCollection<Vote>("votes");
            _answerCollection = database.GetCollection<Answer>("answers");
        }

        [HttpGet]
        public async Task<ActionResult<Answer>> GetListAsync([FromQuery] string questionId, CancellationToken cancellationToken)
        {
            var list = await _answerCollection.AsQueryable().Where(a => a.QuestionId == questionId)
                .ToListAsync(cancellationToken);
            return Ok(list);
        }

        [HttpPatch]
        [Route("{id}")]
        public async Task<ActionResult> UpdateAsync(string id, string content, string summary,
            CancellationToken cancellationToken)
        {
            var filter = Builders<Answer>.Filter.Eq(q => q.Id, id);

            var update = Builders<Answer>.Update
                .Set(q => q.Content, content)
                .Push(q => q.Comments, new Comment { Content = summary, CreatedAt = DateTime.Now });

            await _answerCollection.UpdateOneAsync(filter, update, cancellationToken: cancellationToken);

            return Ok();
        }

        [HttpPost]
        [Route("{id}/comment")]
        public async Task<ActionResult> CommentAsync([FromRoute] string id, [FromBody] CommentRequest request,
    CancellationToken cancellationToken)
        {
            var filter = Builders<Answer>.Filter.Eq(q => q.Id, id);
            var update = Builders<Answer>.Update.Push(q => q.Comments,
                new Comment { Content = request.Content, CreatedAt = DateTime.Now });

            await _answerCollection.UpdateOneAsync(filter, update, null, cancellationToken);

            return Ok();
        }

        [HttpPost]
        [Route("{id}/up")]
        public async Task<ActionResult> UpAsync([FromBody] string id, CancellationToken cancellationToken)
        {
            var vote = new Vote
            {
                Id = Guid.NewGuid().ToString(),
                SourceType = ConstVoteSourceType.Answer,
                SourceId = id,
                Direction = EnumVoteDirection.Up
            };

            await _voteCollection.InsertOneAsync(vote, cancellationToken);

            var filter = Builders<Answer>.Filter.Eq(q => q.Id, id);
            var update = Builders<Answer>.Update.Inc(q => q.VoteCount, 1).AddToSet(q => q.VoteUps, vote.Id);
            await _answerCollection.UpdateOneAsync(filter, update);

            return Ok();
        }

        [HttpPost]
        [Route("{id}/down")]
        public async Task<ActionResult> DownAsync([FromBody] string id, CancellationToken cancellationToken)
        {
            var vote = new Vote
            {
                Id = Guid.NewGuid().ToString(),
                SourceType = ConstVoteSourceType.Answer,
                SourceId = id,
                Direction = EnumVoteDirection.Down
            };

            await _voteCollection.InsertOneAsync(vote, cancellationToken);

            var filter = Builders<Answer>.Filter.Eq(q => q.Id, id);
            var update = Builders<Answer>.Update.Inc(q => q.VoteCount, -1).AddToSet(q => q.VoteDowns, vote.Id);
            await _answerCollection.UpdateOneAsync(filter, update);

            return Ok();
        }
    }
}

GitHub源码链接:

https://github.com/MINGSON666/Personal-Learning-Library/tree/main/ArchitectTrainingCamp/LighterApi

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
1月前
|
开发框架 .NET API
Windows Forms应用程序中集成一个ASP.NET API服务
Windows Forms应用程序中集成一个ASP.NET API服务
90 9
|
1月前
|
存储 开发框架 .NET
.NET 8 实现无实体库表 API 部署服务
【10月更文挑战第12天】在.NET 8中,可通过以下步骤实现无实体库表的API部署:首先安装.NET 8 SDK及开发工具,并选用轻量级Web API框架如ASP.NET Core;接着创建新项目并设计API,利用内存数据结构模拟数据存储;最后配置项目设置并进行测试与部署。此方法适用于小型项目或临时解决方案,但对于大规模应用仍需考虑持久化存储以确保数据可靠性与可扩展性。
|
2月前
|
开发框架 监控 前端开发
在 ASP.NET Core Web API 中使用操作筛选器统一处理通用操作
【9月更文挑战第27天】操作筛选器是ASP.NET Core MVC和Web API中的一种过滤器,可在操作方法执行前后运行代码,适用于日志记录、性能监控和验证等场景。通过实现`IActionFilter`接口的`OnActionExecuting`和`OnActionExecuted`方法,可以统一处理日志、验证及异常。创建并注册自定义筛选器类,能提升代码的可维护性和复用性。
|
1月前
|
存储 消息中间件 前端开发
.NET常见的几种项目架构模式,你知道几种?
.NET常见的几种项目架构模式,你知道几种?
|
1月前
|
监控 安全 API
Docker + .NET API:简化部署和扩展
Docker + .NET API:简化部署和扩展
39 0
|
1月前
|
监控 安全 API
最完美的扩展Docker + .NET API:简化部署和扩展
最完美的扩展Docker + .NET API:简化部署和扩展
71 0
|
1月前
|
API
使用`System.Net.WebClient`类发送HTTP请求来调用阿里云短信API
使用`System.Net.WebClient`类发送HTTP请求来调用阿里云短信API
23 0
|
3月前
|
设计模式 存储 前端开发
揭秘.NET架构设计模式:如何构建坚不可摧的系统?掌握这些,让你的项目无懈可击!
【8月更文挑战第28天】在软件开发中,设计模式是解决常见问题的经典方案,助力构建可维护、可扩展的系统。本文探讨了.NET中三种关键架构设计模式:MVC、依赖注入与仓储模式,并提供了示例代码。MVC通过模型、视图和控制器分离关注点;依赖注入则通过外部管理组件依赖提升复用性和可测性;仓储模式则统一数据访问接口,分离数据逻辑与业务逻辑。掌握这些模式有助于开发者优化系统架构,提升软件质量。
54 5
|
3月前
|
XML 开发框架 .NET
.NET框架:软件开发领域的瑞士军刀,如何让初学者变身代码艺术家——从基础架构到独特优势,一篇不可错过的深度解读。
【8月更文挑战第28天】.NET框架是由微软推出的统一开发平台,支持多种编程语言,简化应用程序的开发与部署。其核心组件包括公共语言运行库(CLR)和类库(FCL)。CLR负责内存管理、线程管理和异常处理等任务,确保代码稳定运行;FCL则提供了丰富的类和接口,涵盖网络、数据访问、安全性等多个领域,提高开发效率。此外,.NET框架还支持跨语言互操作,允许开发者使用C#、VB.NET等语言编写代码并无缝集成。这一框架凭借其强大的功能和广泛的社区支持,已成为软件开发领域的重要工具,适合初学者深入学习以奠定职业生涯基础。
102 1
|
3月前
|
jenkins API 持续交付
jenkins学习笔记之十五:SonarSQube API使用
jenkins学习笔记之十五:SonarSQube API使用

热门文章

最新文章