Insert Data into Particular Rows from a Different Controller

40 Views Asked by At

I have two tables, InHandOrder & ExpRegistry. These table have a common column OrderNo.

When I insert data into the ExpRegistry table, it checks if InHandOrder table's OrderNo is equal to ExpRegistry table's OrderNo; if so, some of the column will automatically save into InHandOrder table that particular row in which OrderNo is matches.

I have tried something but data didn't save into the particular row but it's added a new row and save the data.

ExpRegistry controller class:

[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status202Accepted)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult Create(ExpRegistry obj)
{
    if (ModelState.IsValid)
    {
        _unitOfWork.ExpRegistry.Add(obj);
        _unitOfWork.Save();

        IEnumerable<InHandOrder> objInHandOrderList = _unitOfWork.InHandOrder.GetAll().ToList();

        var checkOrderNo = objInHandOrderList.FirstOrDefault(i => i.OrderNo == obj.OrderNo);
        _unitOfWork.InHandOrder.Add(new InHandOrder()
            {
                InvoiceNo = obj.InvoiceNo,
                ShipQty = obj.ShipQty,
                InvoiceValue = obj.InvoiceValue
            });

        _unitOfWork.Save();

        return CreatedAtAction("GetDetails", new { id = obj.Id }, obj);
    }

    _logger.LogError($"Something went wrong in the {nameof(Create)}");
    return StatusCode(500, "Internal Server Error, Please Try Again Later!");
}

InHandOrder model class:

public class InHandOrder
{
    [Key()]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }

    // Purchase Order
    public int? ContractListId { get; set; }

    [ForeignKey("ContractListId")]
    [ValidateNever]
    public ContractList? ContractList { get; set; }

    public string? OrderNo { get; set; }

    public int? StyleListId { get; set; }

    [ForeignKey("StyleListId")]
    [ValidateNever]
    public StyleList? StyleList { get; set; }

    public int? SeasonListId { get; set; }

    [ForeignKey("SeasonListId")]
    [ValidateNever]
    public SeasonList? SeasonList { get; set; }

    public DateTime? Shipment { get; set; }
    public int? TotalQuantity { get; set; }
    public decimal? UnitPrice { get; set; }
    public decimal? PoValue { get; set; }

    public int? CountryListId { get; set; }

    [ForeignKey("CountryListId")]
    [ValidateNever]
    public CountryList? CountryList { get; set; }

    // Export Register
    public string? InvoiceNo { get; set; }
    public int? ShipQty { get; set; }

    public decimal? InvoiceValue { get; set; }
    public decimal? ShortValue { get; set; }
    public int? ShortQty { get; set; }
}

Here, InvoiceNo, ShipQty, InvoiceValue, shortValue, ShortQty fields will be saved when ExpRegistry is saved.

ExpRegistry model class:

public class ExpRegistry
{
    [Key]
    public int Id { get; set; }
    public int? PoId { get; set; }

    [DisplayName("Exp No")]
    public string? ExpNo { get; set; }
    [DisplayName("UNIT")]
    public int? UnitListId { get; set; }
    [ForeignKey("UnitListId")]
    [ValidateNever]
    public UnitList? UnitList { get; set; }

    [Display(Name = "DATE")] //EXP ISSUE DATE
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? ExpIssueDate { get; set; }

    [DisplayName("ORDER NO")]
    [ValidateNever]
    public string? OrderNo { get; set; }

    [DisplayName("INVOICE NO")]
    [ValidateNever]
    public string? InvoiceNo { get; set; }

    [DisplayName("SHIPPED QTY")]
    [ValidateNever]
    public int? ShipQty { get; set; }

    [DisplayName("VALUE")]
    [DisplayFormat(DataFormatString = "{0:C}", ApplyFormatInEditMode = false)]
    [ValidateNever]
    public decimal? InvoiceValue { get; set; }
}

Please Help me to solve the issue. Thank you.

0

There are 0 best solutions below