Skip to content

Commit

Permalink
Refactor more of NoteController so it is more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
robi1467 committed Apr 1, 2020
1 parent 6848963 commit 9c02b3f
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions server/src/main/java/umm3601/note/NoteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,38 +84,38 @@ public void deleteNote(Context ctx) {
noteCollection.deleteOne(eq("_id", new ObjectId(id)));
}

private void filterExpiredNotes(List<Note> notes){
for(int i = 0; i < notes.size(); i++){ // running through each index of the array
if(notes.get(i).expiration != null){ // makeing sure the expiration date exists
long testExpire = Instant.parse(notes.get(i).expiration).toEpochMilli();

if(checkIfExpired(testExpire) ){
String removeID = notes.get(i)._id;
System.out.println(notes.get(i).message + " is expired");
noteCollection.deleteOne(eq("_id",new ObjectId(removeID)));
}
}
}
}


/**
* Get a JSON response with a list of all the notes.
*
* @param ctx a Javalin HTTP context
*/
public void getOwnerNotes(Context ctx) {
//UpdateNotes
List<Bson> filters = new ArrayList<Bson>(); // start with a blank document
System.out.println("Date in milleseconds" + currentDateTime);
if (ctx.queryParamMap().containsKey("owner_id")) {
filters.add(eq("owner_id", ctx.queryParam("owner_id")));
List<Note> notes = noteCollection.find(and(filters)).into(new ArrayList<>());
for(int i = 0; i < notes.size(); i++){
if(notes.get(i).expiration != null){
long testExpire = Instant.parse(notes.get(i).expiration).toEpochMilli();

if(checkIfExpired(testExpire) ){
String removeID = notes.get(i)._id;
System.out.println(notes.get(i).message + " is expired");
noteCollection.deleteOne(eq("_id",new ObjectId(removeID)));
}

if (ctx.queryParamMap().containsKey("owner_id")) {//
filters.add(eq("owner_id", ctx.queryParam("owner_id")));// gathering the owner id
List<Note> notes = noteCollection.find(and(filters)).into(new ArrayList<>()); // creating an Array List of notes from database
// from a specific owner id
filterExpiredNotes(notes); // filtering out and deleting expired notes
}
}
// updateNotes(filters);
}

ctx.json(noteCollection.find(filters.isEmpty() ? new Document() : and(filters))
.into(new ArrayList<>()));
}

private boolean checkIfExpired(Long expiredDate){
if(expiredDate != null){
if(currentDateTime >= expiredDate){
Expand Down

0 comments on commit 9c02b3f

Please sign in to comment.