public async Task <IActionResult> AddPromotion(AddPromotionDto addPromotionDto) { bool result; string message; try { result = await _promotionDbService.AddPromotion(addPromotionDto); if (result) { message = "Successfully added the promotion."; } else { message = "Couldn't add the promotion."; } } catch (Exception e) { return(Problem(e.Message)); } return(Ok(new { Status = result, Message = message })); }
public async Task TestAddPromotionDto() { //SETUP var options = SqliteInMemory.CreateOptions <BookDbContext>(); using var context = new BookDbContext(options); context.Database.EnsureCreated(); var books = context.SeedDatabaseFourBooks(); context.ChangeTracker.Clear(); var utData = context.SetupSingleDtoAndEntities <AddPromotionDto>(); var service = new CrudServicesAsync(context, utData.ConfigAndMapper); //ATTEMPT var dto = new AddPromotionDto { BookId = books[1].BookId, ActualPrice = 1.23m, PromotionalText = "Save!" }; await service.UpdateAndSaveAsync(dto); //VERIFY var book = context.Books.Single(x => x.BookId == books[1].BookId); book.ActualPrice.ShouldEqual(1.23m); book.OrgPrice.ShouldNotEqual(book.ActualPrice); book.PromotionalText.ShouldEqual("Save!"); }
//public async Task<IActionResult> AddPromotion(AddPromotionDto dto) public async Task AddPromotion(AddPromotionDto dto) { //if (!ModelState.IsValid) //{ // return View(dto); //} await _service.UpdateAndSaveAsync(dto); //#C if (!_service.HasErrors) { //return View("BookUpdated", service.Message); } //Error state //_service.CopyErrorsToModelState(ModelState, dto); //return View(dto); }
public async Task <IActionResult> AddPromotion(AddPromotionDto dto, [FromServices] ICrudServicesAsync <BookDbContext> service) { Request.ThrowErrorIfNotLocal(); if (!ModelState.IsValid) { return(View(dto)); } await service.UpdateAndSaveAsync(dto); SetupTraceInfo(); if (!service.HasErrors) { return(View("BookUpdated", new BookUpdatedDto(service.Message, _backToDisplayController))); } //Error state service.CopyErrorsToModelState(ModelState, dto); return(View(dto)); }
public async Task <bool> AddPromotion(AddPromotionDto addPromotionDto) { using (var session = _cassandraDbConnectionProvider.Connect()) { var preparedModifierStatement = await session.PrepareAsync("INSERT INTO modifiers (id, type, values) VALUES (?, ?, ?);"); Dictionary <Guid, string> modifierWithType = new Dictionary <Guid, string>(); foreach (var mod in addPromotionDto.Modifiers) { Guid id = Guid.NewGuid(); modifierWithType.Add(id, mod.Type); var boundModifierStatement = preparedModifierStatement.Bind(id, mod.Type, mod.Values); await _cassandraQueryProvider.ExecuteAsync(session, boundModifierStatement); } var preparedActionStatement = await session.PrepareAsync("INSERT INTO actions (id, type, flat, percentage, freeship, items) VALUES (?, ?, ?, ?, ?, ?);"); Dictionary <Guid, string> actionsWithType = new Dictionary <Guid, string>(); foreach (var action in addPromotionDto.Actions) { Guid id = Guid.NewGuid(); actionsWithType.Add(id, action.Type); var boundActionStatement = preparedActionStatement.Bind(id, action.Type, action.Flat, action.Percentage, action.FreeShip, action.Items); await _cassandraQueryProvider.ExecuteAsync(session, boundActionStatement); } Guid promoId = Guid.NewGuid(); string cql = "INSERT INTO promotions (id, type, description, modifiers, actions) VALUES (?, ?, ?, ?, ?);"; await _cassandraQueryProvider.ExecuteAsync(session, cql, promoId, addPromotionDto.Type, addPromotionDto.Description, modifierWithType, actionsWithType); return(true); } }
//public async Task<IActionResult> AddPromotion(AddPromotionDto dto) public async Task AddPromotion(AddPromotionDto dto) { //if (!ModelState.IsValid) //{ // return View(dto); //} var book = await _repository //#B .FindEntityAsync(dto.BookId); //#B var status = book.AddPromotion( //#D dto.ActualPrice, dto.PromotionalText); //#D if (!status.HasErrors) { await _repository.PersistDataAsync(); //#E //return View("BookUpdated", service.Message); } //Error state //service.CopyErrorsToModelState(ModelState, dto); //return View(dto); }