Skip to content

Commit

Permalink
debug edit endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nora-kauczor committed Nov 19, 2024
1 parent c37fa31 commit 3f8144b
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public Vocab createAndActivateVocab(@RequestBody VocabDTOCreate vocabDTO, @Authe
}

@PutMapping
public Vocab editVocab(@RequestBody VocabDTOEdit editedVocab, @AuthenticationPrincipal OAuth2User user) throws IdNotFoundException, VocabIsNotEditableException, LanguageNotFoundException {
public Vocab editVocab(@RequestBody VocabDTOEdit editedVocab, @AuthenticationPrincipal OAuth2User user) throws IdNotFoundException, VocabIsNotEditableException {
System.out.println(editedVocab);
return vocabService.editVocab(editedVocab, user.getName());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package org.example.backend.vocab;


import java.time.LocalDate;
import java.util.List;
import java.util.Map;

public record VocabDTOEdit(
String id,
String word,
String translation,
String info,
String language,
Map<String, List<LocalDate>> datesPerUser,
String createdBy) {
String language) {
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public Vocab createAndActivateVocab(VocabDTOCreate vocabDTO, String userName) th
}

public Vocab editVocab(VocabDTOEdit vocabDTO, String userName) throws IdNotFoundException, VocabIsNotEditableException {
System.out.println("editVocab was called in VocabService");
System.out.println("VocabDTO: "+vocabDTO);
System.out.println("userName: "+userName);
Vocab vocab = vocabRepo.findById(vocabDTO.id()).orElseThrow(() -> new IdNotFoundException("ID not found."));
if (!vocab.getCreatedBy().equals(userName)) {
throw new VocabIsNotEditableException("Method not allowed.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ void createAndActivateVocab_throwsLanguageNotFoundException_whenCalledWithVocabW


@Test
void editVocab_shouldReturnEditedVocab_whenCalledWithVocabDTOEdit() throws IdNotFoundException, VocabIsNotEditableException, LanguageNotFoundException {
void editVocab_shouldReturnEditedVocab_whenCalledWithVocabDTOEdit() throws IdNotFoundException, VocabIsNotEditableException {
Vocab oldVocab = new Vocab("000", "la prueba", "test",
"", Language.SPANISH, new HashMap<>(), "jane-doe");
VocabDTOEdit vocabDTO = new VocabDTOEdit("000", "la prueba", "test",
"added infotext", "Spanish", new HashMap<>(), "jane-doe");
"added infotext", "Spanish");
Vocab expected = new Vocab("000", "la prueba", "test",
"added infotext", Language.SPANISH, new HashMap<>(), "jane-doe");
when(mockVocabRepo.findById("000")).thenReturn(Optional.of(oldVocab));
Expand All @@ -160,7 +160,7 @@ void editVocab_shouldReturnEditedVocab_whenCalledWithVocabDTOEdit() throws IdNot
@Test
void editVocab_shouldThrowIdNotFoundException_whenCalledWithVocabWithNonexistentId() {
VocabDTOEdit vocabDTO = new VocabDTOEdit("000", "la prueba", "test",
"added infotext", "Spanish", new HashMap<>(), "jane-doe");
"added infotext", "Spanish");
assertThrows(IdNotFoundException.class, () -> vocabService.editVocab(vocabDTO, "jane-doe"));
verify(mockVocabRepo).findById("000");
}
Expand All @@ -170,7 +170,7 @@ void editVocab_shouldThrowVocabIsNotEditableException_whenCalledWithNonEditableV
Vocab oldVocab = new Vocab("000", "la prueba", "test",
"", Language.SPANISH, new HashMap<>(), "Wordio");
VocabDTOEdit vocabDTO = new VocabDTOEdit("000", "la prueba", "test",
"added infotext", "Spanish", new HashMap<>(), "jane-doe");
"added infotext", "Spanish");
when(mockVocabRepo.findById(vocabDTO.id())).thenReturn(Optional.of(oldVocab));
assertThrows(VocabIsNotEditableException.class, () -> vocabService.editVocab(vocabDTO, "jane-doe"));
}
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ function App() {
function editVocab(editedVocab: Vocab): void {
setVocabToEdit(undefined)
setUseForm(false)
axios.put(`api/vocab/`, editedVocab)
axios.put(`api/vocab`, editedVocab)
.then(() => {
console.log(
`Successfully edited of vocab with ID ${editedVocab.id}.`)
`Successfully edited vocab with ID ${editedVocab.id}.`)
toast.success("Vocab successfully edited")
getAllVocabsOfLanguage()
})
Expand All @@ -239,7 +239,7 @@ function App() {
setDisplayNewVocabsPopUp(false)
}
setUseForm(true)
if (!id) {
if (id) {
const vocab = vocabs.find(vocab => vocab.id === id)
setVocabToEdit(vocab)
}
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/components/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function Form(props: Readonly<Props>) {
const [wordInput, setWordInput] = useState<string>("")
const [translationInput, setTranslationInput] = useState<string>("")
const [infoInput, setInfoInput] = useState<string>("")

useEffect(() => {
if (!props.vocabToEdit) {
return
Expand All @@ -33,9 +34,6 @@ export default function Form(props: Readonly<Props>) {
translation: translationInput,
info: infoInput,
language: props.language,
createdBy: props.userId,
datesPerUser: props.vocabToEdit ? props.vocabToEdit.datesPerUser :
undefined,
};
if (props.vocabToEdit) {
props.editVocab(vocab)
Expand Down

0 comments on commit 3f8144b

Please sign in to comment.