Example #1
0
        public async Task TestChangePubDateDto()
        {
            //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 <ChangePubDateDto>();
            var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

            //ATTEMPT
            var dto = new ChangePubDateDto {
                BookId = books[3].BookId, PublishedOn = new DateTime(2020, 1, 1)
            };
            await service.UpdateAndSaveAsync(dto);

            //VERIFY
            var book = context.Books.Single(x => x.BookId == books[3].BookId);

            book.PublishedOn.ShouldEqual(new DateTime(2020, 1, 1));
        }
 public void OnGet(int id)
 {
     Data = _service.ReadSingle <ChangePubDateDto>(id);
     if (!_service.IsValid)
     {
         _service.CopyErrorsToModelState(ModelState, Data, nameof(Data));
     }
 }
 public IActionResult ChangePubDate(ChangePubDateDto dto,
                                    [FromServices] IChangePubDateService service)
 {
     Request.ThrowErrorIfNotLocal();
     service.UpdateBook(dto);
     SetupTraceInfo(); //REMOVE THIS FOR BOOK as it could be confusing
     return(View("BookUpdated", "Successfully changed publication date"));
 }
Example #4
0
        public Book Update(ChangePubDateDto changePubDateDto)
        {
            var Book = Context.Find <Book>(changePubDateDto.BookId);

            Book.PublishedOn = changePubDateDto.PublishedOn;
            Context.SaveChanges();
            return(Book);
        }
        public Book UpdateBook(ChangePubDateDto dto)
        {
            var book = _context.Find <Book>(dto.BookId);

            book.UpdatePublishedOn(dto.PublishedOn);
            _context.SaveChanges();
            return(book);
        }
Example #6
0
        public Book UpdateBook(ChangePubDateDto dto)     //#D
        {
            var book = _context.Find <Book>(dto.BookId); //#E

            book.PublishedOn = dto.PublishedOn;          //#F
            _context.SaveChanges();                      //#G
            return(book);                                //#H
        }
Example #7
0
        public IActionResult ChangePubDate(ChangePubDateDto changePubDate, [FromServices] IChangePubDateService service)
        {
            Request.ThrowErrorIfNotLocal();
            var updatedBook = service.UpdateBook(changePubDate);

            SetupTraceInfo();

            return(View("BookUpdated", "Successfully changed publication date"));
        }
Example #8
0
        public Book UpdateBook(ChangePubDateDto changePubDate)
        {
            Book book = dataContext.Books.Find(changePubDate.BookId);

            book.PublishedOn = changePubDate.PublishedOn;

            dataContext.SaveChanges();

            return(book);
        }
        public IActionResult ChangePubDate(ChangePubDateDto dto, [FromServices] IChangePubDateService service)
        {
            if (!ModelState.IsValid)
            {
                return(View(dto));
            }

            service.UpdateBook(dto);
            SetupTraceInfo();
            return(View("BookUpdated", "Successfully changed publication date"));
        }
Example #10
0
 public Book UpdateBook(int id, ChangePubDateDto dto)   
 {
     //Should return error message on not found
     var book = _context.Find<Book>(id);
     if (book == null)
     {
         AddError("Sorry, I could not find the book you were looking for.");
         return null;
     }
     
     book.UpdatePublishedOn(dto.PublishedOn);        
     _context.SaveChanges();                    
     return book;                               
 }
        public Book UpdateBook(ChangePubDateDto dto)       //#F
        {
            var book = _context.Books.SingleOrDefault(     //#G
                x => x.BookId == dto.BookId);              //#G

            if (book == null)                              //#H
            {
                throw new ArgumentException(               //#H
                          "Book not found");               //#H
            }
            book.PublishedOn = dto.PublishedOn;            //#I
            _context.SaveChanges();                        //#J
            return(book);                                  //#K
        }
        public IActionResult ChangePubDate(ChangePubDateDto dto, [FromServices] ICrudServices service)
        {
            if (!ModelState.IsValid)
            {
                return(View(dto));
            }
            service.UpdateAndSave(dto);
            SetupTraceInfo();
            if (service.IsValid)
            {
                return(View("BookUpdated", service.Message));
            }

            //Error state
            service.CopyErrorsToModelState(ModelState, dto);
            return(View(dto));
        }
Example #13
0
        public async Task <IActionResult> ChangePubDate(ChangePubDateDto dto, [FromServices] ICrudServicesAsync <BookDbContext> service)
        {
            if (!ModelState.IsValid)
            {
                return(View(dto));
            }
            await service.UpdateAndSaveAsync(dto);

            SetupTraceInfo();
            if (service.IsValid)
            {
                return(View("BookUpdated", new BookUpdatedDto(service.Message, _backToDisplayController)));
            }

            //Error state
            service.CopyErrorsToModelState(ModelState, dto);
            return(View(dto));
        }
        public void PerformsTheDisconnectedUpdateOfTheBookPublishDate()
        {
            int bookIdToChange;

            // The first connection - pick the book id based on the title.
            using (var context = CreateBookStoreContext())
            {
                var book =
                    context
                    .Books
                    .Single(b => b.Title == "Pro PayPal E-Commerce");

                bookIdToChange = book.BookId;

                book.PublishedOn.Should().NotBeAfter(DateTime.Today, $"because the {nameof(Book)} was published in 2010");
            }

            // Prepare the DTO that the entry will be updated with.
            var dto = new ChangePubDateDto(bookIdToChange, 18.April(2038));

            // The second connection - update the publish date.
            using (var context = CreateBookStoreContext())
            {
                var book = context.Find <Book>(dto.BookId);
                book.PublishedOn = dto.NewDateTime;
                context.SaveChanges().Should().Be(1, "because only one entry has been modified");
            }

            // The third connection - verification.
            using (var context = CreateBookStoreContext())
            {
                context
                .Books
                .Where(b => b.Title == "Pro PayPal E-Commerce")
                .Select(b => b.PublishedOn)
                .Single()
                .Should().Be(dto.NewDateTime, "because has been updated with the DTO");
            }
        }