public async Task <ActionResult> AddSpending(string monzoTransId, string name, decimal amount, string date, bool isFinance, int selectedId, int?secondCatId = null, bool cashExpense = false) { var dto = new SpendingDTO { Name = name, Amount = amount, SecondCatId = secondCatId, Date = DateTime.ParseExact(date, "yyyy-MM-ddTHH:mm", new CultureInfo("en-GB")), MonzoTransId = monzoTransId, CashExpense = cashExpense }; dto.FinanceId = !isFinance ? (int?)null : selectedId; dto.CatId = !isFinance ? selectedId : (int?)null; if (dto.FinanceId.HasValue && dto.CatId.HasValue) { dto.CatId = null; } if (!dto.FinanceId.HasValue && !dto.CatId.HasValue) { throw new ApplicationException("Must have FinanceId or catId"); } await monzoService.AddSpending(dto); await monzoService.DeleteMonzoTransaction(monzoTransId); return(View("Close")); }
public async Task <IActionResult> UpdateSpending(int id, SpendingDTO spendingDTO) { if (id != spendingDTO.Id) { return(BadRequest()); } var spending = await _context.Spendings.FindAsync(id); if (spending == null) { return(NotFound()); } spending.Id = spendingDTO.Id; spending.Description = spendingDTO.Description; spending.Price = spendingDTO.Price; spending.Rating = spendingDTO.Rating; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) when(!SpendingExists(id)) { return(NotFound()); } return(NoContent()); }
public async Task TestEditSpedningGoodData() { ACMDbContext context = ACMDbContextInMemoryFactory.InitializeContext(); SpendingService spendingService = new SpendingService(context); Spending spending = new Spending { Amount = 10, Text = "beer", IsPayed = true }; await context.Spendings.AddAsync(spending); await context.SaveChangesAsync(); SpendingDTO model = new SpendingDTO { Amount = 100, Text = "alot of beer", IsPayed = false, Id = spending.Id }; bool output = await spendingService.EditSpending(model); Assert.True(output); Assert.Equal(100, context.Spendings.Where(x => x.Id == spending.Id).FirstOrDefault().Amount); Assert.Equal("alot of beer", context.Spendings.Where(x => x.Id == spending.Id).FirstOrDefault().Text); Assert.False(context.Spendings.Where(x => x.Id == spending.Id).FirstOrDefault().IsPayed); }
public IActionResult Edit(string id) { SpendingDTO model = spendingService.GetOneSpending(id); if (model == null) { return(Redirect("/Spendings/All")); } return(View(model)); }
public async Task <IActionResult> Create(SpendingDTO model) { if (ModelState.IsValid) { await spendingService.CreateSpending(model); return(Redirect("/Spendings/All")); } return(View(model)); }
public async Task <IActionResult> Edit(SpendingDTO model) { if (ModelState.IsValid) { if (await spendingService.EditSpending(model)) { return(Redirect("/Spendings/All")); } } return(View(model)); }
public async Task <string> CreateSpending(SpendingDTO model) { Spending spending = new Spending { Amount = model.Amount, Text = model.Text, IsPayed = model.IsPayed }; await context.Spendings.AddAsync(spending); await context.SaveChangesAsync(); return(spending.Id); }
public async Task <bool> EditSpending(SpendingDTO model) { Spending spending = context.Spendings .Where(x => x.Id == model.Id) .FirstOrDefault(); if (spending == null) { throw new Utilities.ACMException(); } spending.Amount = model.Amount; spending.IsPayed = model.IsPayed; spending.Text = model.Text; await context.SaveChangesAsync(); return(true); }
public async Task TestCreateSpending() { ACMDbContext context = ACMDbContextInMemoryFactory.InitializeContext(); SpendingService spendingService = new SpendingService(context); SpendingDTO model = new SpendingDTO { Amount = 10, Text = "beer", IsPayed = true }; string id = await spendingService.CreateSpending(model); Assert.Single(context.Spendings.ToList()); Assert.True(context.Spendings.Any(x => x.Id == id)); Assert.Equal("beer", context.Spendings.Where(x => x.Id == id).FirstOrDefault().Text); Assert.Equal(10, context.Spendings.Where(x => x.Id == id).FirstOrDefault().Amount); Assert.True(context.Spendings.Where(x => x.Id == id).FirstOrDefault().IsPayed); }
public async Task <ActionResult <SpendingDTO> > CreateSpending(SpendingDTO spendingDTO) { var spending = new Spending { Id = spendingDTO.Id, Description = spendingDTO.Description, Price = spendingDTO.Price, Rating = spendingDTO.Rating }; _context.Spendings.Add(spending); await _context.SaveChangesAsync(); return(CreatedAtAction( nameof(GetSpending), new { id = spending.Id }, SpendingToDTO(spending))); }
public SpendingDTO GetOneSpending(string id) { SpendingDTO model = context.Spendings .Where(x => x.Id == id) .Select(x => new SpendingDTO { Id = x.Id, Amount = x.Amount, IsPayed = x.IsPayed, IssuedOn = x.IssuedOn, Text = x.Text }).FirstOrDefault(); if (model == null) { throw new ACMException(); } return(model); }
public async Task TestGetOneSpendingGoodData() { ACMDbContext context = ACMDbContextInMemoryFactory.InitializeContext(); SpendingService spendingService = new SpendingService(context); Spending spending = new Spending { Amount = 10, Text = "beer", IsPayed = true }; await context.Spendings.AddAsync(spending); await context.SaveChangesAsync(); SpendingDTO output = spendingService.GetOneSpending(spending.Id); Assert.Equal(spending.Id, output.Id); Assert.Equal(10, output.Amount); Assert.Equal("beer", output.Text); Assert.True(output.IsPayed); }
public async Task <HttpResponseMessage> InsertAsync(SpendingDTO dto) { // hack for now if (dto.FinanceId == 0) { dto.FinanceId = null; } if (dto.CatId == 0) { dto.CatId = null; } if (dto.FinanceId.HasValue && dto.CatId.HasValue) { dto.CatId = null; } if (!dto.FinanceId.HasValue && !dto.CatId.HasValue) { return(Request.CreateResponse(HttpStatusCode.BadRequest, false)); } if (dto.CatId == (int)Categories.Savings) { var potIncomeDto = new IncomeDTO { Amount = dto.Amount, SourceId = (int)Categories.SavingsPot, Date = dto.Date, }; await incomeService.InsertIncomeAsync(potIncomeDto); } await spendingService.InsertAsync(dto); return(Request.CreateResponse(HttpStatusCode.OK, true)); }
public async Task TestEditSpedningInvalidId() { ACMDbContext context = ACMDbContextInMemoryFactory.InitializeContext(); SpendingService spendingService = new SpendingService(context); Spending spending = new Spending { Amount = 10, Text = "beer", IsPayed = true }; await context.Spendings.AddAsync(spending); await context.SaveChangesAsync(); SpendingDTO model = new SpendingDTO { Amount = 100, Text = "alot of beer", IsPayed = false, Id = spending.Id + "Random string" }; await Assert.ThrowsAsync <ACMException>(() => spendingService.EditSpending(model)); }