Skip to content

Commit

Permalink
Base de datos con Room implementada
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarrzz committed Oct 16, 2024
1 parent 69ba79e commit fe15035
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
6 changes: 6 additions & 0 deletions AndroidProject/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,10 @@ dependencies {

// Roboelectric (to get a proper Context object in unit tests)
testImplementation libs.roboelectric

def room_version = "2.6.1"

implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import es.unican.gasolineras.model.Gasolinera;
import es.unican.gasolineras.repository.IGasolinerasRepository;
import es.unican.gasolineras.repository.PuntosInteresDao;

/**
* The Presenter-View contract for the Main activity.
Expand Down Expand Up @@ -98,5 +99,10 @@ public interface View {
*/
public void showInfoActivity();

/**
*
* @return
*/
public PuntosInteresDao getPuntosInteresDAO();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import es.unican.gasolineras.model.IDCCAAs;
import es.unican.gasolineras.repository.ICallBack;
import es.unican.gasolineras.repository.IGasolinerasRepository;
import es.unican.gasolineras.repository.PuntosInteresDao;

/**
* The presenter of the main activity of the application. It controls {@link MainView}
Expand Down Expand Up @@ -63,7 +64,7 @@ public void onFailure(Throwable e) {
view.showLoadError();
}
};

PuntosInteresDao puntosInteresDAO = view.getPuntosInteresDAO();
repository.requestGasolineras(callBack, IDCCAAs.CANTABRIA.id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.room.Room;

import org.parceler.Parcels;

Expand All @@ -23,7 +24,9 @@
import es.unican.gasolineras.activities.info.InfoView;
import es.unican.gasolineras.activities.details.DetailsView;
import es.unican.gasolineras.model.Gasolinera;
import es.unican.gasolineras.repository.AppDatabase;
import es.unican.gasolineras.repository.IGasolinerasRepository;
import es.unican.gasolineras.repository.PuntosInteresDao;

/**
* The main view of the application. It shows a list of gas stations.
Expand Down Expand Up @@ -152,4 +155,11 @@ public void showInfoActivity() {
Intent intent = new Intent(this, InfoView.class);
startActivity(intent);
}

@Override
public PuntosInteresDao getPuntosInteresDAO() {
AppDatabase db = Room.databaseBuilder(getApplicationContext(),
AppDatabase.class, "database-name").build();
return db.puntosInteresDao();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package es.unican.gasolineras.model;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Index;
import androidx.room.PrimaryKey;

@Entity(indices = {@Index(value = {"nombre"}, unique = true)})
public class PuntoInteres {
@PrimaryKey
public int idPuntoInteres;

@ColumnInfo(name = "nombre")
public String nombre;

@ColumnInfo(name = "latitud")
public double latitud;

@ColumnInfo(name = "longitud")
public double longitud;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package es.unican.gasolineras.repository;

import androidx.room.Database;
import androidx.room.RoomDatabase;

import es.unican.gasolineras.model.PuntoInteres;

@Database(entities = {PuntoInteres.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract PuntosInteresDao puntosInteresDao();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package es.unican.gasolineras.repository;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;

import java.util.List;

import es.unican.gasolineras.model.PuntoInteres;

@Dao
public interface PuntosInteresDao {
@Query("SELECT * FROM Puntointeres")
List<PuntoInteres> getAll();

// Buscar por id
@Query("SELECT * FROM PuntoInteres WHERE idPuntoInteres IN (:PuntoInteresId)")
PuntoInteres loadById(int PuntoInteresId);

// Buscar por nombre
@Query("SELECT * FROM PuntoInteres WHERE nombre IN (:NombrePuntoInteres)")
PuntoInteres loadByName(String NombrePuntoInteres);

// Buscar por latitud y longitud exactas
@Query("SELECT * FROM PuntoInteres WHERE latitud = :lat AND longitud = :lon")
PuntoInteres loadByLatLon(double lat, double lon);

@Insert
void insertAll(PuntoInteres... puntoInteres);

@Delete
void delete(PuntoInteres puntoInteres);
}

0 comments on commit fe15035

Please sign in to comment.