public void UpdateBudget_CanLoadUpdateBudgetType() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseSqlite(connection) .Options; // Insert seed data into the database using one instance of the context using (var context = new ApplicationDbContext(options)) { context.Database.EnsureCreated(); context.Budgets.AddRange( new UserBudget { BudgetId = 1, Owner = "Sofia" }, new UserBudget { BudgetId = 2, Name = "Camilla" }); context.SaveChanges(); } // Use a separate instance edit some data using (var context = new ApplicationDbContext(options)) { var service = new BudgetService(context); UpdateBudgetCommand toUpdate = service.GetBudgetForUpdate(2); Assert.NotNull(toUpdate); } }
public void GetBudgetForUpdate_Returns_UpdateBudgetCommand_Type() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseSqlite(connection) .Options; // Insert seed data into the database using one instance of the context using (var context = new ApplicationDbContext(options)) { context.Database.EnsureCreated(); context.Budgets.AddRange( new UserBudget { BudgetId = 1, Owner = "Sofia", UserId = "123" } ); context.SaveChanges(); } // Use a separate instance edit some data using (var context = new ApplicationDbContext(options)) { var service = new BudgetService(context); UpdateBudgetCommand obj = service.GetBudgetForUpdate(1); Assert.NotNull(obj); Assert.Equal("Sofia", obj.Owner); Assert.Equal("123", obj.UserId); Assert.NotEqual("Linda", obj.Owner); } }
[HttpPost("edit/{id}")] //need the id parameter to check EnsureBudgetExistAttribute public IActionResult Edit(int id, [FromBody] UpdateBudgetCommand cmd) { _budgetService.UpdateBudget(cmd); var newBudget = _budgetService.GetBudget(cmd.BudgetId); return(Ok(newBudget)); }
public Budget Update(UpdateBudgetCommand command) { var budget = _repository.GetOne(command.Id); if (!string.IsNullOrEmpty(command.Proposal)) { budget.ChangeProposal(command.Proposal); } if (command.Price != 0) { budget.ChangePrice(command.Price); } if (command.Status != 0) { budget.ChangeStatus(command.Status); } if (command.SessionPrice != 0) { budget.ChangeSessionPrice(command.SessionPrice); } _repository.Update(budget); if (Commit()) { return(budget); } return(null); }
public UpdateBudgetCommandTest() { _budgetViewModel = new BudgetViewModel { Income = 45.3m, StartDate = new DateTime(2014, 8, 1), Categories = new[] { new BudgetCategoryViewModel { Name = "Home", LineItems = new[] { new BudgetLineItemViewModel { Actual = 54, Estimate = 23, Name = "Mortgage" } } } } }; _budget = new Budget { StartDate = new DateTime(_budgetViewModel.Year, _budgetViewModel.Month, 1), Categories = new List <Category>() }; _budgetRepository = new InMemoryRepository <Budget>(); _budgetRepository.Insert(_budget).Wait(); _updateBudgetCommand = new UpdateBudgetCommand(_budgetRepository); }
[HttpPost("edit/{id}")] //need the id parameter to check EnsureBudgetExistAttribute public async Task <IActionResult> Edit(int id, [FromBody] UpdateBudgetCommand cmd) { var budget = _budgetService.GetBudget(id); var authResult = await _authService.AuthorizeAsync(User, budget, "CanViewBudget"); if (!authResult.Succeeded) { return(Forbid()); } _budgetService.UpdateBudget(cmd); var newBudget = _budgetService.GetBudget(cmd.BudgetId); return(Ok(newBudget)); }
public Task <HttpResponseMessage> Put([FromBody] dynamic body) { var commandBudget = new UpdateBudgetCommand( id: Guid.Parse((string)body.id), proposal: (string)body.proposal, price: (float)body.price, status: (EBudgetStatus)body.status, proposalDate: (DateTime)body.proposalDate, sessionPrice: (float)body.sessionPrice, idCoachingProcess: (Guid)body.idCoachingProcess ); var budget = _serviceBudget.Update(commandBudget); return(CreateResponse(HttpStatusCode.Created, budget)); }
public void UpdateBudget(UpdateBudgetCommand cmd) { var budget = _context.Budgets.Find(cmd.BudgetId); if (budget == null) { throw new Exception("Unable to find the budget list"); } if (budget.IsDeleted) { throw new Exception("Unable to update a deleted budget list"); } _context.Entry(budget).State = EntityState.Detached; cmd.UpdateBudget(budget); InsertOrUpdateBudget(budget); }
public async Task <IActionResult> EditBudget(UpdateBudgetCommand command) { var budget = _service.GetBudget(command.BudgetId); var authResult = await _authService.AuthorizeAsync(User, budget, "CanViewBudget"); if (!authResult.Succeeded) { _log.LogWarning($"{_userService.GetUserName(User)} attempted to change budget#{command.BudgetId}, but wasn't authorized."); return(Forbid()); } if (!ModelState.IsValid) { return(View(command)); } _service.UpdateBudget(command); return(RedirectToAction(nameof(ViewBudget), new { id = command.BudgetId })); }