public async Task <ActionResult <AttachmentsListResponse> > GetAttachmentsList(AttachmentsListRequest request)
        {
            var entityTypeId = _implementationsContainer.Metadata[request.EntityTypeName].EntityTypeId;

            using (var dbContext = _implementationsContainer.GetLobToolsRepository() as LobToolsDbContext)
            {
                var attachmets = await dbContext.Attachments.Where(x => x.EntityTypeId == entityTypeId && x.Identifier == request.Identifier)
                                 .Select(x => new AttachmentsListResponse.Item
                {
                    Description  = x.Description,
                    EntityTypeId = x.EntityTypeId,
                    Id           = x.Id,
                    Identifier   = x.Identifier,
                    IsDeleted    = x.IsDeleted,
                    Title        = x.Title,
                    Url          = x.Url,
                    FileName     = x.FileName,
                    Size         = x.Size
                })
                                 .ToListAsync();

                return(new AttachmentsListResponse
                {
                    Items = attachmets
                });
            }
        }
        public async Task <ActionResult <LobSummaryResponse> > GetSummary(LobSummaryRequest request)
        {
            var entityTypeId = _implementationsContainer.Metadata[request.EntityTypeName].EntityTypeId;

            using (var dbContext1 = _implementationsContainer.GetLobToolsRepository() as LobToolsDbContext)
                using (var dbContext2 = _implementationsContainer.GetLobToolsRepository() as LobToolsDbContext)
                {
                    var commentCountTask = dbContext1.Comments
                                           .Where(x => x.EntityTypeId == entityTypeId && request.Identifiers.Contains(x.Identifier))
                                           .GroupBy(x => x.Identifier)
                                           .Select(x => new { x.Key, Count = x.Count() })
                                           .ToDictionaryAsync(g => g.Key, g => g.Count);

                    var attachmentCountTask = dbContext2.Attachments
                                              .Where(x => x.EntityTypeId == entityTypeId && request.Identifiers.Contains(x.Identifier))
                                              .GroupBy(x => x.Identifier)
                                              .Select(x => new { x.Key, Count = x.Count() })
                                              .ToDictionaryAsync(g => g.Key, g => g.Count);

                    var commentCounts    = await commentCountTask;
                    var attachmentCounts = await attachmentCountTask;

                    var summaries = new Dictionary <int, LobSummaryInfo>();
                    foreach (var item in commentCounts)
                    {
                        summaries.Add(item.Key, new LobSummaryInfo {
                            CommentsCount = item.Value
                        });
                    }

                    foreach (var item in attachmentCounts)
                    {
                        if (summaries.TryGetValue(item.Key, out var val))
                        {
                            val.AttachmentsCount = item.Value;
                        }
                        else
                        {
                            summaries.Add(item.Key, new LobSummaryInfo {
                                AttachmentsCount = item.Value
                            });
                        }
                    }

                    return(new LobSummaryResponse
                    {
                        EntityTypeName = request.EntityTypeName,
                        Summaries = summaries
                    });
                }
        }
Example #3
0
        public async Task <ActionResult <SaveDraftResponse> > SaveDraft(SaveDraftRequest request)
        {
            using (var dbContext = _implementationsContainer.GetLobToolsRepository() as LobToolsDbContext)
            {
                var draft = new Draft
                {
                    ActionTypeId  = request.ActionTypeId,
                    CreationTime  = DateTime.Now,
                    EntityTypeId  = request.EntityTypeId,
                    Identifier    = request.Identifier ?? Guid.NewGuid(),
                    IsLastVersion = true,
                    JsonData      = request.JsonData,
                    // TODO: The new OwnerUserId can be different from the old one. Is this a problem?
                    OwnerUserId = _usersService.GetUserId(User)
                };
                if (request.Identifier.HasValue)
                {
                    var lastDraft = await dbContext.Drafts.Where(x => x.Identifier == request.Identifier)
                                    .OrderByDescending(x => x.Version).FirstOrDefaultAsync();

                    draft.Version           = lastDraft.Version + 1;
                    lastDraft.IsLastVersion = false;
                }
                else
                {
                    draft.Version = 1;
                }
                dbContext.Drafts.Add(draft);
                await dbContext.SaveChangesAsync();

                return(new SaveDraftResponse
                {
                    Identifier = draft.Identifier,
                    Version = draft.Version
                });
            }
        }
Example #4
0
        public static TLobDbContext GetLobDbContext <TLobDbContext>(this IImplementationsContainer implementationsContainer, HttpContext httpContext)
            where TLobDbContext : DbContext
        {
            if (implementationsContainer == null)
            {
                return(httpContext.RequestServices.GetRequiredService <TLobDbContext>());
            }
            var dbContext = implementationsContainer?.GetLobToolsRepository();

            if (dbContext != null && dbContext is TLobDbContext lobDbContext)
            {
                return(lobDbContext);
            }
            return(httpContext.RequestServices.GetRequiredService <TLobDbContext>());
        }
Example #5
0
        public async Task <ActionResult> SaveAsAttachment(SaveReportAsAttachmentRequest request)
        {
            var report = await _metadataDbContext.ReportDefinitions.FindAsync(request.ReportDefinitionId);

            var(bytes, contentType, fileName) = await ProcessReport(report, request.EntityIdentifier);

            using (var dbContext = _implementationsContainer.GetLobToolsRepository() as LobToolsDbContext)
            {
                var attachment = new Attachment
                {
                    Data         = bytes,
                    Description  = request.Description,
                    EntityTypeId = report.EntityTypeId,
                    Identifier   = request.EntityIdentifier,
                    Title        = request.Title,
                    FileName     = fileName,
                    Size         = bytes.LongLength
                };
                await dbContext.AddAsync(attachment);

                await dbContext.SaveChangesAsync();
            }
            return(Ok());
        }