///<sumary>Creación de un nuevo vehiculo</sumary> ///<param name="vehicle">Instancia de Vehicle</param> ///<returns>Id del vehiculo</returns> ///<exception cref="ModelNotFoundException"> Si no existe el modelo </exception> ///<exception cref="LocationNotFoundException"> Si no existe la ciudad </exception> ///<exception cref="UniqueAttributeException"> /// Es excepción es lanzada cuando ya existe un carro con dicha matricula ///</exception> public int AddVehicle(Vehicle vehicle) { int id = 0; try { PostgresModelDAO modelDAO = new PostgresModelDAO(); modelDAO.GetModelById(vehicle.VehicleModelId); //Throw ModelNotFoundException PostgresLocationDAO locationDAO = new PostgresLocationDAO(); locationDAO.GetLocationById(vehicle.VehicleLocationId); // Throw LocationNotFoundException PgConnection pgConnection = PgConnection.Instance; DataTable dataTable = pgConnection.ExecuteFunction( "LicenseUniqueness(@modelName)", vehicle.License); if (Convert.ToBoolean(dataTable.Rows[0][0])) { throw new UniqueAttributeException("La matricula " + vehicle.License + " ya existe"); } else { dataTable = pgConnection.ExecuteFunction( "AddVehicle(@modelId, @locationId, @license, @price, @status)", vehicle.VehicleModelId, vehicle.VehicleLocationId, vehicle.License, vehicle.Price, vehicle.Status); id = Convert.ToInt32(dataTable.Rows[0][0]); } } catch (DatabaseException ex) { throw new InternalServerErrorException("Error en el servidor : Conexion a base de datos", ex); } catch (InvalidStoredProcedureSignatureException ex) { throw new InternalServerErrorException("Error en el servidor", ex); } return(id); }
///<sumary>Modificar un vehiculo</sumary> ///<param name="vehicle">Instancia de Vehicle</param> ///<returns>True si actualizo correctamente</returns> ///<exception cref="VehicleNotFoundException"> Si el vehiculo no existe </exception> ///<exception cref="ModelNotFoundException"> Si el modelo no existe </exception> ///<exception cref="LocationNotFoundException"> Si la ciudad no existe </exception> public bool UpdateVehicle(Vehicle vehicle) { bool updated = false; try{ GetVehicleById(vehicle.Id); // Throw VehicleNotFoundException PostgresModelDAO modelDAO = new PostgresModelDAO(); modelDAO.GetModelById(vehicle.VehicleModelId); //Throw ModelNotFoundException PostgresLocationDAO locationDAO = new PostgresLocationDAO(); locationDAO.GetLocationById(vehicle.VehicleLocationId); // Throw LocationNotFoundException PgConnection pgConnection = PgConnection.Instance; DataTable dataTable = pgConnection.ExecuteFunction( "UpdateVehicle(@vehid, @vehmodel, @vehlocation, @vehlicense, @vehprice, @vehstatus)", vehicle.Id, vehicle.VehicleModelId, vehicle.VehicleLocationId, vehicle.License, vehicle.Price, vehicle.Status); if (Convert.ToBoolean(dataTable.Rows[0][0])) { updated = true; } }catch (DatabaseException ex) { throw new InternalServerErrorException("Error en el servidor : Conexion a base de datos", ex); }catch (InvalidStoredProcedureSignatureException ex) { throw new InternalServerErrorException("Error en el servidor", ex); } return(updated); }
///<sumary>Obtener vehiculos disponibles por ciudad</sumary> ///<param name="locationId">Id de la ciudad</param> ///<returns>Lista de vehiculos disponibles</returns> ///<exception cref="LocationNotFoundException"> Si no existe la ciudad </exception> ///<exception cref="NotVehiclesAvailableException"> /// Si no hay existencia de vehiculos disponibles ///</exception> public List <Vehicle> GetAvailableVehiclesByLocation(int locationId) { List <Vehicle> vehicles = new List <Vehicle>(); try{ PostgresLocationDAO locationDAO = new PostgresLocationDAO(); Location location = locationDAO.GetLocationById(locationId);// Throw LocationNotFoundException PgConnection pgConnection = PgConnection.Instance; DataTable dataTable = pgConnection.ExecuteFunction("GetAvailableVehiclesByLocation(@locationId)", locationId); if (dataTable.Rows.Count > 0) { foreach (DataRow dataRow in dataTable.Rows) { Vehicle vehicle = new Vehicle( Convert.ToInt32(dataRow[0]), Convert.ToInt32(dataRow[1]), Convert.ToInt32(dataRow[2]), dataRow[3].ToString(), Convert.ToDouble(dataRow[4]), Convert.ToBoolean(dataRow[5]) ); vehicle.VehicleModel = new PostgresModelDAO().GetModelById(vehicle.VehicleModelId); vehicle.VehicleLocation = new PostgresLocationDAO().GetLocationById(vehicle.VehicleLocationId); vehicles.Add(vehicle); } } else { throw new NotVehiclesAvailableException(locationId, "No existen Vehiculos disponibles en " + location.City); } }catch (DatabaseException ex) { throw new InternalServerErrorException("Error en el servidor : Conexion a base de datos", ex); }catch (InvalidStoredProcedureSignatureException ex) { throw new InternalServerErrorException("Error en el servidor", ex); } return(vehicles); }