Ejemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] CustomerVM cust)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                Customer customer = _mapper.Map <CustomerVM, Customer>(cust);

                _logger.LogTrace(LoggingEvents.Trace, String.Format("Adding new Customer:{0}", customer.name));

                await _custrepo.AddAsync(customer);

                _logger.LogInformation(LoggingEvents.Critical, String.Format("Added new CustomerID:{0}-{1}", customer.entityid, customer.name));

                return(Ok(customer.entityid));
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.Error, ex, String.Format("ERROR: Unable to add customer"));
            }

            return(BadRequest("Error: Unable to add customer"));
        }
Ejemplo n.º 2
0
        public async Task <Translation> ExecuteAsync(Translation translation)
        {
            SetPredicate(translation);
            InsureTranslationsCanBeInserted(translation);
            Log.Debug($"Adding translation async: {translation.ToString()}");
            await _mongoRepository.AddAsync(translation);

            return(translation);
        }
Ejemplo n.º 3
0
        public async void CanAddDocumentAsync()
        {
            var customerRepository = new MongoRepository <Customer>();

            await customerRepository.AddAsync(this.TestCustomers[0]);

            this.TestCustomers[0].Id.ShouldNotBeNullOrWhiteSpace();

            await customerRepository.DeleteAsync(this.TestCustomers[0]);
        }
Ejemplo n.º 4
0
        public async void CanDeleteAllDocumentsAsync()
        {
            var customerRepository = new MongoRepository <Customer>();

            await customerRepository.AddAsync(this.TestCustomers);

            await customerRepository.DeleteAllAsync();

            customerRepository.Count().ShouldBe(0);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 添加一条评论记录
 /// </summary>
 /// <param name="info">数据</param>
 /// <returns></returns>
 public async Task <int> AddUserCommAsync(UserComm info)
 {
     try
     {
         return(await _mongoRespository.AddAsync(info));
     }
     catch (Exception ex)
     {
         _log.Error("AddUserCommAsync method error:" + ex);
         throw;
     }
 }
Ejemplo n.º 6
0
        public async void CanCountDocumentsAsync()
        {
            var customerRepository = new MongoRepository <Customer>();

            await customerRepository.AddAsync(this.TestCustomers);

            var count = await customerRepository.CountAsync();

            count.ShouldBe(this.TestCustomers.Count);

            await customerRepository.DeleteAsync(this.TestCustomers);
        }
Ejemplo n.º 7
0
        public async void CanCheckIfDocumentExistsAsync()
        {
            var customerRepository = new MongoRepository <Customer>();

            await customerRepository.AddAsync(this.TestCustomers);

            var exists = await customerRepository.ExistsAsync(x => x.Id == this.TestCustomers[0].Id);

            exists.ShouldBeTrue();

            await customerRepository.DeleteAsync(this.TestCustomers);
        }
Ejemplo n.º 8
0
        public async Task CopyFileAsync(string srcPath, string dstPath)
        {
            var fileEntity = await GetFileEntityByPath(srcPath);

            try
            {
                // 取得流=>取得新文件名=>保存新文件=>取得新文件信息=>保存新文件信息
                var stream = await _mongoRepository.GetFileStreamAsync(fileEntity._id);

                var newFileName = dstPath.Split('/').Last();
                var newid       = await _mongoRepository.UploadFileAsync(newFileName, stream);

                var fileinfo = await _mongoRepository.GetFileByIdAsync(newid);

                await _mongoRepository.AddAsync(new MongoDBFileStoreEntry(fileinfo, dstPath));
            }
            catch (Exception e)
            {
                _logger.LogError(e.StackTrace);
            }
        }
        public async Task CustomerMasterRepositoryTest001_CreateFindDeleteAsync_ExpectNoExceptions()
        {
            // Test cases for async API

            await repo.DeleteAllAsync();

            Assert.Equal(0, repo.Count());

            // Add an entity
            Customer entity = new Customer("CustomerMasterRepositoryTest001_cname", "1-800-start");
            await repo.AddAsync(entity);

            this.testLogger.LogDebug($"New entity: {entity.ToJson()}");

            // Count should increase by 1
            Assert.Equal(1, await repo.CountAsync());

            // Test get by id
            var fetch = await repo.GetByEntityIdAsync(entity.entityid);

            Assert.NotNull(fetch);
            // Assert.Equal(fetch,entity);

            // Test search API
            var searchresult = await repo.GetAsync(e => e.phone == "1-800-start");

            Assert.Equal(1, searchresult.Count);

            // Test Update API
            entity.phone = "1-800-updated";
            await repo.UpdateAsync(entity);

            Assert.Equal(1, (await repo.GetAsync(e => e.phone == "1-800-updated")).Count);

            await repo.DeleteAsync(entity.entityid);

            await Assert.ThrowsAsync <Exception>(async() => fetch = await repo.GetByEntityIdAsync(entity.entityid));
        }
Ejemplo n.º 10
0
        public async void CanDeleteDocumentByExpressionAsync()
        {
            var customerRepository = new MongoRepository <Customer>();

            await customerRepository.AddAsync(this.TestCustomers);

            await customerRepository.DeleteAsync(x => x.Id == this.TestCustomers[0].Id);

            var document = await customerRepository.GetByIdAsync(this.TestCustomers[0].Id);

            document.ShouldBeNull();

            await customerRepository.DeleteAsync(this.TestCustomers);
        }
Ejemplo n.º 11
0
        public async void CanUpdateDocumentAsync()
        {
            var customerRepository = new MongoRepository <Customer>();

            await customerRepository.AddAsync(this.TestCustomers);

            this.TestCustomers[0].LastName = "Updated LastName";
            await customerRepository.UpdateAsync(this.TestCustomers[0]);

            var document = await customerRepository.GetByIdAsync(this.TestCustomers[0].Id);

            document.ShouldNotBeNull();
            document.Id.ShouldBe(this.TestCustomers[0].Id);
            document.LastName.ShouldBe("Updated LastName");

            await customerRepository.DeleteAsync(this.TestCustomers);
        }
Ejemplo n.º 12
0
        public async Task <TrackingUrlEntity> AddAsync(TrackingUrlEntity entity)
        {
            if (string.IsNullOrEmpty(entity.Id))
            {
                // Generating new integer id
                entity.Id = _idGenerator.GenerateId().ToString(CultureInfo.InvariantCulture);
            }

            TrackingUrlEntity result;

            try
            {
                result = await _collection.AddAsync(entity);
            }
            catch (Exception e)
            {
                throw new ApplicationException(string.Format("Mongo driver failure: {0}", e));
            }

            return(result);
        }
Ejemplo n.º 13
0
        public async void CanUpdateMultipleDocumentsAsync()
        {
            var customerRepository = new MongoRepository <Customer>();

            await customerRepository.AddAsync(this.TestCustomers);

            this.TestCustomers[0].LastName = "Updated LastName 1";
            this.TestCustomers[1].LastName = "Updated LastName 2";
            await customerRepository.UpdateAsync(this.TestCustomers);

            var document1 = await customerRepository.GetByIdAsync(this.TestCustomers[0].Id);

            document1.ShouldNotBeNull();
            document1.Id.ShouldBe(this.TestCustomers[0].Id);
            document1.LastName.ShouldBe("Updated LastName 1");

            var document2 = await customerRepository.GetByIdAsync(this.TestCustomers[1].Id);

            document2.ShouldNotBeNull();
            document2.Id.ShouldBe(this.TestCustomers[1].Id);
            document2.LastName.ShouldBe("Updated LastName 2");

            await customerRepository.DeleteAsync(this.TestCustomers);
        }
Ejemplo n.º 14
0
        protected override async Task <bool> ActAsync(MongoRepository <SomeEntity> subject)
        {
            await subject.AddAsync(_entity, CancellationToken);

            return(true);
        }
Ejemplo n.º 15
0
        public void StartServer(CancellationToken cancellationToken)
        {
            this._logger.LogDebug(LoggingEvents.Debug, "Started Data Replication Server");
            Console.WriteLine(" *** Started App Data Replication Server ***");


            // This is the event handler for emailNotification queue. Make sure this does not throw exception
            // Kafka message handling block would not be responsible to handle any exceptions
            Func <KMessage <AppEventArgs <Customer> >, Task> appEventHandler = async(message) =>
            {
                this._logger.LogTrace(LoggingEvents.Trace, $"Response: Partition:{message.Partition}, Offset:{message.Offset} :: {message.Message}");

                AppEventArgs <Customer> evt = message.Message;

                try {
                    Customer customer = evt.afterChange;

                    switch (evt.appEventType)
                    {
                    case AppEventType.Insert:
                        _logger.LogTrace(LoggingEvents.Trace, String.Format("Adding new Customer:{0}", customer.name));
                        await _custrepo.AddAsync(customer);

                        await this._searchrepo.AddAsync(customer);

                        break;

                    case AppEventType.Delete:
                        var cust = await _custrepo.GetByEntityIdAsync(customer.entityid);

                        if (cust == null)
                        {
                            _logger.LogTrace(LoggingEvents.Trace, $"Trying to delete Customer {customer.name} with EmtityID: {customer.entityid} that does not exist");
                        }
                        else
                        {
                            await _custrepo.DeleteAsync(customer.entityid);
                        }

                        if (this._searchrepo.Exists(customer.id))
                        {
                            await this._searchrepo.DeleteAsync(customer.id);
                        }

                        break;

                    case AppEventType.Update:
                        _logger.LogTrace(LoggingEvents.Trace, $"Processing request to update customer:{customer.entityid}");
                        await _custrepo.UpdateAsync(customer);

                        await _searchrepo.UpdateAsync(customer);

                        break;

                    default:
                        _logger.LogTrace(LoggingEvents.Trace, $"No action required for event:{evt.id} of type:{evt.appEventType}");
                        break;
                    }

                    this._logger.LogDebug(LoggingEvents.Trace, $"Processed Customer CRUD event {evt.id}");
                }
                catch (Exception ex) {
                    var msg = $"Event:{evt.id} - Error:{ex.Message}";

                    this._logger.LogError(LoggingEvents.Error, ex, msg);

                    // We will send out a notification for every update
                    var notifyEvt = new EmailEventArgs {
                        subject  = "Data Replication Error",
                        textMsg  = $"Error replicating customer information. {msg}",
                        htmlMsg  = $"<p> Error replicating customer information.  </p><p> <b>Message Details: </b> {msg}  <p>",
                        notifyTo = new List <string>()
                        {
                            "*****@*****.**"
                        },
                        notifyCC  = new List <string>(),
                        notifyBCC = new List <string>()
                    };

                    await this._notificationProducer.ProduceAsync(this._notificationMsgQueueTopic, notifyEvt);
                }
            };

            this._appEventMsgConsumer.Consume(cancellationToken, appEventHandler, null, null);

            this._appEventMsgConsumer.Dispose();
            Console.WriteLine(" *** Stopped App Data Replication Server ***");

            this._logger.LogDebug(LoggingEvents.Debug, "Stopped App Data Replication Server");
        }