/// <summary> /// Update existing charge in the database /// PUT api/charges/5 /// </summary> /// <param name="value"></param> /// <param name="assetID"></param> /// <returns></returns> public Object Put(Charge value, int assetID) { var owner = ObtainCurrentOwner(); var asset = GetAsset(assetID, owner); var charge = asset.Charges.SingleOrDefault(x => x.Id == value.Id); if (charge == null) { return new { status = "KO", message = "Could not load the charge" }; } charge.Amount = value.Amount; charge.Automatic = value.Automatic; charge.Name = value.Name; charge.Notes = value.Notes; charge.Counterparty = value.Counterparty; charge.PaymentDay = value.PaymentDay; charge.AccountNumber = value.AccountNumber; charge.Unit = value.Unit; charge.Start = value.Start; charge.End = value.End; return GetResponse(charge, asset, false); }
public void Post_Test_NewCharge() { ChargesController controler = new ChargesController(); controler.RavenSession = RavenSession; RavenSession.Store(new Owner { Email = "*****@*****.**", Id = 1, Name = "Tester", UserName = "******" }); RavenSession.Store(new Asset { OwnerId = 1, Id = 1, Name = "Asset1" }); RavenSession.SaveChanges(); var newCharge = new Charge { AccountNumber = "1234", Amount = 500, Automatic = false, Counterparty = "Counterparty", Name = "Name", Notes = "Notes", PaymentDay = 1, Unit = 2, }; controler.RavenSession = RavenSession; var result = controler.Post(newCharge, 1); RavenSession.SaveChanges(); var charge = RavenSession.Load<Asset>(1).Charges.First(); Assert.AreEqual(charge.Id, 1); Assert.AreEqual(charge.Name, "Name"); Assert.AreEqual(charge.Notes, "Notes"); Assert.AreEqual(charge.PaymentDay, 1); Assert.AreEqual(charge.Unit, 2); Assert.AreEqual(charge.Amount, 500); }
public Object GetResponse(Charge charge, Asset asset, bool creation) { return new { dto = charge, message = creation ? "Charge added":"Charge updated" }; }
/// <summary> /// Create a new charge and store in the DB /// POST api/charges /// </summary> /// <param name="value"></param> /// <param name="assetID"></param> /// <returns></returns> public Object Post(Charge value, int assetID) { var owner = ObtainCurrentOwner(); var asset = GetAsset(assetID,owner); value.Id = asset.GenerateChargeId(); if (asset.Charges == null) { asset.Charges = new List<Charge>(); } asset.Charges.Add(value); return GetResponse(value, asset, true); }