Skip to content

Commit

Permalink
Sprint4: Implement TravDatabase (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianrui-qi authored Nov 17, 2024
1 parent d66b3e9 commit 32c3c92
Show file tree
Hide file tree
Showing 8 changed files with 645 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainModel {

Expand All @@ -11,6 +12,7 @@ public class MainModel {
private final DestDatabase destDatabase = new DestDatabase();
private final DiniDatabase diniDatabase = new DiniDatabase();
private final AccoDatabase accoDatabase = new AccoDatabase();
private final TravDatabase travDatabase = new TravDatabase();

/* Singleton Design */

Expand Down Expand Up @@ -195,6 +197,57 @@ public void getAccommodation(
});
}

public void addTravel(
String start, String end,
String destination, String accommodation, String dining, String note,
Callback<Boolean> callback
) {
// Retrieve the current username
String username = this.userDatabase.getUsernameCurr();
if (username == null || username.isEmpty()) {
callback.onResult(false); // Fail if the current username is not set
return;
}

Map<String, String> travelData = new HashMap<>();
travelData.put("username", username);
travelData.put("start", start);
travelData.put("end", end);
travelData.put("destination", destination);
travelData.put("accommodation", accommodation);
travelData.put("dining", dining);
travelData.put("note", note);

// Add the travel post to the TravelDatabase
this.travDatabase.addTravel(
travelData, key -> {
if (key != null) {
// Add the travel key to the current user's travel list
this.userDatabase.addTravel(key, success -> {
if (success) {
notifyObservers("TravelAdded", key);
}
callback.onResult(success);
});
} else {
callback.onResult(false); // Return failure if the key is null
}
}
);
}

public void getTravel(
Callback<HashMap<String, HashMap<String, String>>> callback
) {
// Fetch all travel posts from the TravelDatabase
this.travDatabase.getTravel(data -> {
if (data != null) {
notifyObservers("TravelFetched", data);
}
callback.onResult(data); // Return the fetched data
});
}

/* Callbacks */

public interface Callback<T> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.example.sprint1.model;

import androidx.annotation.NonNull;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.HashMap;
import java.util.Map;

public class TravDatabase {

/* Instance fields */

private final DatabaseReference travelDatabase;

/* Constructors */

public TravDatabase() {
this.travelDatabase = FirebaseDatabase.getInstance().getReference("travel");
}

/* Main Features */

public void addTravel(
Map<String, String> travelData,
Callback<String> callback
) {
// Generate a unique key for the travel post
String key = this.travelDatabase.push().getKey();

if (key == null) {
callback.onResult(null);
return;
}

// Save the travel post to Firebase
this.travelDatabase.child(key).setValue(travelData).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
callback.onResult(key); // Return the travel post ID if successful
} else {
callback.onResult(null); // Return null if the operation fails
}
});
}

public void getTravel(
Callback<HashMap<String, HashMap<String, String>>> callback
) {
this.travelDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
HashMap<String, HashMap<String, String>> travelPosts = new HashMap<>();

// Iterate through each travel post
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
HashMap<String, String> postDetails = new HashMap<>();
for (DataSnapshot detailSnapshot : postSnapshot.getChildren()) {
String key = detailSnapshot.getKey();
String value = detailSnapshot.getValue(String.class);
if (key != null && value != null) {
postDetails.put(key, value);
}
}
// Add the travel post details to the result
travelPosts.put(postSnapshot.getKey(), postDetails);
}

callback.onResult(travelPosts);
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
callback.onResult(null); // Return null if the operation fails
}
});
}

/* Callbacks */

public interface Callback<T> {
void onResult(T callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,57 @@ public void onCancelled(@NonNull DatabaseError error) {
});
}

public void addTravel(
String key,
Callback<Boolean> callback
) {
// Reference to the current user's travel list in the database
DatabaseReference ref = this.userDatabase.child(this.usernameCurr).child("travel");

// Retrieve the existing travel list
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ArrayList<String> travelKeys = new ArrayList<>();

// If travel data exists, populate the list
if (dataSnapshot.exists()) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
String travelKey = child.getValue(String.class);
if (travelKey != null) {
travelKeys.add(travelKey);
}
}
}

// Add the new key if it's not already in the list
if (!travelKeys.contains(key)) {
travelKeys.add(key);
}

// Update the travel list in Firebase
ref.setValue(travelKeys).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
callback.onResult(true); // Success
} else {
callback.onResult(false); // Failure
}
});
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
callback.onResult(false);
}
});
}

/* Getter */

public String getUsernameCurr() {
return usernameCurr;
}

/* Callbacks */

public interface Callback<T> {
Expand Down
Loading

0 comments on commit 32c3c92

Please sign in to comment.