Exemplo n.º 1
0
        public GetTruckResult Update(string id, UpdateTruckInput input)
        {
            if (string.IsNullOrEmpty(id))
            {
                return(null);
            }

            if (input is null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            this.UpdateValidator.ValidateAndThrow(input);

            var data = this.GetTruck(id);

            if (data is null)
            {
                return(null);
            }

            var updatedData = this.ModelMapper.Map <Truck>(input);

            updatedData.Id = data.Id;

            var updated = this.Repository.UpdateTruck(updatedData);

            this.Repository.Save();

            updated.Reload();
            return(this.ModelMapper.Map <GetTruckResult>(updated.Entity));
        }
Exemplo n.º 2
0
 public ActionResult <GetTruckResult> UpdateTruck([FromRoute] string id, [FromBody] UpdateTruckInput input)
 {
     try
     {
         var result = this.TruckService.Update(id, input);
         return(!(result is null) ? this.Ok(result) : this.NotFound() as ActionResult);
     }
     catch (Exception ex)
     {
         this.Logger.LogError(ex, $"Error updating Truck: {id}");
         return(this.BadRequest());
     }
 }
Exemplo n.º 3
0
        public void Update_ExistingModel_ShouldReturn()
        {
            var model = new Truck
            {
                Id              = Guid.NewGuid(),
                TruckType       = TruckType.FH,
                ModelYear       = DateTime.Now.Year,
                FabricationYear = DateTime.Now.Year,
                Name            = "Truck",
                Plate           = "AAA0000"
            };

            var input = new UpdateTruckInput
            {
                TruckType       = TruckType.FM,
                ModelYear       = DateTime.Now.Year,
                FabricationYear = DateTime.Now.Year,
                Name            = "Truck0",
                Plate           = "AAA0001"
            };

            using var context = this.BuildContext();
            var entity = context.Add(model);

            context.SaveChanges();
            entity.State = EntityState.Detached;

            var subject  = this.BuildService(context);
            var response = subject.Update(model.Id.ToString(), input);

            Assert.NotNull(response);
            Assert.Equal(model.Id.ToString(), response.Id);

            Assert.Equal(input.TruckType, response.TruckType);
            Assert.Equal(input.ModelYear, response.ModelYear);
            Assert.Equal(input.FabricationYear, response.FabricationYear);
            Assert.Equal(input.Name, response.Name);
            Assert.Equal(input.Plate, response.Plate);
        }