public async Task UpdatePlantLogType()
        {
            await using var context = DatabaseHelper.CreateInMemoryDatabaseContext(nameof(UpdatePlantLogType));

            var id = Guid.NewGuid();
            await context.PlantLogTypes.AddAsync(new PlantLogType { Id = id, Name = "Something old" });

            await context.SaveChangesAsync();

            var name      = "LogType1";
            var cntBefore = await context.PlantLogTypes.CountAsync();

            var updateCmd = new UpdatePlantLogTypeCommand {
                Id = id, Name = name
            };
            var cmdHandler = new UpdatePlantLogTypeCommandHandler(context);
            var result     = await cmdHandler.Handle(updateCmd, CancellationToken.None);

            Assert.True(result.Success);
            Assert.Equal(cntBefore, await context.PlantLogTypes.CountAsync());
            var obj = await context.PlantLogTypes.FindAsync(id);

            Assert.Equal(id, obj.Id);
            Assert.Equal(name, obj.Name);

            var updateInvalidPlantLogType = new UpdatePlantLogTypeCommand {
                Id = Guid.NewGuid(), Name = "hehe"
            };
            var resultInvalidPlantLogType = await cmdHandler.Handle(updateInvalidPlantLogType, CancellationToken.None);

            Assert.False(resultInvalidPlantLogType.Success);
        }
Пример #2
0
        public async Task <ActionResult <PlantLogType> > Update(Guid plantLogTypeId, UpdatePlantLogTypeRequest request)
        {
            var command = new UpdatePlantLogTypeCommand {
                Id = plantLogTypeId, Name = request.Name
            };
            var response = await mediator.Send(command);

            if (!response.Success)
            {
                return(BadRequest(response.ErrorMessages));
            }

            var updatedQuery = new GetPlantLogTypeByIdQuery {
                Id = command.Id
            };
            var updatedResult = await mediator.Send(updatedQuery);

            var updatedObj = PlantLogType.FromCore(updatedResult.Data);

            return(Ok(updatedObj));
        }