static MachineDtoMapper() { Mapper.CreateMap <Machine, MachineDto>() .ForMember(d => d.Version, x => x.MapFrom(y => RowVersionHelper.ConvertToString(y.RowVersion))); Mapper.CreateMap <Credentials, CredentialsDto>(); }
public void Can_convert_between_string_and_byte_array() { var bytes = new byte[] { 255, 24 }; var str = RowVersionHelper.ConvertToString(bytes); var resultBytes = RowVersionHelper.ConvertToBytes(str); var resultStr = RowVersionHelper.ConvertToString(resultBytes); Assert.AreEqual(bytes, resultBytes); Assert.AreEqual(str, resultStr); }
// PUT /api/machines/5 public HttpResponseMessage Put(int id, [FromBody] MachineDto machineDto) { //zero is passed back if the top 'null' option is selected inthe web form drop-down // if (machineDto.CredentialsId == 0) machineDto.CredentialsId = null; //validate if (!ModelState.IsValid) { _logger.Error("MachinesController.Put failed: model state not valid"); throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, Content = new StringContent("Model state not valid") }); } var machine = _unitOfWork.Machines.GetById(id); //check a machine exists if (machine == null) { throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.NotFound, Content = new StringContent("Machine not found") }); } //all's well - so update the machine //but first, validate that the data hasn't been changed by another user since we loaded it var machineRV = RowVersionHelper.ConvertToString(machine.RowVersion); if (machineRV != machineDto.Version) { _logger.Error("Concurrency exception - mismatched rowversions : " + machineRV + " | " + machineDto.Version); throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, Content = new StringContent("Concurrency exception") }); } machine.Update(machineDto); _unitOfWork.Commit(); var response = Request.CreateResponse(HttpStatusCode.NoContent); //pass the updated rowversion info back in an eTag var version = RowVersionHelper.ConvertToString(machine.RowVersion); response.Headers.ETag = new EntityTagHeaderValue((QuotedString)version, true); return(response); }