使用JSON向多个表写入数据
Using JSON to write data to multiple tables
你能帮帮我吗?我仍然是编码的新手。我一直在尝试使用 Angular 11 构建发票表格,然后使用 JSON 将数据发送到我的网站 API,最后将数据写入 [=48] =] 数据库.
我有 2 个 classes - Invoice 和 InvoiceLineItems
public class CustomerInvoice : BaseEntity
{
public string InvoiceNumber { get; set; }
public DateTime DateRaised { get; set; }
public DateTime PaymentDueDate { get; set; }
public int SubTotal { get; set; }
public string DiscountDescription { get; set; }
public int DiscountFactor { get; set; }
public int DiscountAmount { get; set; }
public int TotalDiscount { get; set; }
public int GrandTotal { get; set; }
public FamilyGroup FamilyGroup { get; set; }
public int FamilyGroupId { get; set; }
public List<CustomerInvoiceLine> CustomerInvoiceLines { get; set; }
}
public class CustomerInvoiceLine: BaseEntity
{
public SchoolFee SchoolFee { get; set; }
public int SchoolFeeId { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public int Amount { get; set; }
public CustomerInvoice CustomerInvoice { get; set; }
public int CustomerInvoiceId { get; set; }
}
我有 2 个 DTO(上面每个 class 有 1 个)
public class CustomerInvoiceAddDto
{
public int Id { get; set; }
public string InvoiceNumber { get; set; }
public DateTime DateRaised { get; set; }
public DateTime PaymentDueDate { get; set; }
public int SubTotal { get; set; }
public string DiscountDescription { get; set; }
public int DiscountFactor { get; set; }
public int DiscountAmount { get; set; }
public int TotalDiscount { get; set; }
public int GrandTotal { get; set; }
public int FamilyGroupId { get; set; }
}
和
public class CustomerInvoiceLineAddDto
{
public int SchoolFeeId { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public int Amount { get; set; }
public int CustomerInvoiceId { get; set; }
}
我添加发票的控制器:
[HttpPost("addInvoice")]
public async Task<ActionResult> AddInvoice(CustomerInvoiceAddDto invoiceAdd)
{
CustomerInvoice invoice = new CustomerInvoice();
_mapper.Map(invoiceAdd, invoice);
await _unitOfWork.CustomerInvoiceRepository.AddCustomerInvoiceAsync(invoice);
var createdInvoice = await _context.CustomerInvoices
.Where(s => s.FamilyGroupId == invoiceAdd.FamilyGroupId )
.OrderBy(g => g.Id)
.LastOrDefaultAsync();
return Ok(createdInvoice.Id);
}
并添加发票行项目:
[HttpPost]
public async Task<ActionResult> AddCustomerInvoiceLines(CustomerInvoiceLineAddDto[] invoiceLinesAddDto)
{
var invoiceLineDetails = _mapper.Map<List<CustomerInvoiceLine>>(invoiceLinesAddDto);
await _unitOfWork.CustomerInvoiceLineRepository.AddCustomerInvoiceLinesAsync(invoiceLineDetails);
return Ok();
}
问题:当我最初设计这个时,我打算先调用 API 添加发票,然后 return 将发票 ID 发送到前端。此后,我将调用 InvoiceLineItems 控制器来创建发票行项目。
但是,在我的前端,我有 1 个 API 调用 JSON 格式如下:
{
"DateRaised": "2022-03-01T21:32:13.000Z",
"PaymentDueDate": "2022-03-08T21:32:13.000Z",
"FamilyGroupId": 12,
"subTotal": 1500,
"discountFactor": "2",
"discountAmount": 0,
"discountDescription": null,
"totalDiscount": 0,
"grandTotal": 1500,
"invoiceLineItems": [
{
"schoolFeeId": 1,
"quantity": "1",
"unitPrice": "1500"
},
{
"schoolFeeId": 2,
"quantity": "1",
"unitPrice": "3000"
}
]
}
是否可以有一个控制器可以接受 1 JSON 调用并写入发票 table、return 发票 ID,然后写入 InvoiceLineItems (行项目数组)?
再次感谢!
在 CustomerInvoiceAddDto
中添加 CustomerInvoiceLineAddDto
作为 属性,如下所示-
public class CustomerInvoiceAddDto
{
public int Id { get; set; }
public string InvoiceNumber { get; set; }
public DateTime DateRaised { get; set; }
public DateTime PaymentDueDate { get; set; }
public int SubTotal { get; set; }
public string DiscountDescription { get; set; }
public int DiscountFactor { get; set; }
public int DiscountAmount { get; set; }
public int TotalDiscount { get; set; }
public int GrandTotal { get; set; }
public int FamilyGroupId { get; set; }
public List<CustomerInvoiceLineAddDto> CustomerInvoiceLineDto {get;set;}
}
并保持 Angular JSON 格式不变。
然后在你的操作方法中做类似的事情。
我修改了你的代码假设你有类似的代码-
[HttpPost("addInvoice")]
public async Task<ActionResult> AddInvoice(CustomerInvoiceAddDto invoiceAdd)
{
CustomerInvoice invoice = new CustomerInvoice();
_mapper.Map(invoiceAdd, invoice);
await _unitOfWork.CustomerInvoiceRepository.AddCustomerInvoiceAsync(invoice);
var createdInvoice = await _context.CustomerInvoices
.Where(s => s.FamilyGroupId == invoiceAdd.FamilyGroupId)
.OrderBy(g => g.Id)
.LastOrDefaultAsync();
var invoiceLineDetails = _mapper.Map<List<CustomerInvoiceLine>>(invoiceAdd.CustomerInvoiceLineDto);
foreach (var line in invoiceLineDetails)
{
line.CustomerInvoiceId = createdInvoice.Id;
}
await _unitOfWork.CustomerInvoiceLineRepository.AddCustomerInvoiceLinesAsync(invoiceLineDetails);
return Ok(createdInvoice.Id);
}
您可以使用 ExecuteScalar 取回插入行的 ID。
你能帮帮我吗?我仍然是编码的新手。我一直在尝试使用 Angular 11 构建发票表格,然后使用 JSON 将数据发送到我的网站 API,最后将数据写入 [=48] =] 数据库.
我有 2 个 classes - Invoice 和 InvoiceLineItems
public class CustomerInvoice : BaseEntity
{
public string InvoiceNumber { get; set; }
public DateTime DateRaised { get; set; }
public DateTime PaymentDueDate { get; set; }
public int SubTotal { get; set; }
public string DiscountDescription { get; set; }
public int DiscountFactor { get; set; }
public int DiscountAmount { get; set; }
public int TotalDiscount { get; set; }
public int GrandTotal { get; set; }
public FamilyGroup FamilyGroup { get; set; }
public int FamilyGroupId { get; set; }
public List<CustomerInvoiceLine> CustomerInvoiceLines { get; set; }
}
public class CustomerInvoiceLine: BaseEntity
{
public SchoolFee SchoolFee { get; set; }
public int SchoolFeeId { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public int Amount { get; set; }
public CustomerInvoice CustomerInvoice { get; set; }
public int CustomerInvoiceId { get; set; }
}
我有 2 个 DTO(上面每个 class 有 1 个)
public class CustomerInvoiceAddDto
{
public int Id { get; set; }
public string InvoiceNumber { get; set; }
public DateTime DateRaised { get; set; }
public DateTime PaymentDueDate { get; set; }
public int SubTotal { get; set; }
public string DiscountDescription { get; set; }
public int DiscountFactor { get; set; }
public int DiscountAmount { get; set; }
public int TotalDiscount { get; set; }
public int GrandTotal { get; set; }
public int FamilyGroupId { get; set; }
}
和
public class CustomerInvoiceLineAddDto
{
public int SchoolFeeId { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public int Amount { get; set; }
public int CustomerInvoiceId { get; set; }
}
我添加发票的控制器:
[HttpPost("addInvoice")]
public async Task<ActionResult> AddInvoice(CustomerInvoiceAddDto invoiceAdd)
{
CustomerInvoice invoice = new CustomerInvoice();
_mapper.Map(invoiceAdd, invoice);
await _unitOfWork.CustomerInvoiceRepository.AddCustomerInvoiceAsync(invoice);
var createdInvoice = await _context.CustomerInvoices
.Where(s => s.FamilyGroupId == invoiceAdd.FamilyGroupId )
.OrderBy(g => g.Id)
.LastOrDefaultAsync();
return Ok(createdInvoice.Id);
}
并添加发票行项目:
[HttpPost]
public async Task<ActionResult> AddCustomerInvoiceLines(CustomerInvoiceLineAddDto[] invoiceLinesAddDto)
{
var invoiceLineDetails = _mapper.Map<List<CustomerInvoiceLine>>(invoiceLinesAddDto);
await _unitOfWork.CustomerInvoiceLineRepository.AddCustomerInvoiceLinesAsync(invoiceLineDetails);
return Ok();
}
问题:当我最初设计这个时,我打算先调用 API 添加发票,然后 return 将发票 ID 发送到前端。此后,我将调用 InvoiceLineItems 控制器来创建发票行项目。
但是,在我的前端,我有 1 个 API 调用 JSON 格式如下:
{
"DateRaised": "2022-03-01T21:32:13.000Z",
"PaymentDueDate": "2022-03-08T21:32:13.000Z",
"FamilyGroupId": 12,
"subTotal": 1500,
"discountFactor": "2",
"discountAmount": 0,
"discountDescription": null,
"totalDiscount": 0,
"grandTotal": 1500,
"invoiceLineItems": [
{
"schoolFeeId": 1,
"quantity": "1",
"unitPrice": "1500"
},
{
"schoolFeeId": 2,
"quantity": "1",
"unitPrice": "3000"
}
]
}
是否可以有一个控制器可以接受 1 JSON 调用并写入发票 table、return 发票 ID,然后写入 InvoiceLineItems (行项目数组)?
再次感谢!
在 CustomerInvoiceAddDto
中添加 CustomerInvoiceLineAddDto
作为 属性,如下所示-
public class CustomerInvoiceAddDto
{
public int Id { get; set; }
public string InvoiceNumber { get; set; }
public DateTime DateRaised { get; set; }
public DateTime PaymentDueDate { get; set; }
public int SubTotal { get; set; }
public string DiscountDescription { get; set; }
public int DiscountFactor { get; set; }
public int DiscountAmount { get; set; }
public int TotalDiscount { get; set; }
public int GrandTotal { get; set; }
public int FamilyGroupId { get; set; }
public List<CustomerInvoiceLineAddDto> CustomerInvoiceLineDto {get;set;}
}
并保持 Angular JSON 格式不变。
然后在你的操作方法中做类似的事情。
我修改了你的代码假设你有类似的代码-
[HttpPost("addInvoice")]
public async Task<ActionResult> AddInvoice(CustomerInvoiceAddDto invoiceAdd)
{
CustomerInvoice invoice = new CustomerInvoice();
_mapper.Map(invoiceAdd, invoice);
await _unitOfWork.CustomerInvoiceRepository.AddCustomerInvoiceAsync(invoice);
var createdInvoice = await _context.CustomerInvoices
.Where(s => s.FamilyGroupId == invoiceAdd.FamilyGroupId)
.OrderBy(g => g.Id)
.LastOrDefaultAsync();
var invoiceLineDetails = _mapper.Map<List<CustomerInvoiceLine>>(invoiceAdd.CustomerInvoiceLineDto);
foreach (var line in invoiceLineDetails)
{
line.CustomerInvoiceId = createdInvoice.Id;
}
await _unitOfWork.CustomerInvoiceLineRepository.AddCustomerInvoiceLinesAsync(invoiceLineDetails);
return Ok(createdInvoice.Id);
}
您可以使用 ExecuteScalar 取回插入行的 ID。