[英]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.