mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-15 18:28:47 +00:00
Merge pull request #117 from ClaytonWWilson/intra-list-ordering
Intra list ordering
This commit is contained in:
commit
b1184b1bf4
@ -9,14 +9,16 @@ public class List {
|
||||
long lastUpdated;
|
||||
ArrayList<ItemEntry> entries;
|
||||
boolean shared;
|
||||
Integer uiPosition;
|
||||
|
||||
public List(ResultSet listRow, boolean shared) throws SQLException {
|
||||
public List(ResultSet listRow, boolean shared, Integer uiPosition) throws SQLException {
|
||||
itemID = listRow.getInt("listID");
|
||||
name = listRow.getString("name");
|
||||
owner = listRow.getString("owner");
|
||||
lastUpdated = listRow.getTimestamp("lastUpdated").toInstant().toEpochMilli();
|
||||
entries = new ArrayList<>();
|
||||
this.shared = shared;
|
||||
this.uiPosition = uiPosition;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -27,6 +29,8 @@ public class List {
|
||||
", owner='" + owner + '\'' +
|
||||
", lastUpdated=" + lastUpdated +
|
||||
", entries=" + entries +
|
||||
", shared=" + shared +
|
||||
", uiPosition=" + uiPosition +
|
||||
'}';
|
||||
}
|
||||
|
||||
@ -70,7 +74,14 @@ public class List {
|
||||
this.shared = shared;
|
||||
}
|
||||
|
||||
public ItemEntry[] getEntries() {
|
||||
public Integer getUiPosition() {
|
||||
return uiPosition;
|
||||
}
|
||||
|
||||
public void setUiPosition(Integer uiPosition) {
|
||||
this.uiPosition = uiPosition;
|
||||
|
||||
public ItemEntry[] getEntries() {
|
||||
return entries.toArray(new ItemEntry[entries.size()]);
|
||||
}
|
||||
|
||||
|
||||
@ -9,7 +9,8 @@ public class ListAdder implements CallHandler {
|
||||
private String cognitoID;
|
||||
|
||||
private final String LIST_CREATE = "INSERT INTO List (name, owner, lastUpdated) VALUES (?, ?, ?);";
|
||||
private final String LIST_ACCESS_GRANT = "INSERT INTO ListSharee(listID, userID, permissionLevel) VALUES(?, ?, ?);";
|
||||
private final String LIST_ACCESS_GRANT = "INSERT INTO ListSharee(listID, userID, permissionLevel, uiPosition) VALUES(?, ?, ?, ?);";
|
||||
private final String UI_POSITION_CHECK = "SELECT Max(uiPosition) as maxUIPosition FROM ListSharee WHERE userID = ?;";
|
||||
|
||||
public ListAdder(Connection connection, String cognitoID) {
|
||||
this.connection = connection;
|
||||
@ -17,9 +18,17 @@ public class ListAdder implements CallHandler {
|
||||
}
|
||||
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
|
||||
PreparedStatement statement = connection.prepareStatement(LIST_CREATE, Statement.RETURN_GENERATED_KEYS);
|
||||
|
||||
String listName = bodyMap.get("name").toString();//Needs safe checking
|
||||
|
||||
PreparedStatement uiPositionCheck = connection.prepareStatement(UI_POSITION_CHECK);
|
||||
uiPositionCheck.setString(1, cognitoID);
|
||||
ResultSet uiPositionCheckRS = uiPositionCheck.executeQuery();
|
||||
int nextPosition = 1;
|
||||
if (uiPositionCheckRS.next()) {
|
||||
nextPosition = uiPositionCheckRS.getInt("maxUIPosition") + 1;
|
||||
}
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement(LIST_CREATE, Statement.RETURN_GENERATED_KEYS);
|
||||
statement.setString(1, listName);
|
||||
statement.setString(2, cognitoID);
|
||||
statement.setTimestamp(3, Timestamp.from(Instant.now()));
|
||||
@ -32,6 +41,7 @@ public class ListAdder implements CallHandler {
|
||||
accessGrant.setInt(1, newID);
|
||||
accessGrant.setString(2, cognitoID);
|
||||
accessGrant.setInt(3, ListPermissions.getAll());
|
||||
accessGrant.setInt(4, nextPosition);
|
||||
System.out.println(accessGrant);
|
||||
accessGrant.executeUpdate();
|
||||
connection.commit();
|
||||
|
||||
@ -12,7 +12,7 @@ public class ListGetter implements CallHandler{
|
||||
private final String cognitoID;
|
||||
|
||||
private final String GET_LIST = "SELECT * FROM List WHERE listID = ?;";
|
||||
private final String GET_LISTS = "SELECT listID FROM ListSharee WHERE userID = ?;";
|
||||
private final String GET_LISTS = "SELECT listID FROM ListSharee WHERE userID = ? ORDER BY uiPosition;";
|
||||
private final String SHARE_CHECK = "SELECT * FROM ListSharee WHERE listID = ?;";
|
||||
private final String GET_ENTRIES = "SELECT * FROM ListProduct WHERE listID = ?;";
|
||||
|
||||
@ -42,6 +42,7 @@ public class ListGetter implements CallHandler{
|
||||
ResultSet accessResults = checkAccess.executeQuery();
|
||||
int sharees = 0;
|
||||
boolean verifiedAccess = false;
|
||||
int uiPosition = 1;
|
||||
while ((sharees < 2 && accessResults.next()) || !verifiedAccess) {
|
||||
int permissionLevel = accessResults.getInt("permissionLevel");
|
||||
if (accessResults.getString("userID").equals(cognitoID)) {
|
||||
@ -49,6 +50,7 @@ public class ListGetter implements CallHandler{
|
||||
if (!ListPermissions.hasPermission(permissionLevel, "Read")) {
|
||||
throw new AccessControlException("User " + cognitoID + " does not have permission to read list " + id);
|
||||
}
|
||||
uiPosition = accessResults.getInt("uiPosition");
|
||||
}
|
||||
if (permissionLevel > 0) {
|
||||
sharees++;
|
||||
@ -64,7 +66,7 @@ public class ListGetter implements CallHandler{
|
||||
ResultSet getListResults = getList.executeQuery();
|
||||
getListResults.first();
|
||||
System.out.println(getListResults);
|
||||
List retrievedList = new List(getListResults, shared);
|
||||
List retrievedList = new List(getListResults, shared, uiPosition);
|
||||
System.out.println(retrievedList);
|
||||
PreparedStatement getListEntries = connection.prepareStatement(GET_ENTRIES);
|
||||
getListEntries.setInt(1, id);
|
||||
|
||||
70
Lambdas/Lists/ListReposition/src/ListRepositionActor.java
Normal file
70
Lambdas/Lists/ListReposition/src/ListRepositionActor.java
Normal file
@ -0,0 +1,70 @@
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ListRepositionActor implements CallHandler {
|
||||
|
||||
private Connection connection;
|
||||
private String cognitoID;
|
||||
|
||||
public ListRepositionActor(Connection connection, String cognitoID) {
|
||||
this.connection = connection;
|
||||
this.cognitoID = cognitoID;
|
||||
}
|
||||
|
||||
final private String GET_PRIOR_POSITION = "SELECT uiPosition FROM ListSharee WHERE userID = ? AND listID = ?;";
|
||||
final private String SET_NEW_POSITION = "UPDATE ListSharee SET uiPosition = ? WHERE userID = ? AND listID = ?;";
|
||||
final private String DECREMENT_HIGHER_POSITIONS = "UPDATE ListSharee SET uiPosition = uiPosition - 1 WHERE uiPosition > ? AND userID = ?;";
|
||||
final private String INCREMENT_GEQ_POSITIONS = "UPDATE ListSharee SET uiPosition = uiPosition + 1 WHERE uiPosition >= ? AND userID = ?;";
|
||||
|
||||
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
|
||||
Integer listID = (Integer) bodyMap.get("listID");
|
||||
Integer newPosition = (Integer) bodyMap.get("newPosition");
|
||||
|
||||
PreparedStatement getPriorPosition = connection.prepareStatement(GET_PRIOR_POSITION);
|
||||
getPriorPosition.setString(1, cognitoID);
|
||||
getPriorPosition.setInt(2, listID);
|
||||
ResultSet priorPositionRS = getPriorPosition.executeQuery();
|
||||
if (!priorPositionRS.next()) {
|
||||
throw new IllegalArgumentException("Bad listID for user");
|
||||
}
|
||||
Integer priorPosition = priorPositionRS.getInt("uiPosition");
|
||||
|
||||
PreparedStatement openNewPosition = connection.prepareStatement(INCREMENT_GEQ_POSITIONS);
|
||||
|
||||
if (newPosition.equals(priorPosition)) {
|
||||
return null;
|
||||
}
|
||||
if (newPosition < priorPosition) {
|
||||
openNewPosition.setInt(1, newPosition);
|
||||
} else {
|
||||
openNewPosition.setInt(1, newPosition + 1);
|
||||
}
|
||||
openNewPosition.setString(2, cognitoID);
|
||||
System.out.println(openNewPosition);
|
||||
openNewPosition.executeUpdate();
|
||||
|
||||
PreparedStatement fillPriorPosition = connection.prepareStatement(DECREMENT_HIGHER_POSITIONS);
|
||||
fillPriorPosition.setInt(1, priorPosition);
|
||||
fillPriorPosition.setString(2, cognitoID);
|
||||
System.out.println(fillPriorPosition);
|
||||
fillPriorPosition.executeUpdate();
|
||||
|
||||
|
||||
|
||||
PreparedStatement setNewPosition = connection.prepareStatement(SET_NEW_POSITION);
|
||||
setNewPosition.setInt(1, newPosition);
|
||||
setNewPosition.setString(2, cognitoID);
|
||||
setNewPosition.setInt(3, listID);
|
||||
System.out.println(setNewPosition);
|
||||
setNewPosition.executeUpdate();
|
||||
|
||||
connection.commit();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
11
Lambdas/Lists/ListReposition/src/ListRepositionPUT.java
Normal file
11
Lambdas/Lists/ListReposition/src/ListRepositionPUT.java
Normal file
@ -0,0 +1,11 @@
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestHandler;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class ListRepositionPUT implements RequestHandler<Map<String,Object>, Object> {
|
||||
|
||||
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
|
||||
return BasicHandler.handleRequest(inputMap, unfilled, ListRepositionActor.class);
|
||||
}
|
||||
}
|
||||
@ -22,9 +22,9 @@ public class ListSharer implements CallHandler {
|
||||
}
|
||||
|
||||
final private String CHECK_ACCESS = "SELECT * from ListSharee WHERE listID = ? AND userID = ?;";
|
||||
private final String UI_POSITION_CHECK = "SELECT Max(uiPosition) as maxUIPosition FROM ListSharee WHERE userID = ?;";
|
||||
final private String SHARE_LIST = "INSERT INTO ListSharee(listID, userID, permissionLevel, uiPosition) VALUES(?, ?, ?, ?) ON DUPLICATE KEY UPDATE permissionLevel = ?;";
|
||||
|
||||
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
|
||||
PreparedStatement checkAccess = connection.prepareStatement(CHECK_ACCESS);
|
||||
Integer listID = Integer.parseInt(bodyMap.get("listID").toString());
|
||||
@ -63,12 +63,23 @@ public class ListSharer implements CallHandler {
|
||||
// throw new InputMismatchException("The specified user already has access");
|
||||
// }
|
||||
|
||||
PreparedStatement uiPositionCheck = connection.prepareStatement(UI_POSITION_CHECK);
|
||||
uiPositionCheck.setString(1, shareWithSub);
|
||||
System.out.println(uiPositionCheck);
|
||||
ResultSet uiPositionCheckRS = uiPositionCheck.executeQuery();
|
||||
int nextPosition = 1;
|
||||
if (uiPositionCheckRS.next()) {
|
||||
nextPosition = uiPositionCheckRS.getInt("maxUIPosition") + 1;
|
||||
}
|
||||
|
||||
PreparedStatement shareList = connection.prepareStatement(SHARE_LIST);
|
||||
shareList.setInt(1, listID);
|
||||
shareList.setString(2, shareWithSub);
|
||||
Integer permissionLevel = Integer.parseInt(bodyMap.get("permissionLevel").toString());
|
||||
shareList.setInt(3, permissionLevel);
|
||||
shareList.setInt(4, permissionLevel);
|
||||
shareList.setInt(4, nextPosition);
|
||||
shareList.setInt(5, permissionLevel);
|
||||
System.out.println(shareList);
|
||||
shareList.executeUpdate();
|
||||
connection.commit();
|
||||
return null;
|
||||
|
||||
@ -1,27 +1,15 @@
|
||||
package com.example.listify;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.amplifyframework.auth.AuthException;
|
||||
import android.view.View;
|
||||
import android.widget.*;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.example.listify.data.List;
|
||||
import com.example.listify.data.ListEntry;
|
||||
import com.example.listify.model.Product;
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -239,7 +227,7 @@ public class ItemDetails extends AppCompatActivity implements ListPickerDialogFr
|
||||
Requestor requestor = new Requestor(am, configs.getProperty("apiKey"));
|
||||
SynchronousReceiver<Integer> idReceiver = new SynchronousReceiver<>();
|
||||
|
||||
com.example.listify.data.List newList = new List(-1, name, "user filled by lambda", Instant.now().toEpochMilli());
|
||||
com.example.listify.data.List newList = new List(-1, name, "user filled by lambda", Instant.now().toEpochMilli(), -1);
|
||||
|
||||
Thread t = new Thread(new Runnable() {
|
||||
@Override
|
||||
|
||||
@ -17,6 +17,7 @@ import androidx.navigation.ui.AppBarConfiguration;
|
||||
import androidx.navigation.ui.NavigationUI;
|
||||
import com.amplifyframework.auth.AuthException;
|
||||
import com.example.listify.data.List;
|
||||
import com.example.listify.data.ListReposition;
|
||||
import com.example.listify.data.SearchHistory;
|
||||
import com.example.listify.ui.LoginPage;
|
||||
import com.google.android.material.navigation.NavigationView;
|
||||
@ -108,6 +109,7 @@ public class MainActivity extends AppCompatActivity implements CreateListDialogF
|
||||
requestor.getObject("N/A", SearchHistory.class, historyReceiver, historyReceiver);
|
||||
try {
|
||||
System.out.println(historyReceiver.await());
|
||||
requestor.putObject(new ListReposition(291, 1));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -205,7 +207,7 @@ public class MainActivity extends AppCompatActivity implements CreateListDialogF
|
||||
Requestor requestor = new Requestor(am, configs.getProperty("apiKey"));
|
||||
SynchronousReceiver<Integer> idReceiver = new SynchronousReceiver<>();
|
||||
|
||||
List newList = new List(-1, name, "user filled by lambda", Instant.now().toEpochMilli());
|
||||
List newList = new List(-1, name, "user filled by lambda", Instant.now().toEpochMilli(), -1);
|
||||
|
||||
try {
|
||||
requestor.postObject(newList, idReceiver, idReceiver);
|
||||
|
||||
@ -9,18 +9,20 @@ public class List {
|
||||
long lastUpdated;
|
||||
final ListEntry[] entries;
|
||||
boolean shared;
|
||||
Integer uiPosition;
|
||||
|
||||
public List(Integer itemID, String name, String owner, long lastUpdated, ListEntry[] entries, boolean shared) {
|
||||
public List(Integer itemID, String name, String owner, long lastUpdated, ListEntry[] entries, boolean shared, Integer uiPosition) {
|
||||
this.itemID = itemID;
|
||||
this.name = name;
|
||||
this.owner = owner;
|
||||
this.lastUpdated = lastUpdated;
|
||||
this.entries = entries;
|
||||
this.shared = false;
|
||||
this.uiPosition = uiPosition;
|
||||
}
|
||||
|
||||
public List(Integer itemID, String name, String owner, long lastUpdated) {
|
||||
this(itemID, name, owner, lastUpdated, null, false);
|
||||
public List(Integer itemID, String name, String owner, long lastUpdated, Integer uiPosition) {
|
||||
this(itemID, name, owner, lastUpdated, null, false, uiPosition);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -32,6 +34,7 @@ public class List {
|
||||
", lastUpdated=" + lastUpdated +
|
||||
", entries=" + Arrays.toString(entries) +
|
||||
", shared=" + shared +
|
||||
", uiPosition=" + uiPosition +
|
||||
'}';
|
||||
}
|
||||
|
||||
@ -75,6 +78,13 @@ public class List {
|
||||
this.shared = shared;
|
||||
}
|
||||
|
||||
public Integer getUiPosition() {
|
||||
return uiPosition;
|
||||
}
|
||||
|
||||
public void setUiPosition(Integer uiPosition) {
|
||||
this.uiPosition = uiPosition;
|
||||
|
||||
public ListEntry[] getEntries() {
|
||||
return entries;
|
||||
}
|
||||
|
||||
@ -0,0 +1,35 @@
|
||||
package com.example.listify.data;
|
||||
|
||||
public class ListReposition {
|
||||
Integer listID;
|
||||
Integer newPosition;
|
||||
|
||||
public ListReposition(Integer listID, Integer newPosition) {
|
||||
this.listID = listID;
|
||||
this.newPosition = newPosition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ListReposition{" +
|
||||
"listID=" + listID +
|
||||
", newPosition=" + newPosition +
|
||||
'}';
|
||||
}
|
||||
|
||||
public Integer getListID() {
|
||||
return listID;
|
||||
}
|
||||
|
||||
public void setListID(Integer listID) {
|
||||
this.listID = listID;
|
||||
}
|
||||
|
||||
public Integer getNewPosition() {
|
||||
return newPosition;
|
||||
}
|
||||
|
||||
public void setNewPosition(Integer newPosition) {
|
||||
this.newPosition = newPosition;
|
||||
}
|
||||
}
|
||||
@ -88,7 +88,7 @@ public class HomeFragment extends Fragment implements CreateListDialogFragment.O
|
||||
Requestor requestor = new Requestor(am, configs.getProperty("apiKey"));
|
||||
SynchronousReceiver<Integer> idReceiver = new SynchronousReceiver<>();
|
||||
|
||||
List newList = new List(-1, name, "user filled by lambda", Instant.now().toEpochMilli());
|
||||
List newList = new List(-1, name, "user filled by lambda", Instant.now().toEpochMilli() , -1);
|
||||
|
||||
try {
|
||||
requestor.postObject(newList, idReceiver, idReceiver);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user