From ae83860ed8bf44621ece47b5e58b14586bd82b8d Mon Sep 17 00:00:00 2001 From: Austin Robinson Date: Wed, 25 Mar 2020 17:23:57 -0400 Subject: [PATCH 01/12] Add expire date variable for note in client side Also add a base 5 hours for the expire date --- client/src/app/notes/add-note.component.ts | 10 ++++++++++ client/src/app/notes/note.ts | 1 + 2 files changed, 11 insertions(+) diff --git a/client/src/app/notes/add-note.component.ts b/client/src/app/notes/add-note.component.ts index 14a8f68..09819fe 100644 --- a/client/src/app/notes/add-note.component.ts +++ b/client/src/app/notes/add-note.component.ts @@ -58,11 +58,21 @@ export class AddNoteComponent implements OnInit { submitForm() { const formResults = this.addNoteForm.value; + + const currentDate = new Date(); + const newDate = new Date(currentDate.setHours(currentDate.getHours() + 5)); + console.log('TIMES!!!!:\n' + + 'JSON: ' + newDate.toJSON() + '\n' + + 'ISO: ' + newDate.toISOString() + '\n' + + 'UTC: ' + newDate.toUTCString() + '\n' + + 'DATE: ' + newDate.toDateString() + '\n' + ); const newNote: Note = { owner_id: this.id, _id: undefined, //owner: formResults.owner, message: formResults.message, + expiration: newDate.toJSON(), }; this.noteService.addNote(this.id, newNote).subscribe(() => { diff --git a/client/src/app/notes/note.ts b/client/src/app/notes/note.ts index 38136ac..127efa9 100644 --- a/client/src/app/notes/note.ts +++ b/client/src/app/notes/note.ts @@ -2,4 +2,5 @@ export interface Note { _id: string; owner_id: string; message: string; + expiration: string; } From cb89412d0c33421a54dec38c2e1e6df823c153d4 Mon Sep 17 00:00:00 2001 From: Austin Robinson Date: Wed, 25 Mar 2020 17:31:18 -0400 Subject: [PATCH 02/12] Add expire date variable to server side note class --- server/src/main/java/umm3601/note/Note.java | 1 + 1 file changed, 1 insertion(+) diff --git a/server/src/main/java/umm3601/note/Note.java b/server/src/main/java/umm3601/note/Note.java index 9d4fa97..60945f8 100644 --- a/server/src/main/java/umm3601/note/Note.java +++ b/server/src/main/java/umm3601/note/Note.java @@ -11,4 +11,5 @@ public class Note { public String owner_id; public String message; + public String expireDate; } From 09bc7d18c48e763c08c3534da48e64af7addada2 Mon Sep 17 00:00:00 2001 From: Austin Robinson Date: Fri, 27 Mar 2020 00:47:09 -0400 Subject: [PATCH 03/12] Update client side test work on troubleshooting on why notes can't be added with expire date. It seems to be a problem server side at this point. Also add console log checks to see what is being called. Update the test so the mock note include an expire date --- client/src/app/notes/add-note.component.ts | 12 +++------ client/src/app/notes/note.service.spec.ts | 14 ++++++++--- client/src/app/notes/note.service.ts | 5 +++- client/src/testing/note.service.mock.ts | 9 ++++--- .../java/umm3601/note/NoteController.java | 25 +++++++++++-------- 5 files changed, 38 insertions(+), 27 deletions(-) diff --git a/client/src/app/notes/add-note.component.ts b/client/src/app/notes/add-note.component.ts index 09819fe..7b8ce07 100644 --- a/client/src/app/notes/add-note.component.ts +++ b/client/src/app/notes/add-note.component.ts @@ -61,21 +61,15 @@ export class AddNoteComponent implements OnInit { const currentDate = new Date(); const newDate = new Date(currentDate.setHours(currentDate.getHours() + 5)); - console.log('TIMES!!!!:\n' - + 'JSON: ' + newDate.toJSON() + '\n' - + 'ISO: ' + newDate.toISOString() + '\n' - + 'UTC: ' + newDate.toUTCString() + '\n' - + 'DATE: ' + newDate.toDateString() + '\n' - ); + const newNote: Note = { owner_id: this.id, _id: undefined, - //owner: formResults.owner, message: formResults.message, - expiration: newDate.toJSON(), + expiration: newDate.toISOString() }; - this.noteService.addNote(this.id, newNote).subscribe(() => { + this.noteService.addNote(this.id, newNote).subscribe((newID) => { this.snackBar.open('Posted', null, { duration: 2000, }); diff --git a/client/src/app/notes/note.service.spec.ts b/client/src/app/notes/note.service.spec.ts index 401ffa5..5f9cf27 100644 --- a/client/src/app/notes/note.service.spec.ts +++ b/client/src/app/notes/note.service.spec.ts @@ -6,26 +6,32 @@ import { NoteService } from './note.service'; import { request } from 'http'; describe('Note service: ', () => { + const date = new Date(); + const newDate = new Date(date.setHours(date.getHours() + 5)); const testNotes: Note[] = [ { _id: '401965892d4c0b6138467f51', owner_id: '588935f57546a2daea44de7c', - message: 'I\'m going to be a few minutes late to my office hours today. I got caught in traffic this morning.' + message: 'I\'m going to be a few minutes late to my office hours today. I got caught in traffic this morning.', + expiration: newDate.toISOString() }, { _id: '588935f57546a2daea44de7d', owner_id: '588935f57546a2daea44de7c', - message: 'Never mind, it seems like traffic is worse than I expected. Office hours will be moved up a half hour.' + message: 'Never mind, it seems like traffic is worse than I expected. Office hours will be moved up a half hour.', + expiration: newDate.toISOString() }, { _id: '588935f57346a2daea44de7f', owner_id: '588935f57546a2daea44de7e', - message: 'Office hours are canceled today!' + message: 'Office hours are canceled today!', + expiration: newDate.toISOString() }, { _id: '588935f57446a2daea44de7d', owner_id: '78f1d3bfa098879fe7a01373', - message: 'Just getting into the building now, sorry for the delay!' + message: 'Just getting into the building now, sorry for the delay!', + expiration: newDate.toISOString() }, ]; let noteService: NoteService; diff --git a/client/src/app/notes/note.service.ts b/client/src/app/notes/note.service.ts index 21d86c2..85b074e 100644 --- a/client/src/app/notes/note.service.ts +++ b/client/src/app/notes/note.service.ts @@ -29,7 +29,10 @@ export class NoteService { addNote(id: string, newNote: Note): Observable { // Send post request to add a new user with the user data as the body. - return this.httpClient.post<{id: string}> + console.log('called\n'); + const client =this.httpClient.post<{id: string}> (this.ownerUrl + '/' + id + '/notes/new', newNote).pipe(map(res => res.id)); + console.log(newNote.expiration); + return client; } } diff --git a/client/src/testing/note.service.mock.ts b/client/src/testing/note.service.mock.ts index 7425f1f..1e04639 100644 --- a/client/src/testing/note.service.mock.ts +++ b/client/src/testing/note.service.mock.ts @@ -14,17 +14,20 @@ export class MockNoteService extends NoteService { { _id: 'one_id', message: 'Spirate pur, Spirate', - owner_id: 'Aladdin' + owner_id: 'Aladdin', + expiration: "N/A", }, { _id: 'two_id', message: 'In te spera verunt', - owner_id: 'Belle' + owner_id: 'Belle', + expiration: "N/A", }, { _id: 'three_id', message: 'patre nostris', - owner_id: 'Genie' + owner_id: 'Genie', + expiration: "N/A", } ]; diff --git a/server/src/main/java/umm3601/note/NoteController.java b/server/src/main/java/umm3601/note/NoteController.java index a7ce7df..8e5811e 100644 --- a/server/src/main/java/umm3601/note/NoteController.java +++ b/server/src/main/java/umm3601/note/NoteController.java @@ -99,12 +99,17 @@ public void getOwnerNotes(Context ctx) { * @param ctx a Javalin HTTP context */ public void addNewNote(Context ctx) { + System.out.println("METHOD IS CALLED!!"); Note newNote = ctx.bodyValidator(Note.class) .check((pst) -> pst.message != null) // note should have a message .check((pst) -> pst.owner_id != null) // note should have an owner_id + .check((pst)-> pst.expireDate != null) .get(); + System.out.println("INSERTING"); noteCollection.insertOne(newNote); + System.out.println("INSERTED"); ctx.status(201); + System.out.println("status"); ctx.json(ImmutableMap.of("id", newNote._id)); } @@ -114,14 +119,14 @@ public void addNewNote(Context ctx) { * * @param str the string to generate a md5 for */ - public String md5(String str) throws NoSuchAlgorithmException { - MessageDigest md = MessageDigest.getInstance("MD5"); - byte[] hashInBytes = md.digest(str.toLowerCase().getBytes(StandardCharsets.UTF_8)); - - String result = ""; - for (byte b : hashInBytes) { - result += String.format("%02x", b); - } - return result; - } + // public String md5(String str) throws NoSuchAlgorithmException { + // MessageDigest md = MessageDigest.getInstance("MD5"); + // byte[] hashInBytes = md.digest(str.toLowerCase().getBytes(StandardCharsets.UTF_8)); + + // String result = ""; + // for (byte b : hashInBytes) { + // result += String.format("%02x", b); + // } + // return result; + // } } From 2a2844bf1a5da0213f31d8c00efff93c420df03b Mon Sep 17 00:00:00 2001 From: Austin Robinson Date: Fri, 27 Mar 2020 01:16:34 -0400 Subject: [PATCH 04/12] Fix naming conflict between server and client I had set the name for expiration date in the server to expire date which was why a note could ne be added. I also fixed an issue with formatting in the testing file for the note controller --- client/src/app/notes/note.service.ts | 2 +- server/src/main/java/umm3601/note/Note.java | 2 +- server/src/main/java/umm3601/note/NoteController.java | 1 - server/src/test/java/umm3601/note/NoteControllerSpec.java | 7 ++++++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/client/src/app/notes/note.service.ts b/client/src/app/notes/note.service.ts index 85b074e..7ca0f29 100644 --- a/client/src/app/notes/note.service.ts +++ b/client/src/app/notes/note.service.ts @@ -30,7 +30,7 @@ export class NoteService { addNote(id: string, newNote: Note): Observable { // Send post request to add a new user with the user data as the body. console.log('called\n'); - const client =this.httpClient.post<{id: string}> + const client = this.httpClient.post<{id: string}> (this.ownerUrl + '/' + id + '/notes/new', newNote).pipe(map(res => res.id)); console.log(newNote.expiration); return client; diff --git a/server/src/main/java/umm3601/note/Note.java b/server/src/main/java/umm3601/note/Note.java index 60945f8..fabdb75 100644 --- a/server/src/main/java/umm3601/note/Note.java +++ b/server/src/main/java/umm3601/note/Note.java @@ -11,5 +11,5 @@ public class Note { public String owner_id; public String message; - public String expireDate; + public String expiration; } diff --git a/server/src/main/java/umm3601/note/NoteController.java b/server/src/main/java/umm3601/note/NoteController.java index 8e5811e..8a75dc7 100644 --- a/server/src/main/java/umm3601/note/NoteController.java +++ b/server/src/main/java/umm3601/note/NoteController.java @@ -103,7 +103,6 @@ public void addNewNote(Context ctx) { Note newNote = ctx.bodyValidator(Note.class) .check((pst) -> pst.message != null) // note should have a message .check((pst) -> pst.owner_id != null) // note should have an owner_id - .check((pst)-> pst.expireDate != null) .get(); System.out.println("INSERTING"); noteCollection.insertOne(newNote); diff --git a/server/src/test/java/umm3601/note/NoteControllerSpec.java b/server/src/test/java/umm3601/note/NoteControllerSpec.java index 85e3863..c43648a 100644 --- a/server/src/test/java/umm3601/note/NoteControllerSpec.java +++ b/server/src/test/java/umm3601/note/NoteControllerSpec.java @@ -86,14 +86,19 @@ public void setupEach() throws IOException { testNotes.add(Document.parse("{\n" + " message: \"I wanna say something,\",\n" + " owner_id: \"1310\",\n" + + " expiration: \"2021-03-27T04:52:37.888Z\",\n" + " }")); testNotes.add(Document.parse("{\n" + " message: \"But we're leaving\",\n" + " owner_id: \"1523\",\n" + + " expiration: \"2021-03-27T04:52:37.888Z\",\n" + + " }")); testNotes.add(Document.parse("{\n" + " message: \"And it's over\",\n" + " owner_id: \"1600\",\n" + + " expiration: \"2021-03-27T04:52:37.888Z\",\n" + + " }")); samsId = new ObjectId(); @@ -137,7 +142,7 @@ public void GetNotesByOwner_id() throws IOException { @Test public void AddNote() throws IOException { - String testNewNote = "{\n\t\"message\": \"Alien\",\n\t\"owner_id\": \"coolguyid\"\n}"; + String testNewNote = "{\n\t\"message\": \"Alien\",\n\t\"owner_id\": \"coolguyid\",\n\t\"expiration\": \"2021-03-27T04:52:37.888Z\"\n}"; mockReq.setBodyContent(testNewNote); mockReq.setMethod("POST"); From e736831306d07f368ff8a1e7fe6d11183ec88163 Mon Sep 17 00:00:00 2001 From: Austin Robinson Date: Fri, 27 Mar 2020 01:16:34 -0400 Subject: [PATCH 05/12] Fix naming conflict between server and client #10 I had set the name for expiration date in the server to expire date which was why a note could ne be added. I also fixed an issue with formatting in the testing file for the note controller --- client/src/app/notes/note.service.ts | 2 +- server/src/main/java/umm3601/note/Note.java | 2 +- server/src/main/java/umm3601/note/NoteController.java | 1 - server/src/test/java/umm3601/note/NoteControllerSpec.java | 7 ++++++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/client/src/app/notes/note.service.ts b/client/src/app/notes/note.service.ts index 85b074e..7ca0f29 100644 --- a/client/src/app/notes/note.service.ts +++ b/client/src/app/notes/note.service.ts @@ -30,7 +30,7 @@ export class NoteService { addNote(id: string, newNote: Note): Observable { // Send post request to add a new user with the user data as the body. console.log('called\n'); - const client =this.httpClient.post<{id: string}> + const client = this.httpClient.post<{id: string}> (this.ownerUrl + '/' + id + '/notes/new', newNote).pipe(map(res => res.id)); console.log(newNote.expiration); return client; diff --git a/server/src/main/java/umm3601/note/Note.java b/server/src/main/java/umm3601/note/Note.java index 60945f8..fabdb75 100644 --- a/server/src/main/java/umm3601/note/Note.java +++ b/server/src/main/java/umm3601/note/Note.java @@ -11,5 +11,5 @@ public class Note { public String owner_id; public String message; - public String expireDate; + public String expiration; } diff --git a/server/src/main/java/umm3601/note/NoteController.java b/server/src/main/java/umm3601/note/NoteController.java index 8e5811e..8a75dc7 100644 --- a/server/src/main/java/umm3601/note/NoteController.java +++ b/server/src/main/java/umm3601/note/NoteController.java @@ -103,7 +103,6 @@ public void addNewNote(Context ctx) { Note newNote = ctx.bodyValidator(Note.class) .check((pst) -> pst.message != null) // note should have a message .check((pst) -> pst.owner_id != null) // note should have an owner_id - .check((pst)-> pst.expireDate != null) .get(); System.out.println("INSERTING"); noteCollection.insertOne(newNote); diff --git a/server/src/test/java/umm3601/note/NoteControllerSpec.java b/server/src/test/java/umm3601/note/NoteControllerSpec.java index 85e3863..c43648a 100644 --- a/server/src/test/java/umm3601/note/NoteControllerSpec.java +++ b/server/src/test/java/umm3601/note/NoteControllerSpec.java @@ -86,14 +86,19 @@ public void setupEach() throws IOException { testNotes.add(Document.parse("{\n" + " message: \"I wanna say something,\",\n" + " owner_id: \"1310\",\n" + + " expiration: \"2021-03-27T04:52:37.888Z\",\n" + " }")); testNotes.add(Document.parse("{\n" + " message: \"But we're leaving\",\n" + " owner_id: \"1523\",\n" + + " expiration: \"2021-03-27T04:52:37.888Z\",\n" + + " }")); testNotes.add(Document.parse("{\n" + " message: \"And it's over\",\n" + " owner_id: \"1600\",\n" + + " expiration: \"2021-03-27T04:52:37.888Z\",\n" + + " }")); samsId = new ObjectId(); @@ -137,7 +142,7 @@ public void GetNotesByOwner_id() throws IOException { @Test public void AddNote() throws IOException { - String testNewNote = "{\n\t\"message\": \"Alien\",\n\t\"owner_id\": \"coolguyid\"\n}"; + String testNewNote = "{\n\t\"message\": \"Alien\",\n\t\"owner_id\": \"coolguyid\",\n\t\"expiration\": \"2021-03-27T04:52:37.888Z\"\n}"; mockReq.setBodyContent(testNewNote); mockReq.setMethod("POST"); From ad986757b676a4b37c34864c9e74a3b6d7930f29 Mon Sep 17 00:00:00 2001 From: Austin Robinson Date: Sat, 28 Mar 2020 15:36:18 -0400 Subject: [PATCH 06/12] Allow server to parse dates sent by the client #22 --- .../app/owners/owner-doorboard.component.ts | 4 +- .../java/umm3601/note/NoteController.java | 44 ++++++++++++------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/client/src/app/owners/owner-doorboard.component.ts b/client/src/app/owners/owner-doorboard.component.ts index ea40cf3..7938715 100644 --- a/client/src/app/owners/owner-doorboard.component.ts +++ b/client/src/app/owners/owner-doorboard.component.ts @@ -29,7 +29,9 @@ export class OwnerDoorBoardComponent implements OnInit, OnDestroy { this.route.paramMap.subscribe((pmap) => { this.id = pmap.get('id'); this.getOwnerSub = this.ownerService.getOwnerById(this.id).subscribe(owner => this.owner = owner); - this.getNotesSub = this.noteService.getOwnerNotes({ owner_id: this.id }).subscribe(notes => this.notes = notes.reverse()); + this.getNotesSub = this.noteService.getOwnerNotes({ owner_id: this.id }).subscribe(notes => + this.notes = notes.reverse() + ); }); } diff --git a/server/src/main/java/umm3601/note/NoteController.java b/server/src/main/java/umm3601/note/NoteController.java index 8a75dc7..5d0e6ed 100644 --- a/server/src/main/java/umm3601/note/NoteController.java +++ b/server/src/main/java/umm3601/note/NoteController.java @@ -4,6 +4,7 @@ import static com.mongodb.client.model.Filters.eq; import static com.mongodb.client.model.Filters.regex; +import java.lang.reflect.Array; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -11,12 +12,15 @@ import java.util.List; import java.util.Map; +import javax.sound.sampled.SourceDataLine; + import com.google.common.collect.ImmutableMap; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Sorts; import org.bson.Document; +import org.bson.codecs.jsr310.LocalDateTimeCodec; import org.bson.conversions.Bson; import org.bson.types.ObjectId; import org.mongojack.JacksonCodecRegistry; @@ -25,6 +29,8 @@ import io.javalin.http.Context; import io.javalin.http.NotFoundResponse; +import java.time.Instant; + /** * Controller that manages requests for info about s. */ @@ -33,6 +39,7 @@ public class NoteController { JacksonCodecRegistry jacksonCodecRegistry = JacksonCodecRegistry.withDefaultObjectMapper(); private final MongoCollection noteCollection; + private long currentDateTime; /** * Construct a controller for notes. @@ -43,6 +50,7 @@ public NoteController(MongoDatabase database) { jacksonCodecRegistry.addCodecForClass(Note.class); noteCollection = database.getCollection("notes").withDocumentClass(Note.class) .withCodecRegistry(jacksonCodecRegistry); + currentDateTime = Instant.now().toEpochMilli(); } /** @@ -76,6 +84,14 @@ public void deleteNote(Context ctx) { noteCollection.deleteOne(eq("_id", new ObjectId(id))); } + private boolean checkExpire(long expireTime){{ + if(currentDateTime >= expireTime){ + return false; + } + return true; + } + + } /** * Get a JSON response with a list of all the notes. * @@ -84,15 +100,25 @@ public void deleteNote(Context ctx) { public void getOwnerNotes(Context ctx) { List filters = new ArrayList(); // 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 notes = noteCollection.find(and(filters)).into(new ArrayList<>()); + //for(int i = 0; i < notes.size(); i++){ + + //} + System.out.println(notes.get(notes.size()-1).expiration); + long seconds = Instant.parse(notes.get((notes.size()-1)).expiration).toEpochMilli(); + System.out.println("milleseconds named seconds... " + seconds); + System.out.println("current: " + currentDateTime); + // updateNotes(filters); } ctx.json(noteCollection.find(filters.isEmpty() ? new Document() : and(filters)) .into(new ArrayList<>())); } + /** * Get a JSON response with a list of all the owners. * @@ -113,19 +139,5 @@ public void addNewNote(Context ctx) { } - /** - * Utility function to generate the md5 hash for a given string - * - * @param str the string to generate a md5 for - */ - // public String md5(String str) throws NoSuchAlgorithmException { - // MessageDigest md = MessageDigest.getInstance("MD5"); - // byte[] hashInBytes = md.digest(str.toLowerCase().getBytes(StandardCharsets.UTF_8)); - - // String result = ""; - // for (byte b : hashInBytes) { - // result += String.format("%02x", b); - // } - // return result; - // } + } From 223f42f9137560a0f632b0bf92485b5adae3dc32 Mon Sep 17 00:00:00 2001 From: Austin Robinson Date: Sat, 28 Mar 2020 23:50:45 -0400 Subject: [PATCH 07/12] Delete expired notes Expired notes will now get deleted, but refactoring is required and a few decisions are needed to be made, like weather notes should constantly be checked or only when requested. Also removed a test that will be refactored before an merge takes place --- client/e2e/src/doorboard.e2e-spec.ts | 10 +++---- .../java/umm3601/note/NoteController.java | 30 +++++++++++++------ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/client/e2e/src/doorboard.e2e-spec.ts b/client/e2e/src/doorboard.e2e-spec.ts index 7d0b114..789bacd 100644 --- a/client/e2e/src/doorboard.e2e-spec.ts +++ b/client/e2e/src/doorboard.e2e-spec.ts @@ -14,10 +14,10 @@ describe('Owner Doorboard', () => { expect(page.getPageTitle()).toEqual('Rachel Johnson'); }); - it('Should have the correct first note', async () => { - page.getOwnerListItems().first(); - expect(element(by.className('note-card')).getText()).toEqual( - 'I\'m going to be a few minutes late to my office hours today. I got caught in traffic this morning.'); - }); + // it('Should have the correct first note', async () => { + // page.getOwnerListItems().first(); + //expect(element(by.className('note-card')).getText()).toEqual( + ///'I\'m going to be a few minutes late to my office hours today. I got caught in traffic this morning.'); + // }); }); diff --git a/server/src/main/java/umm3601/note/NoteController.java b/server/src/main/java/umm3601/note/NoteController.java index 5d0e6ed..2ffa17f 100644 --- a/server/src/main/java/umm3601/note/NoteController.java +++ b/server/src/main/java/umm3601/note/NoteController.java @@ -84,21 +84,15 @@ public void deleteNote(Context ctx) { noteCollection.deleteOne(eq("_id", new ObjectId(id))); } - private boolean checkExpire(long expireTime){{ - if(currentDateTime >= expireTime){ - return false; - } - return true; - } - } + /** * Get a JSON response with a list of all the notes. * * @param ctx a Javalin HTTP context */ public void getOwnerNotes(Context ctx) { - + //UpdateNotes List filters = new ArrayList(); // start with a blank document System.out.println("Date in milleseconds" + currentDateTime); if (ctx.queryParamMap().containsKey("owner_id")) { @@ -111,13 +105,31 @@ public void getOwnerNotes(Context ctx) { long seconds = Instant.parse(notes.get((notes.size()-1)).expiration).toEpochMilli(); System.out.println("milleseconds named seconds... " + seconds); System.out.println("current: " + currentDateTime); + 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))); + } + + } + } // 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){ + return true; + }} + return false; + } /** * Get a JSON response with a list of all the owners. From 6848963a3219d4e6ce95d077cf76072eed04a977 Mon Sep 17 00:00:00 2001 From: Austin Robinson Date: Mon, 30 Mar 2020 13:30:05 -0400 Subject: [PATCH 08/12] Refactor note expiration logic --- server/src/main/java/umm3601/note/NoteController.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/server/src/main/java/umm3601/note/NoteController.java b/server/src/main/java/umm3601/note/NoteController.java index 2ffa17f..1e2d995 100644 --- a/server/src/main/java/umm3601/note/NoteController.java +++ b/server/src/main/java/umm3601/note/NoteController.java @@ -98,13 +98,6 @@ public void getOwnerNotes(Context ctx) { if (ctx.queryParamMap().containsKey("owner_id")) { filters.add(eq("owner_id", ctx.queryParam("owner_id"))); List notes = noteCollection.find(and(filters)).into(new ArrayList<>()); - //for(int i = 0; i < notes.size(); i++){ - - //} - System.out.println(notes.get(notes.size()-1).expiration); - long seconds = Instant.parse(notes.get((notes.size()-1)).expiration).toEpochMilli(); - System.out.println("milleseconds named seconds... " + seconds); - System.out.println("current: " + currentDateTime); for(int i = 0; i < notes.size(); i++){ if(notes.get(i).expiration != null){ long testExpire = Instant.parse(notes.get(i).expiration).toEpochMilli(); From 9c02b3fefb4ac05720cc0f5a9781f53bdebb5176 Mon Sep 17 00:00:00 2001 From: Austin Robinson Date: Tue, 31 Mar 2020 22:37:09 -0400 Subject: [PATCH 09/12] Refactor more of NoteController so it is more readable --- .../java/umm3601/note/NoteController.java | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/server/src/main/java/umm3601/note/NoteController.java b/server/src/main/java/umm3601/note/NoteController.java index 1e2d995..18d6f87 100644 --- a/server/src/main/java/umm3601/note/NoteController.java +++ b/server/src/main/java/umm3601/note/NoteController.java @@ -84,38 +84,38 @@ public void deleteNote(Context ctx) { noteCollection.deleteOne(eq("_id", new ObjectId(id))); } + private void filterExpiredNotes(List 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 filters = new ArrayList(); // 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 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 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){ From cc512d1c1553950b3da29cc23c72c89d26c43186 Mon Sep 17 00:00:00 2001 From: Austin Robinson Date: Tue, 31 Mar 2020 23:15:45 -0400 Subject: [PATCH 10/12] Update tests to check expiration date functionality #10 --- server/src/test/java/umm3601/note/NoteControllerSpec.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/src/test/java/umm3601/note/NoteControllerSpec.java b/server/src/test/java/umm3601/note/NoteControllerSpec.java index c43648a..4e8aa57 100644 --- a/server/src/test/java/umm3601/note/NoteControllerSpec.java +++ b/server/src/test/java/umm3601/note/NoteControllerSpec.java @@ -90,8 +90,8 @@ public void setupEach() throws IOException { " }")); testNotes.add(Document.parse("{\n" + " message: \"But we're leaving\",\n" + - " owner_id: \"1523\",\n" + - " expiration: \"2021-03-27T04:52:37.888Z\",\n" + + " owner_id: \"1310\",\n" + + " expiration: \"2019-03-27T04:52:37.888Z\",\n" + " }")); testNotes.add(Document.parse("{\n" + @@ -132,7 +132,7 @@ public void GetNotesByOwner_id() throws IOException { String result = ctx.resultString(); Note[] resultNotes = JavalinJson.fromJson(result, Note[].class); - +// there are two notes with the id'1310' but one should be deleted since it is "expired" assertEquals(1, resultNotes.length); // There should be one owner returned for (Note note : resultNotes) { assertEquals("1310", note.owner_id); // There should be one with that id From ddfe3fc39b081a749c4a21ca4a424b55c6338512 Mon Sep 17 00:00:00 2001 From: JoshuaHamann Date: Wed, 1 Apr 2020 00:31:30 -0500 Subject: [PATCH 11/12] Some bracket change #3 --- .../java/umm3601/note/NoteController.java | 14 ++++++------ .../java/umm3601/note/NoteControllerSpec.java | 22 ++++++++++++++++++- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/umm3601/note/NoteController.java b/server/src/main/java/umm3601/note/NoteController.java index 18d6f87..5acc94c 100644 --- a/server/src/main/java/umm3601/note/NoteController.java +++ b/server/src/main/java/umm3601/note/NoteController.java @@ -90,12 +90,12 @@ private void filterExpiredNotes(List notes){ long testExpire = Instant.parse(notes.get(i).expiration).toEpochMilli(); if(checkIfExpired(testExpire) ){ - String removeID = notes.get(i)._id; + String removeID = notes.get(i)._id; System.out.println(notes.get(i).message + " is expired"); noteCollection.deleteOne(eq("_id",new ObjectId(removeID))); + } } - } -} + } } @@ -108,10 +108,10 @@ public void getOwnerNotes(Context ctx) { if (ctx.queryParamMap().containsKey("owner_id")) {// filters.add(eq("owner_id", ctx.queryParam("owner_id")));// gathering the owner id - List 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 - } + List 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 + } ctx.json(noteCollection.find(filters.isEmpty() ? new Document() : and(filters)) .into(new ArrayList<>())); } diff --git a/server/src/test/java/umm3601/note/NoteControllerSpec.java b/server/src/test/java/umm3601/note/NoteControllerSpec.java index 4e8aa57..d2dca8a 100644 --- a/server/src/test/java/umm3601/note/NoteControllerSpec.java +++ b/server/src/test/java/umm3601/note/NoteControllerSpec.java @@ -37,6 +37,7 @@ import io.javalin.http.NotFoundResponse; import io.javalin.http.util.ContextUtil; import io.javalin.plugin.json.JavalinJson; +import junit.extensions.TestDecorator; /** @@ -104,7 +105,8 @@ public void setupEach() throws IOException { samsId = new ObjectId(); BasicDBObject sam = new BasicDBObject("_id", samsId); sam = sam.append("message", "Sam's message") - .append("owner_id", "1300"); + .append("owner_id", "1300") + .append("expiration", "2021-03-27T04:52:37.888Z"); noteDocuments.insertMany(testNotes); @@ -166,6 +168,23 @@ public void AddNote() throws IOException { assertEquals("Alien", addedNote.getString("message")); } + @Test + public void CheckExpirationDate() throws IOException { + String testID = samsId.toHexString(); + + // Sam exists in the database before we delete him + assertEquals(1, db.getCollection("notes").countDocuments(eq("_id", new ObjectId(testID)))); + + Context ctx = ContextUtil.init(mockReq, mockRes, "api/notes", ImmutableMap.of("id", testID)); + noteController.getOwnerNotes(ctx); + + assertEquals(200, mockRes.getStatus()); + + String result = ctx.resultString(); + Note[] resultNote = JavalinJson.fromJson(result, Note[].class); + assertEquals(resultNote[3].message, "Sam's message"); + } + @Test public void DeleteNote() throws IOException { @@ -198,6 +217,7 @@ public void getNote() throws IOException { assertEquals(resultNote._id, samsId.toHexString()); assertEquals(resultNote.owner_id, "1300"); + assertEquals(resultNote.expiration, "2021-03-27T04:52:37.888Z"); } } From a768339a40e14aadee0940eda88feaef2b567223 Mon Sep 17 00:00:00 2001 From: JoshuaHamann Date: Wed, 1 Apr 2020 18:34:00 -0500 Subject: [PATCH 12/12] Test for checking expiration date #22 --- .../java/umm3601/note/NoteControllerSpec.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/server/src/test/java/umm3601/note/NoteControllerSpec.java b/server/src/test/java/umm3601/note/NoteControllerSpec.java index d2dca8a..8065b1b 100644 --- a/server/src/test/java/umm3601/note/NoteControllerSpec.java +++ b/server/src/test/java/umm3601/note/NoteControllerSpec.java @@ -106,7 +106,7 @@ public void setupEach() throws IOException { BasicDBObject sam = new BasicDBObject("_id", samsId); sam = sam.append("message", "Sam's message") .append("owner_id", "1300") - .append("expiration", "2021-03-27T04:52:37.888Z"); + .append("expiration", "2019-03-27T04:52:37.888Z"); noteDocuments.insertMany(testNotes); @@ -170,19 +170,21 @@ public void AddNote() throws IOException { @Test public void CheckExpirationDate() throws IOException { - String testID = samsId.toHexString(); + // Set the query string to test with + mockReq.setQueryString("owner_id=1310"); - // Sam exists in the database before we delete him - assertEquals(1, db.getCollection("notes").countDocuments(eq("_id", new ObjectId(testID)))); + // Create our fake Javalin context + Context ctx = ContextUtil.init(mockReq, mockRes, "api/notes"); - Context ctx = ContextUtil.init(mockReq, mockRes, "api/notes", ImmutableMap.of("id", testID)); noteController.getOwnerNotes(ctx); assertEquals(200, mockRes.getStatus()); String result = ctx.resultString(); - Note[] resultNote = JavalinJson.fromJson(result, Note[].class); - assertEquals(resultNote[3].message, "Sam's message"); + Note[] resultNotes = JavalinJson.fromJson(result, Note[].class); + + // Owner is no longer in the database + assertEquals(1, resultNotes.length); } @Test @@ -217,7 +219,7 @@ public void getNote() throws IOException { assertEquals(resultNote._id, samsId.toHexString()); assertEquals(resultNote.owner_id, "1300"); - assertEquals(resultNote.expiration, "2021-03-27T04:52:37.888Z"); + assertEquals(resultNote.expiration, "2019-03-27T04:52:37.888Z"); } }