public IActionResult Post([FromBody] object payload) { // TO DO, validation of required fields // TO DO, validation if request includes breakdown item, ensure the sum of all breakdown is equal to 100 // TO DO, fail request if stored "pricedOn" is greater that request "pricedOn" IFundMapper fundMapper = new FundMapper(); if (payload is JObject) { FundDetails fundsDetails = ((JObject)payload).ToObject <FundDetails>(); ValidationContext vc = new ValidationContext(fundsDetails); var results = new List <ValidationResult>(); if (_context.Funds.Any(x => x.Id == fundsDetails.id)) { return(Conflict("fund already exists")); } var isValid = Validator.TryValidateObject(fundsDetails, vc, results); _context.Funds.Add(fundMapper.FromContractToDomain(fundsDetails)); _context.SaveChangesAsync(); return(Created("http://localhost:5000/api/funds", fundsDetails.id)); } return(BadRequest()); }
public async Task <IActionResult> Post([FromBody] JArray payload) { IFundMapper fundMapper = new FundMapper(); FundDetails[] fundsDetails = (payload).ToObject <FundDetails[]>(); foreach (var fd in fundsDetails) { // TO DO, validate each fund details // TO DO, fail the entire request if a error is found // TO DO, fail if the fund already exists _context.Funds.Add(fundMapper.FromContractToDomain(fd)); _context.SaveChanges(); } return(Ok()); }