public async Task <Either <ActionResult, DataBlockViewModel> > Update(Guid id,
                                                                              UpdateDataBlockViewModel updateDataBlock)
        {
            return(await _persistenceHelper
                   .CheckEntityExists <DataBlock>(id)
                   .OnSuccess(CheckCanUpdateReleaseForDataBlock)
                   .OnSuccess(async existing =>
            {
                // TODO EES-753 Alter this when multiple charts are supported
                var infographicChart = existing.Charts.OfType <InfographicChart>().FirstOrDefault();
                var updatedInfographicChart = updateDataBlock.Charts.OfType <InfographicChart>().FirstOrDefault();

                if (infographicChart != null && infographicChart.FileId != updatedInfographicChart?.FileId)
                {
                    var release = GetReleaseForDataBlock(existing.Id);
                    await _releaseFileService.Delete(
                        release.Id,
                        new Guid(infographicChart.FileId)
                        );
                }

                _mapper.Map(updateDataBlock, existing);

                _context.DataBlocks.Update(existing);
                await _context.SaveChangesAsync();

                return await Get(id);
            }));
        }
        public async Task Update()
        {
            var release = new Release();

            var dataBlock = new DataBlock
            {
                Heading       = "Old heading",
                Name          = "Old name",
                HighlightName = "Old highlight name",
                Source        = "Old source",
                Order         = 5,
                Query         = new ObservationQueryContext
                {
                    Filters = new List <Guid>
                    {
                        Guid.NewGuid(),
                    },
                    Indicators = new List <Guid>
                    {
                        Guid.NewGuid(),
                    },
                },
                Table = new TableBuilderConfiguration
                {
                    TableHeaders = new TableHeaders
                    {
                        Rows = new List <TableHeader>
                        {
                            new TableHeader(Guid.NewGuid().ToString(), TableHeaderType.Indicator)
                        },
                        Columns = new List <TableHeader>
                        {
                            new TableHeader(Guid.NewGuid().ToString(), TableHeaderType.Filter)
                        }
                    }
                },
                Charts = new List <IChart>
                {
                    new LineChart
                    {
                        Title  = "Old chart",
                        Height = 400,
                        Width  = 500,
                    }
                },
            };

            var contextId = Guid.NewGuid().ToString();

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddAsync(
                    new ReleaseContentBlock
                {
                    Release      = release,
                    ContentBlock = dataBlock
                }
                    );

                await context.SaveChangesAsync();
            }

            var updateRequest = new UpdateDataBlockViewModel
            {
                Heading       = "New heading",
                Name          = "New name",
                HighlightName = "New highlight name",
                Source        = "New source",
                Query         = new ObservationQueryContext
                {
                    Filters = new List <Guid>
                    {
                        Guid.NewGuid(),
                    },
                    Indicators = new List <Guid>
                    {
                        Guid.NewGuid(),
                    },
                },
                Table = new TableBuilderConfiguration
                {
                    TableHeaders = new TableHeaders
                    {
                        Rows = new List <TableHeader>
                        {
                            new TableHeader(Guid.NewGuid().ToString(), TableHeaderType.Indicator)
                        },
                        Columns = new List <TableHeader>
                        {
                            new TableHeader(Guid.NewGuid().ToString(), TableHeaderType.Filter)
                        }
                    }
                },
                Charts = new List <IChart>
                {
                    new LineChart
                    {
                        Title  = "New chart",
                        Height = 600,
                        Width  = 700,
                    }
                },
            };

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var service = BuildDataBlockService(context);
                var result  = await service.Update(dataBlock.Id, updateRequest);

                Assert.True(result.IsRight);

                Assert.Equal(dataBlock.Id, result.Right.Id);
                Assert.Equal(updateRequest.Heading, result.Right.Heading);
                Assert.Equal(updateRequest.Name, result.Right.Name);
                Assert.Equal(updateRequest.HighlightName, result.Right.HighlightName);
                Assert.Equal(updateRequest.Source, result.Right.Source);
                Assert.Equal(dataBlock.Order, result.Right.Order);

                Assert.Equal(updateRequest.Query, result.Right.Query);
                Assert.Equal(updateRequest.Table, result.Right.Table);
                Assert.Equal(updateRequest.Charts, result.Right.Charts);
            }

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var updatedDataBlock = await context.DataBlocks.FindAsync(dataBlock.Id);

                Assert.Equal(updateRequest.Heading, updatedDataBlock.Heading);
                Assert.Equal(updateRequest.Name, updatedDataBlock.Name);
                Assert.Equal(updateRequest.HighlightName, updatedDataBlock.HighlightName);
                Assert.Equal(updateRequest.Source, updatedDataBlock.Source);

                Assert.Equal(updateRequest.Query, updatedDataBlock.Query);
                Assert.Equal(updateRequest.Table, updatedDataBlock.Table);
                Assert.Equal(updateRequest.Charts, updatedDataBlock.Charts);
            }
        }
        public async Task Update_RemoveOldInfographic()
        {
            var release = new Release();
            var fileId  = Guid.NewGuid();

            var dataBlock = new DataBlock
            {
                Charts = new List <IChart>
                {
                    new InfographicChart
                    {
                        Title  = "Old chart",
                        FileId = fileId.ToString(),
                        Height = 400,
                        Width  = 500,
                    }
                },
            };

            var file = new File
            {
                Id       = fileId,
                Filename = "test-infographic.jpg"
            };

            var contextId = Guid.NewGuid().ToString();

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddAsync(
                    new ReleaseContentBlock
                {
                    Release      = release,
                    ContentBlock = dataBlock
                }
                    );

                await context.AddAsync(file);

                await context.SaveChangesAsync();
            }

            var updateRequest = new UpdateDataBlockViewModel
            {
                Charts = new List <IChart>
                {
                    new LineChart
                    {
                        Title  = "New chart",
                        Height = 600,
                        Width  = 700,
                    }
                },
            };

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var releaseFileService = new Mock <IReleaseFileService>();

                releaseFileService
                .Setup(s => s.Delete(release.Id, fileId, false))
                .ReturnsAsync(Unit.Instance);

                var service = BuildDataBlockService(context, releaseFileService: releaseFileService.Object);
                var result  = await service.Update(dataBlock.Id, updateRequest);

                Assert.True(result.IsRight);

                Assert.Equal(updateRequest.Charts, result.Right.Charts);

                MockUtils.VerifyAllMocks(releaseFileService);
            }
        }
 public async Task <ActionResult <DataBlockViewModel> > UpdateDataBlock(Guid id,
                                                                        UpdateDataBlockViewModel dataBlock)
 {
     return(await _dataBlockService.Update(id, dataBlock)
            .HandleFailuresOrOk());
 }