public void ModelUpdateApplyHelper_MultipleFieldUpdate()
        {
            var old     = new Model();
            var initial = JsonSerializer.Serialize(old);
            var update  = new ModelDTO
            {
                Flag = true,
            };

            Assert.NotEqual(update.Flag, old.Flag);

            var(changes, description, fields) = ModelUpdateApplyHelper.ApplyUpdateRequestToModel(old, update);

            Assert.True(changes);
            Assert.NotNull(description);
            Assert.NotEmpty(description);
            Assert.NotEqual(JsonSerializer.Serialize(old), initial);
            Assert.Contains(nameof(Model.SomeField), description);
            Assert.DoesNotContain(nameof(Model.Id), description);
            Assert.Contains(nameof(Model.Flag), description);
            Assert.NotNull(fields);
            Assert.Contains(nameof(Model.SomeField), fields);
            Assert.DoesNotContain(nameof(Model.Id), fields);
            Assert.Contains(nameof(Model.Flag), fields);

            Assert.Equal(update.SomeField, old.SomeField);
            Assert.Equal(update.Flag, old.Flag);
        }
        public void ModelUpdateApplyHelper_BadModelThrows()
        {
            var old    = new BadModel();
            var update = new BadModel();

            Assert.Throws <ArgumentException>(() => ModelUpdateApplyHelper.ApplyUpdateRequestToModel(old, update));
        }
示例#3
0
        public async Task <IActionResult> UpdateLFSProject([Required][FromBody] LFSProjectDTO request)
        {
            var item = await FindAndCheckAccess(request.Id);

            if (item == null || item.Deleted)
            {
                return(NotFound());
            }

            var user = HttpContext.AuthenticatedUser() !;

            var(changes, description, _) = ModelUpdateApplyHelper.ApplyUpdateRequestToModel(item, request);

            if (!changes)
            {
                return(Ok());
            }

            item.BumpUpdatedAt();

            await database.AdminActions.AddAsync(new AdminAction()
            {
                Message = $"LFS Project {item.Id} edited",

                // TODO: there could be an extra info property where the description is stored
                PerformedById = user.Id,
            });

            await database.SaveChangesAsync();

            logger.LogInformation("LFS project {Id} edited by {Email}, changes: {Description}", item.Id,
                                  user.Email, description);
            return(Ok());
        }
示例#4
0
        public async Task <IActionResult> UpdateSymbol([Required][FromBody] DebugSymbolDTO request)
        {
            var symbol = await database.DebugSymbols.FindAsync(request.Id);

            if (symbol == null)
            {
                return(NotFound());
            }

            var user = HttpContext.AuthenticatedUser() !;

            var(changes, description, _) = ModelUpdateApplyHelper.ApplyUpdateRequestToModel(symbol, request);

            if (!changes)
            {
                return(Ok());
            }

            symbol.BumpUpdatedAt();

            await database.ActionLogEntries.AddAsync(new ActionLogEntry()
            {
                Message = $"DebugSymbol {symbol.Id} edited",

                // TODO: there could be an extra info property where the description of the edit is stored
                PerformedById = user.Id,
            });

            await database.SaveChangesAsync();

            logger.LogInformation("DebugSymbol {Id} edited by {Email}, changes: {Description}", symbol.Id,
                                  user.Email, description);

            return(Ok());
        }
        public void ModelUpdateApplyHelper_NoChangesResultsInNoChanges()
        {
            var old     = new Model();
            var initial = JsonSerializer.Serialize(old);
            var update  = new Model();

            Assert.Equal(JsonSerializer.Serialize(old), JsonSerializer.Serialize(update));

            var(changes, description, fields) = ModelUpdateApplyHelper.ApplyUpdateRequestToModel(old, update);

            Assert.False(changes);
            Assert.Null(description);
            Assert.Null(fields);
            Assert.Equal(JsonSerializer.Serialize(old), initial);
            Assert.Equal(update.SomeField, old.SomeField);
        }
        public void ModelUpdateApplyHelper_IgnoredFieldsDoesNotApply()
        {
            var old     = new Model();
            var initial = JsonSerializer.Serialize(old);
            var update  = new ModelDTO
            {
                SomeField = old.SomeField,
                Id        = 123,
            };

            var(changes, description, fields) = ModelUpdateApplyHelper.ApplyUpdateRequestToModel(old, update);

            Assert.False(changes);
            Assert.Null(description);
            Assert.Null(fields);
            Assert.Equal(JsonSerializer.Serialize(old), initial);
            Assert.NotEqual(update.Id, old.Id);
        }
        public async Task <IActionResult> UpdateSingle([Required] long id, [Required][FromBody] CrashReportDTO request)
        {
            var report = await database.CrashReports.FindAsync(id);

            if (report == null)
            {
                return(NotFound());
            }

            var user = HttpContext.AuthenticatedUserOrThrow();

            var(changes, description, fields) = ModelUpdateApplyHelper.ApplyUpdateRequestToModel(report, request);

            if (!changes || fields == null)
            {
                return(Ok());
            }

            report.BumpUpdatedAt();

            bool editedDescription = fields.Contains(nameof(CrashReport.Description));

            if (editedDescription)
            {
                report.DescriptionLastEdited     = DateTime.UtcNow;
                report.DescriptionLastEditedById = user.Id;
            }

            await database.ActionLogEntries.AddAsync(new ActionLogEntry()
            {
                Message = editedDescription ?
                          $"Crash report {report.Id} edited (edit included description)" :
                          $"Crash report {report.Id} edited",

                // TODO: there could be an extra info property where the description is stored
                PerformedById = user.Id,
            });

            await database.SaveChangesAsync();

            logger.LogInformation("Crash report {Id} edited by {Email}, changes: {Description}", report.Id,
                                  user.Email, description);
            return(Ok());
        }
        public void ModelUpdateApplyHelper_BasicChangeApplies()
        {
            var old     = new Model();
            var initial = JsonSerializer.Serialize(old);
            var update  = new ModelDTO();

            var(changes, description, fields) = ModelUpdateApplyHelper.ApplyUpdateRequestToModel(old, update);

            Assert.True(changes);
            Assert.NotNull(description);
            Assert.NotEmpty(description);
            Assert.False(description.StartsWith(","));
            Assert.NotNull(fields);
            Assert.Contains(nameof(Model.SomeField), fields);
            Assert.DoesNotContain(nameof(Model.Id), fields);
            Assert.DoesNotContain(nameof(Model.Flag), fields);

            Assert.NotEqual(JsonSerializer.Serialize(old), initial);

            Assert.Contains(nameof(Model.SomeField), description);
            Assert.DoesNotContain(nameof(Model.Id), description);
            Assert.DoesNotContain(nameof(Model.Flag), description);
            Assert.Equal(update.SomeField, old.SomeField);
        }