How do I write a POST request in JSON to post a new product?

I am working on a WebAPI. I have used Entity framework to make an SQL database via the C# code my friend and I have written. Our product table in the SQL Database is based on the following code:

public class Product
{

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Display(Name = "Product Number")]
    public int ProductID { get; set; }
    [StringLength(50, MinimumLength = 3)]
    public string ProductName { get; set; }


    [Range(0, 99999)]
    public int Price { get; set; }

    public int MarketID { get; set; }


    public Market Market { get; set; }    
    public ICollection<Subscription> Subscriptions { get; set; }
    public ICollection<ProductAssignment> ProductAssignments { get; set; }

}

When we run our solution and open up Postman to make a post request we are not sure how the JSON should be written. So far we have written:

{
    "productname": "API Reading Pen",
    "price": 100,
}

The problem is that we do not know how to write the JSON for Market, Subscriptions and ProductAssignments.

Postman:
EDIT JSON in Postman

What should we write in JSON to POST a new Product?

EDIT:

Market class:

public class Market
    {
        public int MarketID { get; set; }

        [StringLength(50, MinimumLength = 3)]
        public string Name { get; set; }

        [DataType(DataType.Currency)]
        [Column(TypeName = "money")]
        public decimal Budget { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Start Date")]
        public DateTime StartDate { get; set; }

        public int? ProductGuideID { get; set; }

        [Timestamp]
        public byte[] RowVersion { get; set; }

        public ProductGuide Administrator { get; set; }
        public ICollection<Product> Products { get; set; }
    }

Subscription:

 public enum CustomerLoyalty
{
    A, B, C, D, F
}
public class Subscription
{
    public int SubscriptionID { get; set; }
    public int ProductID { get; set; }
    public int CustomerID { get; set; }
    [DisplayFormat(NullDisplayText = "Loyalty not set")]
    public CustomerLoyalty? CustomerLoyalty { get; set; }

    public Product Product { get; set; }
    public Customer Customer { get; set; }
}

ProductAssignment:

public class ProductAssignment
{
    public int ProductGuideID { get; set; }
    public int ProductID { get; set; }
    public ProductGuide ProductGuide { get; set; }
    public Product Product { get; set; }
}

EDIT:
Post method in ProductsController.cs:

 [HttpPost]
    public async Task<ActionResult<Product>> PostProduct(Product product)
    {
        _context.Products.Add(product);
        try
        {
            await _context.SaveChangesAsync();
        }
        catch (DbUpdateException)
        {
            if (ProductExists(product.ProductID))
            {
                return Conflict();
            }
            else
            {
                throw;
            }
        }

        return CreatedAtAction("GetProduct", new { id = product.ProductID }, product);
    }

EDIT: Link to our github repository: https://github.com/tux-superman/VitekSky

EDIT:
My Postman JSON code so far:

{

“ProductID”: 1234,
“ProductName”: “Testproduct”,
“Price”: 10,
“MarketID”: 4321,
“Market”:
{
“MarketID”: 1111,
“Name”: “Market1”,
“Budget”: 1.5,
“StartDate”: 2019-11-28,
“ProductGuideID”: 123
}
“Subscriptions”:
{
“SubscriptionId”:1234,
“ProductId”: 1234,
“CustomerID”: 1234,
“CustomerLoyalty”: “A”,
“Product”:
{

    }
    "Customer":
    {

    }
}
"ProductAssignments"
{
    "ProductGuideId": 1234,
    "ProductID": 1234
    "ProductGuide"
    {

    }
    "Product"
    {

    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *