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:
{
"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"
}
"ProductAssignments"
{
"ProductGuideId": 1234,
"ProductID": 1234
}
}
The problem is that we do not know how to write the JSON for Market, Subscriptions and ProductAssignments.
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: ProductsController.cs:
namespace VitekAPI.Controllers
{
[Route("api/Products")]
[ApiController]
public class ProductsController : ControllerBase
{
private readonly BusinessContext _context;
public ProductsController(BusinessContext context)
{
_context = context;
}
// GET: api/Products
[HttpGet]
public async Task<ActionResult<IEnumerable<Product>>> GetProducts()
{
return await _context.Products.ToListAsync();
}
// GET: api/Products/5
[HttpGet("{id}")]
public async Task<ActionResult<Product>> GetProduct(int id)
{
var product = await _context.Products.FindAsync(id);
if (product == null)
{
return NotFound();
}
return product;
}
// PUT: api/Products/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPut("{id}")]
public async Task<IActionResult> PutProduct(int id, Product product)
{
if (id != product.ProductID)
{
return BadRequest();
}
_context.Entry(product).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ProductExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Products
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[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);
}
// DELETE: api/Products/5
[HttpDelete("{id}")]
public async Task<ActionResult<Product>> DeleteProduct(int id)
{
var product = await _context.Products.FindAsync(id);
if (product == null)
{
return NotFound();
}
_context.Products.Remove(product);
await _context.SaveChangesAsync();
return product;
}
private bool ProductExists(int id)
{
return _context.Products.Any(e => e.ProductID == id);
}
}
EDIT: Link to our github repository: https://github.com/tux-superman/VitekSky
You can lookup the notations of JSON types here .
Given your product class the JSON Notation would look something like this:
{
"ProductID": 1234,
"ProductName": "Testproduct",
"Price": 10,
"MarketID": 4321,
"Market":
{
"MarketID": 1111,
"Name": "Market1",
"Budget": 1.5,
"StartDate": 2019-11-28,
"ProductGuideID": 123
}
// All your other custom objects
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.