簡體   English   中英

JSON 數組的 POST 請求

[英]POST Request of a JSON Array

我可以寫一個 controller 用於將 1 object 發布到我的 controller 中的數據庫。但是我現在想使用 JSON 在 1 API 調用中發布多個對象

我的實體

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 int CustomerInvoiceId { get; set; }
    }

我有一個 DTO 如下:

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; }
    }

我創建了一個存儲庫:

public async Task AddCustomerInvoiceLineAsync(CustomerInvoiceLine customerInvoiceLine)
        {
            _context.CustomerInvoiceLines.Add(customerInvoiceLine);
            await _context.SaveChangesAsync();
        }

最后是 Controller

[HttpPost]
        public async Task<ActionResult> AddCustomerInvoiceLine(CustomerInvoiceLineAddDto invoiceLineAddDto)
        {
            CustomerInvoiceLine invoiceLineDetails = new CustomerInvoiceLine();
            _mapper.Map(invoiceLineAddDto, invoiceLineDetails);
            await _unitOfWork.CustomerInvoiceLineRepository.AddCustomerInvoiceLineAsync(invoiceLineDetails);
            return Ok();
        }

上面的 controller 在 JSON 請求中僅發布 1 個項目時工作正常。

然后我嘗試更改它以接收 JSON 數組。

public async Task<ActionResult> AddCustomerInvoiceLine(CustomerInvoiceLineAddDto invoiceLineAddDto)
        {
            string json = "";
            CustomerInvoiceLine invoiceLineDetails = new CustomerInvoiceLine();
            CustomerInvoiceLineAddDto invoiceLines = JsonConvert.DeserializeObject<CustomerInvoiceLineAddDto>( json );
            _mapper.Map(invoiceLineAddDto, invoiceLines);
            await _unitOfWork.CustomerInvoiceLineRepository.AddCustomerInvoiceLineAsync(invoiceLineDetails);
            return Ok();
        }

我的 JSON 請求:

[
    {
        "schoolFeeId": 1,
        "customerInvoiceId": 18,
        "description": "School Fees for 2022",
        "quantity": 1,
        "amount": 5000
    },
    {
        "schoolFeeId": 1,
        "customerInvoiceId": 18,
        "description": "School Fees for 2021",
        "quantity": 1,
        "amount": 3000
    }
]

有人可以幫助理解如何反序列化 JSON 主體然后處理到數據庫嗎?

如果要接受數組,請確保 controller 接受數組。 然后 automapper 足夠聰明,可以將一種類型的數組轉換為另一種類型的數組。

public async Task<ActionResult> AddCustomerInvoiceLine(CustomerInvoiceLineAddDto[] invoiceLinesAddDto)
{
    var invoiceLineDetails = _mapper.Map<List<CustomerInvoiceLine>>(invoiceLinesAddDto);
    await _unitOfWork.CustomerInvoiceLineRepository.AddCustomerInvoiceLinesAsync(invoiceLineDetails);
    return Ok();
}

在你的工作單元中,添加一個function來處理列表的添加

public async Task AddCustomerInvoiceLinesAsync(IEnumerable<CustomerInvoiceLine> customerInvoiceLines)
{
    _context.CustomerInvoiceLines.AddRange(customerInvoiceLines);
    await _context.SaveChangesAsync();
}

請注意,您不能使用一個 controller 方法來處理數組請求和單個項目請求。 我建議定義兩個不同的端點,以便 api 的消費者知道哪個需要數組,哪個不需要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM