/
ToBeReinserted
58 lines (41 loc) · 1.91 KB
/
ToBeReinserted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// DOES NOT WORK
@PostMapping("/{id}/add-tag")
public String addTagToReview(@PathVariable(value = "id") long reviewId, @RequestParam(value = "name") String name) {
Optional<Review> reviewOptional = reviewRepo.findById(reviewId);
Review review = reviewOptional.get();
Long reviewOptionalId = review.getId();
Tag tagSearchResult = tagRepo.findByNameIgnoreCaseLike(name);
if (!(tagSearchResult == null)) {
Collection<Review> reviewSearchResults = reviewRepo.findByTagsId(tagSearchResult.getId());
if (!(reviewSearchResults.contains(reviewOptionalId))) {
tagSearchResult.addReview(review);
tagRepo.save(tagSearchResult);
return "Tag found. Review NOT found. Adding review to Tag. Tag Saved.";
}
return "Tag found. Review found. Doing nothing (supposedly).";
} else {
tagSearchResult = new Tag(name, review);
tagRepo.save(tagSearchResult);
return "Tag NOT found. Creating new tag and adding review to it. Tag Saved.";
}
}
// WORKS but has errors; repo should bring up a collection of reviews (many to many relationship)
@PostMapping("/{id}/add-tag")
public String addTagToReview(@PathVariable(value = "id") long reviewId, @RequestParam(value = "name") String name) {
Optional<Review> reviewOptional = reviewRepo.findById(reviewId);
Review review = reviewOptional.get();
Tag tagSearchResult = tagRepo.findByNameIgnoreCaseLike(name);
if (!(tagSearchResult == null)) {
Review reviewSearchResult = reviewRepo.findByTagsId(tagSearchResult.getId());
if (reviewSearchResult != review ) {
tagSearchResult.addReview(review);
tagRepo.save(tagSearchResult);
return "Tag found. Review NOT found. Adding review to Tag. Tag Saved.";
}
return "Tag found. Review found. Doing nothing (supposedly).";
} else {
tagSearchResult = new Tag(name, review);
tagRepo.save(tagSearchResult);
return "Tag NOT found. Creating new tag and adding review to it. Tag Saved.";
}
}