Added CRUD to Pet, Service, Supplier
- Made Pet, Service, supplier CRUD by calling the backend apiendpoints
This commit is contained in:
@@ -12,7 +12,8 @@ import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class RetrofitClient {
|
||||
//base URL
|
||||
public static final String BASE_URL = "http://10.0.2.2:8080/api/"; //for emulator testing change to computer ip if using hardware to test
|
||||
// public static final String BASE_URL = "http://10.0.2.2:8080/api/"; //for emulator testing change to computer ip if using hardware to test
|
||||
public static final String BASE_URL = "http://10.0.0.200:8080/api/"; //for emulator testing change to computer ip if using hardware to test
|
||||
|
||||
private static Retrofit retrofit = null;
|
||||
|
||||
|
||||
@@ -12,12 +12,29 @@ public class PetDTO {
|
||||
private String updatedAt;
|
||||
|
||||
public Long getPetId() { return petId; }
|
||||
public void setPetId(Long petId) { this.petId = petId; }
|
||||
|
||||
public String getPetName() { return petName; }
|
||||
public void setPetName(String petName) { this.petName = petName; }
|
||||
|
||||
public String getPetSpecies() { return petSpecies; }
|
||||
public void setPetSpecies(String petSpecies) { this.petSpecies = petSpecies; }
|
||||
|
||||
public String getPetBreed() { return petBreed; }
|
||||
public void setPetBreed(String petBreed) { this.petBreed = petBreed; }
|
||||
|
||||
public Integer getPetAge() { return petAge; }
|
||||
public void setPetAge(Integer petAge) { this.petAge = petAge; }
|
||||
|
||||
public String getPetStatus() { return petStatus; }
|
||||
public void setPetStatus(String petStatus) { this.petStatus = petStatus; }
|
||||
|
||||
public String getPetPrice() { return petPrice; }
|
||||
public void setPetPrice(String petPrice) { this.petPrice = petPrice; }
|
||||
|
||||
public String getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(String createdAt) { this.createdAt = createdAt; }
|
||||
|
||||
public String getUpdatedAt() { return updatedAt; }
|
||||
public void setUpdatedAt(String updatedAt) { this.updatedAt = updatedAt; }
|
||||
}
|
||||
|
||||
@@ -10,10 +10,23 @@ public class ServiceDTO {
|
||||
private String updatedAt;
|
||||
|
||||
public Long getServiceId() { return serviceId; }
|
||||
public void setServiceId(Long serviceId) { this.serviceId = serviceId; }
|
||||
|
||||
public String getServiceName() { return serviceName; }
|
||||
public void setServiceName(String serviceName) { this.serviceName = serviceName; }
|
||||
|
||||
public String getServiceDesc() { return serviceDesc; }
|
||||
public void setServiceDesc(String serviceDesc) { this.serviceDesc = serviceDesc; }
|
||||
|
||||
public Integer getServiceDuration() { return serviceDuration; }
|
||||
public void setServiceDuration(Integer serviceDuration) { this.serviceDuration = serviceDuration; }
|
||||
|
||||
public Double getServicePrice() { return servicePrice; }
|
||||
public void setServicePrice(Double servicePrice) { this.servicePrice = servicePrice; }
|
||||
|
||||
public String getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(String createdAt) { this.createdAt = createdAt; }
|
||||
|
||||
public String getUpdatedAt() { return updatedAt; }
|
||||
public void setUpdatedAt(String updatedAt) { this.updatedAt = updatedAt; }
|
||||
}
|
||||
|
||||
@@ -11,11 +11,26 @@ public class SupplierDTO {
|
||||
private String updatedAt;
|
||||
|
||||
public Long getSupId() { return supId; }
|
||||
public void setSupId(Long supId) { this.supId = supId; }
|
||||
|
||||
public String getSupCompany() { return supCompany; }
|
||||
public void setSupCompany(String supCompany) { this.supCompany = supCompany; }
|
||||
|
||||
public String getSupContactFirstName() { return supContactFirstName; }
|
||||
public void setSupContactFirstName(String supContactFirstName) { this.supContactFirstName = supContactFirstName; }
|
||||
|
||||
public String getSupContactLastName() { return supContactLastName; }
|
||||
public void setSupContactLastName(String supContactLastName) { this.supContactLastName = supContactLastName; }
|
||||
|
||||
public String getSupEmail() { return supEmail; }
|
||||
public void setSupEmail(String supEmail) { this.supEmail = supEmail; }
|
||||
|
||||
public String getSupPhone() { return supPhone; }
|
||||
public void setSupPhone(String supPhone) { this.supPhone = supPhone; }
|
||||
|
||||
public String getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(String createdAt) { this.createdAt = createdAt; }
|
||||
|
||||
public String getUpdatedAt() { return updatedAt; }
|
||||
public void setUpdatedAt(String updatedAt) { this.updatedAt = updatedAt; }
|
||||
}
|
||||
|
||||
@@ -4,9 +4,11 @@ import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@@ -18,9 +20,16 @@ import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.petstoremobile.R;
|
||||
import com.example.petstoremobile.api.PetApi;
|
||||
import com.example.petstoremobile.api.RetrofitClient;
|
||||
import com.example.petstoremobile.dtos.PetDTO;
|
||||
import com.example.petstoremobile.fragments.ListFragment;
|
||||
import com.example.petstoremobile.fragments.listfragments.PetFragment;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
public class PetDetailFragment extends Fragment {
|
||||
|
||||
private TextView tvMode, tvPetId;
|
||||
@@ -47,34 +56,124 @@ public class PetDetailFragment extends Fragment {
|
||||
handleArguments();
|
||||
|
||||
//set button click listeners
|
||||
btnBack.setOnClickListener(v -> {
|
||||
//get the list fragment and pop the back stack to return to the previous view (PetFragment)
|
||||
ListFragment listFragment = (ListFragment) getParentFragment();
|
||||
if (listFragment != null) {
|
||||
listFragment.getChildFragmentManager().popBackStack();
|
||||
}
|
||||
});
|
||||
//set save and delete button to run the appropriate method
|
||||
btnBack.setOnClickListener(v -> navigateBack());
|
||||
btnSavePet.setOnClickListener(v -> savePet());
|
||||
btnDeletePet.setOnClickListener(v -> deletePet());
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
//TODO: Method to Update or Add a pet
|
||||
//Method to Update or Add a pet
|
||||
private void savePet() {
|
||||
//get all the values from the fields
|
||||
String name = etPetName.getText().toString().trim();
|
||||
String species = etPetSpecies.getText().toString().trim();
|
||||
String breed = etPetBreed.getText().toString().trim();
|
||||
String ageStr = etPetAge.getText().toString().trim();
|
||||
String priceStr = etPetPrice.getText().toString().trim();
|
||||
String status = spinnerPetStatus.getSelectedItem().toString();
|
||||
|
||||
//check if all the fields are filled
|
||||
if (name.isEmpty() || species.isEmpty() || breed.isEmpty() || ageStr.isEmpty() || priceStr.isEmpty()) {
|
||||
Toast.makeText(getContext(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
//create a pet object to send to the API
|
||||
PetDTO petDTO = new PetDTO();
|
||||
petDTO.setPetName(name);
|
||||
petDTO.setPetSpecies(species);
|
||||
petDTO.setPetBreed(breed);
|
||||
petDTO.setPetAge(Integer.parseInt(ageStr));
|
||||
petDTO.setPetPrice(priceStr);
|
||||
petDTO.setPetStatus(status);
|
||||
|
||||
PetApi petApi = RetrofitClient.getPetApi(requireContext());
|
||||
|
||||
//check if the pet is being edited or added
|
||||
if (isEditing) {
|
||||
// TODO: Update pet
|
||||
Toast.makeText(getContext(), "Save functionality not yet implemented with DB", Toast.LENGTH_SHORT).show();
|
||||
// Update existing pet
|
||||
petDTO.setPetId((long) petId);
|
||||
petApi.updatePet((long) petId, petDTO).enqueue(new Callback<PetDTO>() {
|
||||
@Override
|
||||
public void onResponse(Call<PetDTO> call, Response<PetDTO> response) {
|
||||
if (response.isSuccessful()) {
|
||||
Toast.makeText(getContext(), "Pet updated successfully!", Toast.LENGTH_SHORT).show();
|
||||
navigateBack();
|
||||
} else {
|
||||
Toast.makeText(getContext(), "Failed to update pet: " + response.code(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<PetDTO> call, Throwable t) {
|
||||
Log.e("PetDetailFragment", "Error updating pet", t);
|
||||
Toast.makeText(getContext(), "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// TODO: Add new pet
|
||||
Toast.makeText(getContext(), "Save functionality not yet implemented with DB", Toast.LENGTH_SHORT).show();
|
||||
// Add new pet
|
||||
petApi.createPet(petDTO).enqueue(new Callback<PetDTO>() {
|
||||
@Override
|
||||
public void onResponse(Call<PetDTO> call, Response<PetDTO> response) {
|
||||
if (response.isSuccessful()) {
|
||||
Toast.makeText(getContext(), "Pet added successfully!", Toast.LENGTH_SHORT).show();
|
||||
navigateBack();
|
||||
} else {
|
||||
Toast.makeText(getContext(), "Failed to add pet: " + response.code(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<PetDTO> call, Throwable t) {
|
||||
Log.e("PetDetailFragment", "Error adding pet", t);
|
||||
Toast.makeText(getContext(), "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Method to Delete a pet
|
||||
//Method to Delete a pet
|
||||
private void deletePet() {
|
||||
Toast.makeText(getContext(), "Delete functionality not yet implemented with DB", Toast.LENGTH_SHORT).show();
|
||||
//Alert the user to confirm the delete
|
||||
new AlertDialog.Builder(requireContext())
|
||||
.setTitle("Delete Pet")
|
||||
.setMessage("Are you sure you want to delete " + etPetName.getText().toString() + "?")
|
||||
.setPositiveButton("Delete", (dialog, which) -> {
|
||||
PetApi petApi = RetrofitClient.getPetApi(requireContext());
|
||||
//if they say yes then delete the pet
|
||||
petApi.deletePet((long) petId).enqueue(new Callback<Void>() {
|
||||
@Override
|
||||
public void onResponse(Call<Void> call, Response<Void> response) {
|
||||
if (response.isSuccessful()) {
|
||||
Toast.makeText(getContext(), "Pet deleted successfully!", Toast.LENGTH_SHORT).show();
|
||||
navigateBack();
|
||||
} else {
|
||||
Toast.makeText(getContext(), "Failed to delete pet: " + response.code(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Void> call, Throwable t) {
|
||||
Log.e("PetDetailFragment", "Error deleting pet", t);
|
||||
Toast.makeText(getContext(), "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
})
|
||||
.setNegativeButton("Cancel", null)
|
||||
.show();
|
||||
}
|
||||
|
||||
//Helper method to navigate back to the list
|
||||
private void navigateBack() {
|
||||
ListFragment listFragment = (ListFragment) getParentFragment();
|
||||
if (listFragment != null) {
|
||||
// If editing pop back twice to get back to PetDetail Fragment instead of PetProfileFragment
|
||||
if (isEditing) {
|
||||
listFragment.getChildFragmentManager().popBackStack();
|
||||
}
|
||||
listFragment.getChildFragmentManager().popBackStack();
|
||||
}
|
||||
}
|
||||
|
||||
//helper function to check if pet is being edited or added and show the view accordingly
|
||||
|
||||
@@ -2,8 +2,10 @@ package com.example.petstoremobile.fragments.listfragments.detailfragments;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@@ -13,9 +15,16 @@ import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.petstoremobile.R;
|
||||
import com.example.petstoremobile.api.RetrofitClient;
|
||||
import com.example.petstoremobile.api.ServiceApi;
|
||||
import com.example.petstoremobile.dtos.ServiceDTO;
|
||||
import com.example.petstoremobile.fragments.ListFragment;
|
||||
import com.example.petstoremobile.fragments.listfragments.ServiceFragment;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
public class ServiceDetailFragment extends Fragment {
|
||||
|
||||
private TextView tvMode, tvServiceId;
|
||||
@@ -40,33 +49,115 @@ public class ServiceDetailFragment extends Fragment {
|
||||
handleArguments();
|
||||
|
||||
//set button click listeners
|
||||
btnBack.setOnClickListener(v -> {
|
||||
//get the list fragment and pop the back stack to return to the previous view (ServiceFragment)
|
||||
ListFragment listFragment = (ListFragment) getParentFragment();
|
||||
if (listFragment != null) {
|
||||
listFragment.getChildFragmentManager().popBackStack();
|
||||
}
|
||||
});
|
||||
btnBack.setOnClickListener(v -> navigateBack());
|
||||
btnSaveService.setOnClickListener(v -> saveService());
|
||||
btnDeleteService.setOnClickListener(v -> deleteService());
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
//TODO: Method to Update or Add a service
|
||||
//Method to Update or Add a service
|
||||
private void saveService() {
|
||||
//get all the values from the fields
|
||||
String name = etServiceName.getText().toString().trim();
|
||||
String desc = etServiceDesc.getText().toString().trim();
|
||||
String durationStr = etServiceDuration.getText().toString().trim();
|
||||
String priceStr = etServicePrice.getText().toString().trim();
|
||||
|
||||
//check if all the fields are filled (desc is optional)
|
||||
if (name.isEmpty() || durationStr.isEmpty() || priceStr.isEmpty()) {
|
||||
Toast.makeText(getContext(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
//create a service object to send to the API
|
||||
ServiceDTO serviceDTO = new ServiceDTO();
|
||||
serviceDTO.setServiceName(name);
|
||||
serviceDTO.setServiceDesc(desc);
|
||||
serviceDTO.setServiceDuration(Integer.parseInt(durationStr));
|
||||
serviceDTO.setServicePrice(Double.parseDouble(priceStr));
|
||||
|
||||
ServiceApi serviceApi = RetrofitClient.getServiceApi(requireContext());
|
||||
|
||||
//check if the service is being edited or added
|
||||
if (isEditing) {
|
||||
// TODO: Update service
|
||||
Toast.makeText(getContext(), "Save functionality not yet implemented with DB", Toast.LENGTH_SHORT).show();
|
||||
// Update existing service
|
||||
serviceDTO.setServiceId((long) serviceId);
|
||||
serviceApi.updateService((long) serviceId, serviceDTO).enqueue(new Callback<ServiceDTO>() {
|
||||
@Override
|
||||
public void onResponse(Call<ServiceDTO> call, Response<ServiceDTO> response) {
|
||||
if (response.isSuccessful()) {
|
||||
Toast.makeText(getContext(), "Service updated successfully!", Toast.LENGTH_SHORT).show();
|
||||
navigateBack();
|
||||
} else {
|
||||
Toast.makeText(getContext(), "Failed to update service: " + response.code(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<ServiceDTO> call, Throwable t) {
|
||||
Log.e("ServiceDetailFragment", "Error updating service", t);
|
||||
Toast.makeText(getContext(), "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// TODO: Add new service
|
||||
Toast.makeText(getContext(), "Save functionality not yet implemented with DB", Toast.LENGTH_SHORT).show();
|
||||
// Add new service
|
||||
serviceApi.createService(serviceDTO).enqueue(new Callback<ServiceDTO>() {
|
||||
@Override
|
||||
public void onResponse(Call<ServiceDTO> call, Response<ServiceDTO> response) {
|
||||
if (response.isSuccessful()) {
|
||||
Toast.makeText(getContext(), "Service added successfully!", Toast.LENGTH_SHORT).show();
|
||||
navigateBack();
|
||||
} else {
|
||||
Toast.makeText(getContext(), "Failed to add service: " + response.code(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<ServiceDTO> call, Throwable t) {
|
||||
Log.e("ServiceDetailFragment", "Error adding service", t);
|
||||
Toast.makeText(getContext(), "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Method to Delete a service
|
||||
//Method to Delete a service
|
||||
private void deleteService() {
|
||||
Toast.makeText(getContext(), "Delete functionality not yet implemented with DB", Toast.LENGTH_SHORT).show();
|
||||
//Alert the user to confirm the delete
|
||||
new AlertDialog.Builder(requireContext())
|
||||
.setTitle("Delete Service")
|
||||
.setMessage("Are you sure you want to delete " + etServiceName.getText().toString() + "?")
|
||||
.setPositiveButton("Delete", (dialog, which) -> {
|
||||
ServiceApi serviceApi = RetrofitClient.getServiceApi(requireContext());
|
||||
serviceApi.deleteService((long) serviceId).enqueue(new Callback<Void>() {
|
||||
@Override
|
||||
public void onResponse(Call<Void> call, Response<Void> response) {
|
||||
if (response.isSuccessful()) {
|
||||
Toast.makeText(getContext(), "Service deleted successfully!", Toast.LENGTH_SHORT).show();
|
||||
navigateBack();
|
||||
} else {
|
||||
Toast.makeText(getContext(), "Failed to delete service: " + response.code(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Void> call, Throwable t) {
|
||||
Log.e("ServiceDetailFragment", "Error deleting service", t);
|
||||
Toast.makeText(getContext(), "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
})
|
||||
.setNegativeButton("Cancel", null)
|
||||
.show();
|
||||
}
|
||||
|
||||
//Helper method to navigate back to the list
|
||||
private void navigateBack() {
|
||||
ListFragment listFragment = (ListFragment) getParentFragment();
|
||||
if (listFragment != null) {
|
||||
listFragment.getChildFragmentManager().popBackStack();
|
||||
}
|
||||
}
|
||||
|
||||
//helper function to check if service is being edited or added and show the view accordingly
|
||||
@@ -106,4 +197,4 @@ public class ServiceDetailFragment extends Fragment {
|
||||
btnDeleteService = view.findViewById(R.id.btnDeleteService);
|
||||
btnBack = view.findViewById(R.id.btnBack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@ package com.example.petstoremobile.fragments.listfragments.detailfragments;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@@ -13,9 +15,16 @@ import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.petstoremobile.R;
|
||||
import com.example.petstoremobile.api.RetrofitClient;
|
||||
import com.example.petstoremobile.api.SupplierApi;
|
||||
import com.example.petstoremobile.dtos.SupplierDTO;
|
||||
import com.example.petstoremobile.fragments.ListFragment;
|
||||
import com.example.petstoremobile.fragments.listfragments.SupplierFragment;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
public class SupplierDetailFragment extends Fragment {
|
||||
|
||||
private TextView tvMode, tvSupId;
|
||||
@@ -40,33 +49,117 @@ public class SupplierDetailFragment extends Fragment {
|
||||
handleArguments();
|
||||
|
||||
//set button click listeners
|
||||
btnBack.setOnClickListener(v -> {
|
||||
//get the list fragment and pop the back stack to return to the previous view (SupplierFragment)
|
||||
ListFragment listFragment = (ListFragment) getParentFragment();
|
||||
if (listFragment != null) {
|
||||
listFragment.getChildFragmentManager().popBackStack();
|
||||
}
|
||||
});
|
||||
btnBack.setOnClickListener(v -> navigateBack());
|
||||
btnSaveSupplier.setOnClickListener(v -> saveSupplier());
|
||||
btnDeleteSupplier.setOnClickListener(v -> deleteSupplier());
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
//TODO: Method to Update or Add a supplier
|
||||
//Method to Update or Add a supplier
|
||||
private void saveSupplier() {
|
||||
//get all the values from the fields
|
||||
String company = etSupCompany.getText().toString().trim();
|
||||
String firstName = etSupContactFirstName.getText().toString().trim();
|
||||
String lastName = etSupContactLastName.getText().toString().trim();
|
||||
String email = etSupEmail.getText().toString().trim();
|
||||
String phone = etSupPhone.getText().toString().trim();
|
||||
|
||||
//check if all the fields are filled
|
||||
if (company.isEmpty() || firstName.isEmpty() || lastName.isEmpty() || email.isEmpty() || phone.isEmpty()) {
|
||||
Toast.makeText(getContext(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
//create a supplier object to send to the API
|
||||
SupplierDTO supplierDTO = new SupplierDTO();
|
||||
supplierDTO.setSupCompany(company);
|
||||
supplierDTO.setSupContactFirstName(firstName);
|
||||
supplierDTO.setSupContactLastName(lastName);
|
||||
supplierDTO.setSupEmail(email);
|
||||
supplierDTO.setSupPhone(phone);
|
||||
|
||||
SupplierApi supplierApi = RetrofitClient.getSupplierApi(requireContext());
|
||||
|
||||
//check if the supplier is being edited or added
|
||||
if (isEditing) {
|
||||
// TODO: Update supplier
|
||||
Toast.makeText(getContext(), "Save functionality not yet implemented with DB", Toast.LENGTH_SHORT).show();
|
||||
// Update existing supplier
|
||||
supplierDTO.setSupId((long) supId);
|
||||
supplierApi.updateSupplier((long) supId, supplierDTO).enqueue(new Callback<SupplierDTO>() {
|
||||
@Override
|
||||
public void onResponse(Call<SupplierDTO> call, Response<SupplierDTO> response) {
|
||||
if (response.isSuccessful()) {
|
||||
Toast.makeText(getContext(), "Supplier updated successfully!", Toast.LENGTH_SHORT).show();
|
||||
navigateBack();
|
||||
} else {
|
||||
Toast.makeText(getContext(), "Failed to update supplier: " + response.code(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<SupplierDTO> call, Throwable t) {
|
||||
Log.e("SupplierDetailFragment", "Error updating supplier", t);
|
||||
Toast.makeText(getContext(), "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// TODO: Add new supplier
|
||||
Toast.makeText(getContext(), "Save functionality not yet implemented with DB", Toast.LENGTH_SHORT).show();
|
||||
// Add new supplier
|
||||
supplierApi.createSupplier(supplierDTO).enqueue(new Callback<SupplierDTO>() {
|
||||
@Override
|
||||
public void onResponse(Call<SupplierDTO> call, Response<SupplierDTO> response) {
|
||||
if (response.isSuccessful()) {
|
||||
Toast.makeText(getContext(), "Supplier added successfully!", Toast.LENGTH_SHORT).show();
|
||||
navigateBack();
|
||||
} else {
|
||||
Toast.makeText(getContext(), "Failed to add supplier: " + response.code(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<SupplierDTO> call, Throwable t) {
|
||||
Log.e("SupplierDetailFragment", "Error adding supplier", t);
|
||||
Toast.makeText(getContext(), "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Method to Delete a supplier
|
||||
//Method to Delete a supplier
|
||||
private void deleteSupplier() {
|
||||
Toast.makeText(getContext(), "Delete functionality not yet implemented with DB", Toast.LENGTH_SHORT).show();
|
||||
//Alert the user to confirm the delete
|
||||
new AlertDialog.Builder(requireContext())
|
||||
.setTitle("Delete Supplier")
|
||||
.setMessage("Are you sure you want to delete " + etSupCompany.getText().toString() + "?")
|
||||
.setPositiveButton("Delete", (dialog, which) -> {
|
||||
SupplierApi supplierApi = RetrofitClient.getSupplierApi(requireContext());
|
||||
supplierApi.deleteSupplier((long) supId).enqueue(new Callback<Void>() {
|
||||
@Override
|
||||
public void onResponse(Call<Void> call, Response<Void> response) {
|
||||
if (response.isSuccessful()) {
|
||||
Toast.makeText(getContext(), "Supplier deleted successfully!", Toast.LENGTH_SHORT).show();
|
||||
navigateBack();
|
||||
} else {
|
||||
Toast.makeText(getContext(), "Failed to delete supplier: " + response.code(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Void> call, Throwable t) {
|
||||
Log.e("SupplierDetailFragment", "Error deleting supplier", t);
|
||||
Toast.makeText(getContext(), "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
})
|
||||
.setNegativeButton("Cancel", null)
|
||||
.show();
|
||||
}
|
||||
|
||||
//Helper method to navigate back to the list
|
||||
private void navigateBack() {
|
||||
ListFragment listFragment = (ListFragment) getParentFragment();
|
||||
if (listFragment != null) {
|
||||
listFragment.getChildFragmentManager().popBackStack();
|
||||
}
|
||||
}
|
||||
|
||||
//helper function to check if supplier is being edited or added and show the view accordingly
|
||||
@@ -108,4 +201,4 @@ public class SupplierDetailFragment extends Fragment {
|
||||
btnDeleteSupplier = view.findViewById(R.id.btnDeleteSupplier);
|
||||
btnBack = view.findViewById(R.id.btnBack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user