如何在Cqrs模式API的角度客户端中创建Crud模型?

问题描述 投票:0回答:1

您好,我在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();
    }
}

在客户端,我有一个用于Party List的组件,该组件用于分页数据并绑定到PartyViewModel[]以及一个或两个(我不知道请指导我)用于添加和更新的组件

我应该如何在客户端设计DataModels或在服务器端进行哪些更改以完成CRUD方案?

非常感谢

angular asp.net-core web cqrs clean-architecture
1个回答
0
投票
您只需要一个界面即可输入对象。

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

© www.soinside.com 2019 - 2024. All rights reserved.