public DeleteAdModel DeleteAd(long adId)
        {
            DeleteAdModel result = new DeleteAdModel();
            result.AdId = adId;
            result.CanDeleteAd = _adRepository.CanDeleteAd(adId);

            if (!result.CanDeleteAd)
                result.InfoMessage = "Cette annonce n'existe pas ou plus.";

            return result;
        }
        public DeleteAdModel DeleteAd(DeleteAdModel model)
        {
            DeleteAdModel result = new DeleteAdModel();
            result.AdId = model.AdId;

            BaseAd ad = _adRepository.GetAdById<BaseAd>(model.AdId);

            if (ad == null || ad.IsDeleted)
            {
                result.InfoMessage = "Cette annonce n'existe pas ou plus.";
                result.CanDeleteAd = false;
                return result;
            }

            if (model.Password != ad.CreatedBy.Password)
            {
                result.NbTry = ++model.NbTry;
                result.SelectedDeletionReasonId = model.SelectedDeletionReasonId;
                result.CanDeleteAd = true;
                return result;
            }

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
            {
                ad.IsDeleted = true;
                ad.DeletionDate = _helper.GetCurrentDateTime();
                if (model.SelectedDeletionReasonId.HasValue)
                    ad.DeletedReason = _repository.Get<DeletionReason>(model.SelectedDeletionReasonId);
                _repository.Save<BaseAd>(ad);

                SearchAdCache adCache = _repository.Get<SearchAdCache>(ad.Id);
                if (adCache != null)
                    _repository.Delete<SearchAdCache>(adCache);

                result.IsDeleted = true;
                result.InfoMessage = "Votre annonce a correctement été supprimée. Elle n'est plus disponible à la recherche.";

                scope.Complete();
            }

            return result;
        }
        public void PerformDeleteAd_AdDoesNotExists_ReturnMessage()
        {
            // Given
            DeleteAdModel model = new DeleteAdModel
            {
                AdId = 56
            };

            var adRepoMock = new Moq.Mock<IAdRepository>();
            adRepoMock.Setup(r => r.GetAdById<BaseAd>(model.AdId)).Returns((BaseAd)null);

            AdDeletionServices service = new AdDeletionServices(adRepoMock.Object, null, null);

            // When
            DeleteAdModel result = service.DeleteAd(model);

            // Then
            Assert.AreEqual(model.AdId, result.AdId);
            Assert.IsNull(result.Password);
            Assert.IsNull(result.SelectedDeletionReasonId);
            Assert.AreEqual(0, result.NbTry);
            Assert.AreEqual("Cette annonce n'existe pas ou plus.", result.InfoMessage);
            Assert.IsFalse(result.CanDeleteAd);
            Assert.IsFalse(result.IsDeleted);
        }
        public void PerformDeleteAd_AdExists_PasswordMatches_MarkAdAsDeletedAndRemoveFromSearchAdCache()
        {
            // Given
            DeleteAdModel model = new DeleteAdModel
            {
                AdId = 56,
                Password = "******",
                NbTry = 1,
                SelectedDeletionReasonId = 7
            };
            DeletionReason dr = new DeletionReason() { Id = 7 };
            SearchAdCache adc = new SearchAdCache() { AdId = 56 };
            User creator = new User()
            {
                Password = "******"
            };
            BaseAd ad = new Ad()
            {
                Id = 56,
                CreatedBy = creator
            };

            var adRepoMock = new Moq.Mock<IAdRepository>();
            adRepoMock.Setup(r => r.GetAdById<BaseAd>(model.AdId)).Returns(ad);

            var repoMock = new Moq.Mock<IRepository>();
            repoMock.Setup(x => x.Get<DeletionReason>(7)).Returns(dr);
            repoMock.Setup(x => x.Get<SearchAdCache>(model.AdId)).Returns(adc);
            repoMock.Setup(x => x.Save<BaseAd>(ad));
            repoMock.Setup(x => x.Delete<SearchAdCache>(adc));

            var helperMock = new Moq.Mock<IHelperService>();
            helperMock.Setup(x => x.GetCurrentDateTime()).Returns(new DateTime(2013, 01, 26));

            AdDeletionServices service = new AdDeletionServices(adRepoMock.Object, repoMock.Object, helperMock.Object);

            // When
            DeleteAdModel result = service.DeleteAd(model);

            // Then
            Assert.AreEqual(model.AdId, result.AdId);
            Assert.IsNull(result.Password);
            Assert.IsNull(result.SelectedDeletionReasonId);
            Assert.AreEqual(0, result.NbTry);
            Assert.AreEqual("Votre annonce a correctement été supprimée. Elle n'est plus disponible à la recherche.", result.InfoMessage);
            Assert.IsFalse(result.CanDeleteAd);
            Assert.IsTrue(result.IsDeleted);

            Assert.AreEqual(new DateTime(2013, 01, 26), ad.DeletionDate);
            Assert.IsTrue(ad.IsDeleted);
            Assert.AreEqual(dr, ad.DeletedReason);

            repoMock.Verify(x => x.Save<BaseAd>(ad), Times.Once());
            repoMock.Verify(x => x.Delete<SearchAdCache>(adc), Times.Once());
        }
        public void PerformDeleteAd_AdExists_PasswordDoesNotMatch()
        {
            // Given
            DeleteAdModel model = new DeleteAdModel
            {
                AdId = 56,
                Password = "******",
                NbTry = 1,
                SelectedDeletionReasonId = 7
            };
            User creator = new User()
            {
                Password = "******"
            };
            BaseAd ad = new Ad()
            {
                CreatedBy = creator
            };

            var adRepoMock = new Moq.Mock<IAdRepository>();
            adRepoMock.Setup(r => r.GetAdById<BaseAd>(model.AdId)).Returns(ad);

            AdDeletionServices service = new AdDeletionServices(adRepoMock.Object, null, null);

            // When
            DeleteAdModel result = service.DeleteAd(model);

            // Then
            Assert.AreEqual(model.AdId, result.AdId);
            Assert.IsNull(result.Password);
            Assert.AreEqual(7, result.SelectedDeletionReasonId);
            Assert.AreEqual(2, result.NbTry);
            Assert.IsNull(result.InfoMessage);
            Assert.IsTrue(result.CanDeleteAd);
            Assert.IsFalse(result.IsDeleted);
        }
Beispiel #6
0
        public ActionResult Delete(DeleteAdModel model)
        {
            var result = _adDeletionServices.DeleteAd(model);
            if (result.NbTry > 0)
                ModelState.AddModelError("Password", "Mot de passe incorrect");

            return View(result);
        }