public static async Task <InfractionDTO> AddNew(InfractionDTO infraction) { using (var client = GetHttpClient()) { var serializeObject = JsonConvert.SerializeObject(infraction); var content = new StringContent(serializeObject, Encoding.UTF8, "application/json"); using (var response = await client.PostAsync(URL_KEY + "/save", content)) { if (response.IsSuccessStatusCode) { if (response.Content != null) { var stringResult = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject <InfractionDTO>(stringResult); return(result); } else { return(null); } } else { throw new Exception(await CastResultError(response)); } } } }
public IHttpActionResult Save(InfractionDTO dto) { try { var result = this._dgtAppService.AddNewInfraction(dto); return(Ok(result)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
private async void cmdAccept_Click(object sender, EventArgs e) { if (this.ValidateData()) { try { this._infraction = await ApiManagerInfractions.AddNew(this._infraction); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show("Ha ocurrido el siguiente error:" + Environment.NewLine + Environment.NewLine + ex.GetBaseException().Message, "DGT", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
public InfractionDTO AddNewInfraction() { ClearErrors(); this._infraction = new InfractionDTO(); this.infractionDTOBindingSource.DataSource = this._infraction; this.infractionDTOBindingSource.MoveFirst(); var dialogResult = this.ShowDialog(); if (dialogResult == DialogResult.OK) { return(this._infraction); } else { return(null); } }
private Infraction MaterializeInfractionFromDto(InfractionDTO dto) { var infraction = InfractionFactory.CreateInfraction(dto.VehicleId, dto.InfractionTypeId, dto.DriverId, dto.Date); return(infraction); }
/// <summary> /// <see cref="IDgtAppService"/> /// </summary> /// <returns><see cref="IDgtAppService"/></returns> public InfractionDTO AddNewInfraction(InfractionDTO infractionDTO) { try { if (infractionDTO == null) { throw new ArgumentNullException("infractionDTO"); } // Get associated driver var driver = _driverRepository.Get(infractionDTO.DriverId); if (driver == null) { throw new InvalidOperationException(String.Format(CommonMessages.exception_EntityWithIdNotExists, Names.Driver, infractionDTO.DriverId)); } //Get associated vehicle var vehicle = _vehicleRepository.Get(infractionDTO.VehicleId); if (vehicle == null) { throw new InvalidOperationException(String.Format(CommonMessages.exception_EntityWithIdNotExists, Names.Vehicle, infractionDTO.VehicleId)); } // Check vehicle belong to driver var vehicleDriver = _vehicleDriverRepository.GetFiltered(vd => vd.DriverId == driver.Id && vd.VehicleId == vehicle.Id); if (vehicleDriver == null || !vehicleDriver.Any()) { throw new InvalidOperationException(String.Format( CommonMessages.exception_VehicleDoesNowBelongToDriver, vehicle.License, driver.Identifier)); } // Get associated infraction type var infractionType = _infractionTypeRepository.Get(infractionDTO.InfractionTypeId); if (infractionType == null) { throw new InvalidOperationException(String.Format(CommonMessages.exception_EntityWithIdNotExists, Names.InfractionType, infractionDTO.InfractionTypeId)); } // Materialize infraction from dto var infraction = MaterializeInfractionFromDto(infractionDTO); infraction.Validate(); infraction.GenerateNewIdentity(); infraction.CreatedDate = DateTime.Now; _infractionRepository.Add(infraction); // Remove points to driver driver.RemovePoints(infractionType.Points); _infractionRepository.UnitOfWork.Commit(); _driverRepository.UnitOfWork.Commit(); return(infraction.ProjectedAs <InfractionDTO>()); } catch (ApplicationValidationErrorsException valEx) { string erMsg = ""; foreach (var item in valEx.ValidationErrors) { erMsg += Environment.NewLine + item; } throw new Exception(erMsg); } catch (Exception ex) { throw ex; } }