Exemplo n.º 1
0
        public async Task <Response> Handle(CreatePlantLogForPlantCommand request, CancellationToken cancellationToken)
        {
            var plant = await context.Plants.FindAsync(request.PlantId);

            if (null == plant)
            {
                return(new Response
                {
                    Success = false,
                    ErrorMessages =
                    {
                        { nameof(request.PlantId), new[] { "Plant not found" } }
                    },
                });
            }

            var log = new PlantLog
            {
                Id             = request.Id,
                Plant          = plant,
                PlantLogTypeId = request.PlantLogTypeId,
                DateTime       = request.DateTime,
                Log            = request.Log,
            };
            await context.PlantLogs.AddAsync(log);

            await context.SaveChangesAsync(cancellationToken);

            return(new Response
            {
                Success = true
            });
        }
        public async Task <Response> Handle(CreatePlantSpeciesCommand request, CancellationToken cancellationToken)
        {
            dbContext.PlantSpecies.Add(new PlantSpecies
            {
                Id   = request.Id,
                Name = request.Name
            });
            await dbContext.SaveChangesAsync(cancellationToken);

            return(new Response
            {
                Success = true
            });
        }
        public async Task <Response> Handle(UpdatePlantSpeciesCommand request, CancellationToken cancellationToken)
        {
            var plantSpecies = await dbContext.PlantSpecies.FindAsync(request.Id);

            if (null == plantSpecies)
            {
                var retVal = new Response();
                retVal.AddErrorMessage(nameof(request.Id), "No entity found");
                return(retVal);
            }
            plantSpecies.Name = request.Name;
            await dbContext.SaveChangesAsync(cancellationToken);

            return(new Response {
                Success = true
            });
        }
        public async Task <Response> Handle(DeletePlantLogTypeCommand request, CancellationToken cancellationToken)
        {
            var plantLogType = await dbContext.PlantLogTypes.FindAsync(request.Id);

            if (null == plantLogType)
            {
                var response = new Response();
                response.AddErrorMessage(nameof(request.Id), "No entity found");
                return(response);
            }

            dbContext.PlantLogTypes.Remove(plantLogType);
            await dbContext.SaveChangesAsync(cancellationToken);

            return(new Response
            {
                Success = true
            });
        }