简体   繁体   English

如何在 Angular 客户端为 cqrs 模式 API 创建 crud 模型?

[英]How to create crud Model in Angular client side for cqrs pattern API?

I implement web API in ASP.net core in CQRS Pattent and using mediatR library I have separate model Like CreatePartyCommand, UpadatePartyCommand, DeletePartyCommand, PartyViewModel, PartyQuery我在CQRS Pattent ASP.net 核心中实现了 Web API,并使用mediatR library我有单独的模型,如CreatePartyCommand, UpadatePartyCommand, DeletePartyCommand, PartyViewModel, PartyQuery

public class CreatePartyCommand : IRequest<int>
{
    public string Name { get; set; }

    public string Family { get; set; }

    public long Code { get; set; }

    public string Title { get; set; }
}

public class UpdatePartyCommand : IRequest<int>
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Family { get; set; }

    public long Code { get; set; }

    public string Title { get; set; }

}

public class DeletePartyCommand : IRequest
{
    public int Id { get; set; }
}

[Route("api/[controller]")]
[ApiController]
public class PartyController : ControllerBase
{
    private readonly IMediator _mediator;

    public PartyController(IMediator mediator)
    {
        _mediator = mediator;
    }

    [HttpGet]
    public async Task<ActionResult<IEnumerable<PartyViewModel>>> Get([FromQuery] PartyQuery partyQuery)
    {
        return Ok(await _mediator.Send(partyQuery));
    }

    [HttpPost]
    public async Task<ActionResult<int>> Create([FromBody] CreatePartyCommand command)
    {
        var productId = await _mediator.Send(command);

        return Ok(productId);
    }

    [HttpPut]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    [ProducesDefaultResponseType]
    public async Task<IActionResult> Update([FromBody] UpdatePartyCommand command)
    {
        await _mediator.Send(command);

        return NoContent();
    }

    [HttpDelete("{id}")]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    [ProducesDefaultResponseType]
    public async Task<IActionResult> Delete(int id)
    {
        await _mediator.Send(new DeletePartyCommand { Id = id });

        return NoContent();
    }
}

In client side I have a component for Party List that paged data and bind to PartyViewModel[] and one or two (I don't know guide me please) component for add and update在客户端,我有一个Party List组件,用于分页数据并绑定到PartyViewModel[]和一两个(我不知道请指导我)组件用于添加和更新

How should I design DataModels in client side or what changes made in server side to complete CRUD scenario?我应该如何在客户端设计数据模型或在服务器端进行哪些更改以完成 CRUD 场景?

You just need one interface to type your objects.您只需要一个界面来键入您的对象。

export interface IParty {
  Id?: number;
  Name: string;
  Family: string;
  Code: number;
  Title: string;
}

Example for POST: POST 示例:

service:服务:

create(party: IParty): Promise<IParty> {
  return this.http.post('link', party).toPromise();
}

component成分

async createParty(party: IParty): Promise<void> {
  const party = await this.partyService.create(party); // if you use promise you should put this code in a try-catch block
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 6-从模型创建CRUD - Angular 6 - Create CRUD from Model 如何使用 Angular 5 创建 CRUD - How Create CRUD with Angular 5 如何在Angular 2客户端应用程序中隐藏/保护API密钥? - How to hide/secure API keys in an Angular 2 client side application? 在客户端创建和保存文件 - Angular - Create and Save File on Client Side - Angular 如何将图像作为字节数组从 Angular 9 发送到 C# Web API Core,就像对象的属性一样? 带有图像的 CRUD 客户端应用程序 - How send image as byte array from Angular 9 to C# Web API Core, like are property of object? CRUD Client aplication with image 在服务器端 Django REST 中使用 API 并在 ZC31C318BA0372813DE1Z51 中为客户端提供服务 - Consume an API in Django REST, server side, and serve it ,client side, in Angular Angular 和 API 中的 CRUD(更新)操作 - CRUD (Update) Operation in Angular With API 在Angular Universal中,如何在服务器端渲染期间避免API客户端并将它们推迟到客户端? - How to avoid API clients during server side rendering and defer them to the client, in Angular Universal? 如何创建自定义键 Angular Firebase CRUD 以创建嵌套子项 - How to create custom key Angular Firebase CRUD to make a Nested Child Crud 服务 RXJS Angular 9 命令查询模式 - Crud Service RXJS Angular 9 Command Query Pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM