public async Task <IActionResult> PostGoodCateCode([FromBody] GoodCateCode goodCateCode) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var getCate = await _context.GetTransactionInv .Where(w => w.ProductCode == goodCateCode.GoodCatecode) .GroupBy(g => new { g.Product }) .Select(s => new { s.Key.Product }) .ToListAsync(); var getSubCodePro = await _context.CodePromotion .Where(w => w.SubId == goodCateCode.SubId) .GroupBy(g => new { g.SubCodePro }) .Select(s => new { s.Key.SubCodePro }) .ToListAsync(); goodCateCode.GoodCateName = getCate[0].Product; goodCateCode.SubCodePro = getSubCodePro[0].SubCodePro; _context.GoodCateCode.Add(goodCateCode); await _context.SaveChangesAsync(); return(CreatedAtAction("GetGoodCateCode", new { id = goodCateCode.Id }, goodCateCode)); }
public async Task <IActionResult> PutGoodCateCode([FromRoute] int id, [FromBody] GoodCateCode goodCateCode) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != goodCateCode.Id) { return(BadRequest()); } var getProduct = await _context.GetTransactionInv .Where(w => w.ProductCode == goodCateCode.GoodCatecode) .GroupBy(g => new { g.Product }) .Select(s => new { s.Key.Product }) .ToListAsync(); var getSubCodePro = await _context.CodePromotion .Where(w => w.SubId == goodCateCode.SubId) .GroupBy(g => new { g.SubCodePro }) .Select(s => new { s.Key.SubCodePro }) .ToListAsync(); goodCateCode.GoodCateName = getProduct[0].Product; goodCateCode.SubCodePro = getSubCodePro[0].SubCodePro; _context.Entry(goodCateCode).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!GoodCateCodeExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }