示例#1
0
        public async Task <ActionResult <EquipmentViewModel> > PutEquipment(string id, EquipmentEditModel equipmentModel)
        {
            Equipment equipment = await _equipmentsRepository.FindByIdAsync(id);

            if (equipment is null)
            {
                return(BadRequest($"No existe ningún equipo con el código {id}."));
            }

            _mapper.Map(equipmentModel, equipment);
            _equipmentsRepository.Update(equipment);

            try
            {
                await _unitWork.SaveAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EquipmentExists(id))
                {
                    return(NotFound($"Actualizacón fallida. No existe ningún equipo con el código {id}."));
                }

                throw;
            }

            return(_mapper.Map <EquipmentViewModel>(equipment));
        }
示例#2
0
        public async Task Update_Existing_Equipment()
        {
            try
            {
                Equipment equipment = await _equipmentsRepository.FindByIdAsync("123RF");

                Assert.IsNotNull(equipment);

                equipment.Name = "Rociador de pesticida";
                _equipmentsRepository.Update(equipment);

                await _dbContext.SaveChangesAsync();

                equipment = await _equipmentsRepository.FindByIdAsync("123RF");

                Assert.AreEqual("Rociador de pesticida", equipment.Name);
            }
            catch (DbUpdateException e)
            {
                Assert.Fail(e.Message);
            }
        }