Ejemplo n.º 1
0
        public void EditAsync_ReturnsCorrectNumberOfModifiedEntries()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;

            using (var dbContext = new ApplicationDbContext(options))
            {
                Phone phone = new Phone()
                {
                    PhoneNumber = "0897248721"
                };
                EditPhoneServiceModel model = new EditPhoneServiceModel
                {
                    Id          = 1,
                    PhoneNumber = "0897248722",
                };
                var phonesService = new PhonesService(dbContext);

                dbContext.Phones.Add(phone);
                dbContext.SaveChanges();

                var result = phonesService.EditAsync(model);

                Assert.Equal(1, result.Result);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Edits the <see cref="Phone"/> using <see cref="EditPhoneServiceModel"/>.
        /// </summary>
        /// <param name="model">Number of modified entities.</param>
        /// <returns>Service model with <c>Id</c> and <c>PhoneNumber</c></returns>
        public async Task <int> EditAsync(EditPhoneServiceModel model)
        {
            Phone phone = this.dbContext.Phones.Find(model.Id);

            phone.PhoneNumber = model.PhoneNumber;

            // TODO: check for duplicates

            int modifiedEntities = await this.dbContext.SaveChangesAsync();

            return(modifiedEntities);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(PhoneInputModel model)
        {
            if (!this.phonesService.Exists(model.Id))
            {
                return(this.BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(this.RedirectToAction("Error", "Home"));
            }

            EditPhoneServiceModel serviceModel = new EditPhoneServiceModel
            {
                Id          = model.Id,
                PhoneNumber = model.PhoneNumber,
            };

            await this.phonesService.EditAsync(serviceModel);

            return(this.RedirectToAction("Details", "Phones", new { id = serviceModel.Id }));
        }