Пример #1
0
        public async void SubscriberRepositoryTest()
        {
            var repo = new SubscriberRepository(_logger, false);

            var firstSubscriber = new Subscriber("Amir", "*****@*****.**");
            await repo.AddAsync(firstSubscriber);

            var id = firstSubscriber.Id;

            repo.Detach(firstSubscriber);

            var secondSubscriber = new Subscriber("Amir", "*****@*****.**");

            await Assert.ThrowsAsync <Microsoft.EntityFrameworkCore.DbUpdateException>(
                () => repo.AddAsync(secondSubscriber)
                );

            Assert.Throws <Microsoft.EntityFrameworkCore.DbUpdateException>(
                () => {
                repo.Add(secondSubscriber);
            });

            //we should reconnect to see the deleted objects
            repo = new SubscriberRepository(_logger, false);

            repo.Delete(id);

            var count = repo.GetAll().Count();

            Assert.Equal(0, count);
        }
Пример #2
0
        public async Task <IActionResult> Add([FromBody] SubscribeRequest request)
        {
            var result = false;

            try
            {
                _log.LogEntrance(nameof(Add), request.ToString());

                var repository = new SubscriberRepository(_log, false);

                var newSubscriber = new Subscriber(request.FirstName, request.EMail);

                await repository.AddAsync(newSubscriber);

                result = true;

                return(Ok(result));
            }
            catch (Exception ex)
            {
                _log.LogError($"my exception : [{ex.Message}]  [{ex.StackTrace}]");

                return(Ok(false));
            }
            finally
            {
                _log.LogExit(nameof(Add), result.ToString());
            }
        }