-
Notifications
You must be signed in to change notification settings - Fork 2
03.3 Database API
Jkutkut edited this page Mar 15, 2023
·
1 revision
The database API is a collection of classes, interfaces and methods to handle the use of Firebase.
In order to simplify the logic, the API's methods follow this structure:
void method(
...arguments,
OnSuccessCallback onSuccessCallback,
OnFailureCallback<T> onFailureCallback
}
void method(
...arguments,
OnSuccessValueCallback<T> onSuccessValueCallback,
OnFailureCallback<T> onFailureCallback
}With the use of generics and 3 different interfaces, all the logic from Firebase can be used following the same logic.
The following chapters detail examples of how this API can be used.
This interface allows to handle all the logic related with the authentication of the users. To initialize it, just:
CinehubAuth auth = CinehubAPI.getAuthInstance();auth.login(
"foo@bar.com",
"foobar123",
() -> {
System.out.println("Logged in successfully");
},
(error) -> {
System.out.println("Error: " + error);
}
);auth.signup(
"test user",
"test@gmail.com",
"test123",
() -> {
System.out.println("User created");
},
(error) -> {
System.out.println("Error: " + error);
}
);auth.autoLogin(
email -> {
System.out.println("Already logged with email: " + email);
},
error -> {}
);db.whoami(
user -> {
System.out.println("Me:");
System.out.println(String.format(
"Name: %s, Email: %s",
user.getName(),
user.getEmail()
));
},
(error) -> {
System.out.println("Error at me: " + error);
}
);This interface allows to handle all the logic related with the database. To initialize it, just:
CinehubDB db = CinehubAPI.getDBInstance();db.getMovies(
movies -> {
System.out.println("Movies:");
for (Movie movie : movies) {
// print movie
}
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getMovie(
"movieId",
movie -> {
// print movie
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getMoviesWithBanner(
movies -> {
System.out.println("Movies with banner:");
for (Movie movie : movies) {
// print movie
}
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getProjections(
projections -> {
System.out.println("Projections:");
for (Projection projection : projections) {
// print projection
}
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getProjection(
"projectionId",
projection -> {
// print projection
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getProjectionConfiguration(
"projectionId",
config -> {
for (char[] row : config.getSeats()) {
for (char seat : row) {
System.out.print(seat);
}
System.out.println();
}
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getReservations(
reservations -> {
System.out.println("Reservations:");
for (Reservation reservation : reservations) {
// print reservation
}
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getReservation(
"reservationId",
reservation -> {
// print reservation
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getReservationsIdsUser(
0,
reservations -> {
System.out.println("Reservations:");
for (Integer reservation : reservations) {
// print reservation
}
},
(error) -> {
System.out.println("Error: " + error);
}
);User usr = new User("marta@gmail.com", "Marta");
Projection projection = new Projection(0, 0, "2023-03-30T18:42:00.000Z");
ArrayList<Seat> seats = new ArrayList<>();
seats.add(new Seat(2, 0));
seats.add(new Seat(2, 1));
seats.add(new Seat(2, 2));
db.addReservation(
usr,
projection,
seats,
() -> running.set(VALID),
getFailureCallback()
);db.getRooms(
rooms -> {
System.out.println("Rooms:");
for (Room room : rooms) {
// print room
}
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getRoom(
"roomId",
room -> {
// print room
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getRoomConfiguration(
"roomId",
config -> {
for (char[] row : config.getSeats()) {
for (char seat : row) {
System.out.print(seat);
}
System.out.println();
}
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getSeatReservations(
seatReservations -> {
System.out.println("SeatReservations:");
for (SeatReservation seatReservation : seatReservations) {
// print seatReservation
}
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getSeatReservation(
"seatReservationId",
seatReservation -> {
// print seatReservation
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getUser(
"marta@gmail.com",
user -> db.getSeatReservationUser(
user,
reservations -> {
print("Reservations of user: " + reservations.size());
if (reservations.size() == 3)
running.set(VALID);
else
running.set(INVALID);
},
getFailureCallback()
),
getFailureCallback()
);db.getSeatReservationReservation(
0,
seatReservations -> {
System.out.println("SeatReservations:");
for (SeatReservation seatReservation : seatReservations) {
// print seatReservation
}
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getSpecialSeats(
specialSeats -> {
System.out.println("SpecialSeats:");
for (SpecialSeat specialSeat : specialSeats) {
// print specialSeat
}
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getSpecialSeat(
"specialSeatId",
specialSeat -> {
// print specialSeat
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getUsers(
users -> {
System.out.println("Users:");
for (User user : users) {
System.out.println(String.format(
"Name: %s, Email: %s",
user.getName(),
user.getEmail()
));
}
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getUser(
"marta@gmail.com",
user -> {
System.out.println("User:");
System.out.println(String.format(
"Name: %s, Email: %s",
user.getName(),
user.getEmail()
));
},
(error) -> {
System.out.println("Error: " + error);
}
);db.getUserById(
0,
user -> {
System.out.println("User:");
System.out.println(String.format(
"Name: %s, Email: %s",
user.getName(),
user.getEmail()
));
},
(error) -> {
System.out.println("Error: " + error);
}
);This interface allows to handle all the logic related with the file storage. To initialize it, just:
CinehubStorage auth = CinehubAPI.getStorageInstance();storage.getBanner(
movie,
url -> {
print("Banner url: " + url);
// Glide this url
},
error -> {
print("Error: " + error);
}
);