Ejemplo n.º 1
0
        public async Task <IActionResult> CreateEntryAsync(string id, [FromBody, Required] LexEntryDto entryDto)
        {
            if (!(await ProjectRepo.TryGetAsync(id)).TryResult(out LexProject project))
            {
                return(NotFound());
            }

            IRepository <LexEntry> lexEntryRepo = _lexEntryRepoFactory.Create(project);
            LexEntry entry  = Map <LexEntry>(entryDto);
            var      now    = DateTime.UtcNow;
            string   userId = UserId;

            entry.AuthorInfo.CreatedByUserRef  = userId;
            entry.AuthorInfo.CreatedDate       = now;
            entry.AuthorInfo.ModifiedByUserRef = userId;
            entry.AuthorInfo.ModifiedDate      = now;
            if (await lexEntryRepo.InsertAsync(entry))
            {
                entryDto = Map <LexEntryDto>(entry, RouteNames.LexEntry, new { id, entryId = entry.Id });
                return(Created(entryDto.Href, entryDto));
            }
            return(StatusCode(StatusCodes.Status409Conflict));
        }
 public static IRepository <T> Create <T>(this IProjectRepositoryFactory <T> factory, Project project)
     where T : IEntity
 {
     return(factory.Create(project.ProjectCode));
 }
Ejemplo n.º 3
0
        public async Task RunAsync(PerformContext context, IJobCancellationToken cancellationToken, string userId,
                                   string jobId)
        {
            SendReceiveJob job = await _jobRepo.UpdateAsync(j => j.Id == jobId, u => u
                                                            .Set(j => j.BackgroundJobId, context.BackgroundJob.Id)
                                                            .Set(j => j.State, SendReceiveJob.SyncingState));

            if (job == null)
            {
                return;
            }

            try
            {
                SendReceiveOptions options = _options.Value;
                if ((await _userRepo.TryGetAsync(userId)).TryResult(out User user))
                {
                    if ((await _projectRepo.TryGetAsync(job.ProjectRef)).TryResult(out TranslateProject project))
                    {
                        if (!Directory.Exists(options.TranslateDir))
                        {
                            Directory.CreateDirectory(options.TranslateDir);
                        }

                        IRepository <TranslateDocumentSet> docSetRepo = _docSetRepoFactory.Create(project);
                        using (var conn = new Connection(new Uri(options.ShareDBUrl)))
                        {
                            await conn.ConnectAsync();

                            ParatextProject        sourceParatextProject = project.Config.Source.ParatextProject;
                            IReadOnlyList <string> sourceBooks           = await _paratextService.GetBooksAsync(user,
                                                                                                                sourceParatextProject.Id);

                            ParatextProject        targetParatextProject = project.Config.Target.ParatextProject;
                            IReadOnlyList <string> targetBooks           = await _paratextService.GetBooksAsync(user,
                                                                                                                targetParatextProject.Id);

                            var booksToSendReceive = new HashSet <string>();
                            booksToSendReceive.UnionWith(sourceBooks);
                            booksToSendReceive.IntersectWith(targetBooks);

                            var booksToDelete = new HashSet <string>();
                            booksToDelete.UnionWith(GetBooksToDelete(project, sourceParatextProject, sourceBooks));
                            booksToDelete.UnionWith(GetBooksToDelete(project, targetParatextProject, targetBooks));

                            int step      = 0;
                            int stepCount = booksToSendReceive.Count + booksToDelete.Count;
                            foreach (string bookId in booksToSendReceive)
                            {
                                if (!BookNames.TryGetValue(bookId, out string name))
                                {
                                    name = bookId;
                                }
                                TranslateDocumentSet docSet = await docSetRepo.UpdateAsync(ds => ds.BookId == bookId,
                                                                                           u => u.SetOnInsert(ds => ds.Name, name)
                                                                                           .SetOnInsert(ds => ds.BookId, bookId)
                                                                                           .Set(ds => ds.IsDeleted, false), true);

                                await SendReceiveBookAsync(user, conn, project, docSet, sourceParatextProject, "source",
                                                           bookId);
                                await SendReceiveBookAsync(user, conn, project, docSet, targetParatextProject, "target",
                                                           bookId);

                                step++;
                                job = await UpdateProgress(job, step, stepCount);
                            }

                            foreach (string bookId in booksToDelete)
                            {
                                TranslateDocumentSet docSet = await docSetRepo.UpdateAsync(ds => ds.BookId == bookId,
                                                                                           u => u.Set(ds => ds.IsDeleted, true));

                                await DeleteBookAsync(conn, project, docSet, sourceParatextProject, "source", bookId);
                                await DeleteBookAsync(conn, project, docSet, targetParatextProject, "target", bookId);

                                step++;
                                job = await UpdateProgress(job, step, stepCount);
                            }

                            await conn.CloseAsync();
                        }

                        job = await _jobRepo.UpdateAsync(job, u => u
                                                         .Set(j => j.State, SendReceiveJob.IdleState)
                                                         .Unset(j => j.BackgroundJobId));

                        await _projectRepo.UpdateAsync(project,
                                                       u => u.Set(p => p.LastSyncedDate, job.DateModified));
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error occurred while executing Paratext S/R job '{Job}'", job.Id);
                await _jobRepo.UpdateAsync(job, u => u
                                           .Set(j => j.State, SendReceiveJob.HoldState)
                                           .Unset(j => j.BackgroundJobId));
            }
        }