public ActionResult SaveBy([FromBody] dynamic data)
        {
            var result = new ValidResult();

            Book        condition   = JsonConvert.DeserializeObject <Book>(JsonConvert.SerializeObject(data.conditionForFilter));
            List <Book> clientBooks = JsonConvert.DeserializeObject <List <Book> >(JsonConvert.SerializeObject(data.books));

            var emptyBooks = clientBooks.Where(item => item.IsEmptyInstance()).ToList();

            foreach (var bookFromEmpty in emptyBooks)
            {
                clientBooks.Remove(bookFromEmpty);
            }

            foreach (var clientBook in clientBooks)
            {
                clientBook.Recorder = HttpContext.User.Claims.FirstOrDefault(p => p.Type == ClaimTypes.Name).Value;
            }

            if (!result.IsValid)
            {
                return(BadRequest(result));
            }

            var dataBaseBooks = this.FetchBy(condition);

            try
            {
                bookRepository.Delete(condition);

                if (clientBooks.Count() > 0)
                {
                    bookRepository.CreateAll(clientBooks).Wait();
                }
            }
            catch (Exception exception)
            {
                logger.LogError(JsonConvert.SerializeObject(dataBaseBooks));
                throw exception;
            }

            return(Ok(result));
        }
Exemple #2
0
        public void Execution(string[] args)
        {
            logger.Debug($"this.localMongoDBOptions.ConnectionString: {this.localMongoDBOptions.ConnectionString}");
            logger.Debug($"this.remoteMongoDBOptions.ConnectionString: {this.remoteMongoDBOptions.ConnectionString}");

            var localAccountingSubjectRepository  = new AccountingSubjectRepository(this.localMongoDBOptions);
            var remoteAccountingSubjectRepository = new AccountingSubjectRepository(this.remoteMongoDBOptions);

            var accountingSubjects = remoteAccountingSubjectRepository.FetchAll().Result;

            logger.Debug($"accountingSubjects.Count(): {accountingSubjects.Count()}");

            localAccountingSubjectRepository.DeleteAll().Wait();
            logger.Debug($"localAccountingSubjectRepository.DeleteAll().Wait();");

            localAccountingSubjectRepository.CreateAll(accountingSubjects).Wait();
            logger.Debug($"localAccountingSubjectRepository.CreateAll(accountingSubjects).Wait();");

            var localAuthorizationRepository  = new AuthorizationRepository(this.localMongoDBOptions);
            var remoteAuthorizationRepository = new AuthorizationRepository(this.remoteMongoDBOptions);

            var authorizations = remoteAuthorizationRepository.FetchAll().Result;

            logger.Debug($"authorizations.Count(): {authorizations.Count()}");

            localAuthorizationRepository.DeleteAll().Wait();
            logger.Debug($"localAuthorizationRepository.DeleteAll().Wait();");

            localAuthorizationRepository.CreateAll(authorizations).Wait();
            logger.Debug($"localAuthorizationRepository.CreateAll(authorizations).Wait();");

            var localBookRepository  = new BookRepository(this.localMongoDBOptions);
            var remoteBookRepository = new BookRepository(this.remoteMongoDBOptions);

            var books = remoteBookRepository.FetchAll().Result;

            logger.Debug($"books.Count(): {books.Count()}");

            if (books.Count() > 0)
            {
                localBookRepository.DeleteAll().Wait();
                logger.Debug($"localBookRepository.DeleteAll().Wait();");

                localBookRepository.CreateAll(books).Wait();
                logger.Debug($"localBookRepository.CreateAll(books).Wait();");
            }

            var localDetailRepository  = new DetailRepository(this.localMongoDBOptions);
            var remoteDetailRepository = new DetailRepository(this.remoteMongoDBOptions);

            var packageInformationIds = localDetailRepository.GetPackageInformationIds();

            logger.Debug($"packageInformationIds.Count(): {packageInformationIds.Count()}");

            var details = remoteDetailRepository.FetchAll(item => !packageInformationIds.Contains(item.PackageInformation.Id)).Result;

            logger.Debug($"details.Count(): {details.Count()}");

            foreach (var detail in details)
            {
                if (localDetailRepository.Exist(detail.Id))
                {
                    localDetailRepository.Update(detail);
                }
                else
                {
                    localDetailRepository.Create(detail).Wait();
                }
            }
            logger.Debug($"localDetailRepository.CreateAll(details).Wait();");
        }