public async Task CreateAsync(DetailBeli obj) { try { if (obj.Tanggal == DateTime.Today) { obj.TotalHarga = obj.Harga * obj.Qty; _context.Add(obj); //perlu looping //search idbeli then dapet value total e berapa ? Beli datapembelian = new Beli(); datapembelian = await _Beli.GetById(obj.IdBeli); datapembelian.TotalHarga = datapembelian.TotalHarga + obj.TotalHarga; var update = _Beli.UpdateAsync(datapembelian); //mendapatkan data barang berdasarkan input ID //ketika data barang ada maka stok nya di update dengan stok awal + qty yang akan dinput var data = await _Barang.GetById(obj.IdBarang); data.Stok = Convert.ToInt16(obj.Qty + data.Stok); await _Barang.UpdateAsync(data); } else { throw new Exception("Data di input di tanggal pembelian yang berbeda"); } } catch (Exception ex) { throw new Exception(ex.InnerException.Message); } await _context.SaveChangesAsync(); }
public async Task <IActionResult> Put([FromBody] Beli Beli) { try { await _Beli.UpdateAsync(Beli); return(Ok("Data berhasil diupdate")); } catch (Exception ex) { return(BadRequest(ex.Message)); } }